Tạo hàm lọc bỏ mây (cloud mask)

Hủy bỏ tất cả các đám mây trong bộ sưu tập hình ảnh.

This introductory training is part of the official training materials prepared by the United States Department of Agriculture.

Code Editor cho phép người dùng thực hiện được tất cả các chức năng của Earth Engine, tuy nhiên, cần có hiểu biết cơ bản về lập trình và JavaScript. Trong bài tập này, chúng ta sẽ tiếp tục học về ngôn ngữ lập trình Java và các khái niệm mới về dữ liệu không gian trong Earth Engine. Bạn sẽ tiếp tục những gì được học ở bài tập 2 về đối tượng ảnh; tuy nhiên trong bài học này bạn sẽ tập trung nhiều hơn vào các tập hợp hoặc chồng lớp của các đối tượng ảnh có tính chất tương tự. Trong bài học này, bạn sẽ tập trung vào các khái niệm cơ bản và phương pháp liên quan đến các tập hợp ảnh trong Earth Engine. Đây là hướng dẫn giúp người sử dụng có thể viết được các sript đơn giản trong JavaScript.

 

Tạo hàm lọc bỏ mây (cloud mask)

1. Copy và paste các dòng lệnh sau vào một cửa sổ Code Editor trống. Trông chúng khá quen vì bạn đã làm việc với chúng từ bài tập 2. Chạy script và đảm bảo nó được thực hiện.

// Get an image and display in map window.
var LC8_image = ee.Image('LANDSAT/LC8_L1T_TOA/LC81290502013110LGN01');

//Specify the cloud likelihood threshold -
var cloud_thresh = 40;

//use add the cloud likelihood band to the image
var CloudScore = ee.Algorithms.Landsat.simpleCloudScore(LC8_image);
//isolate the cloud likelihood band
var quality = CloudScore.select('cloud');
//get pixels above the threshold
var cloud01 = quality.gt(cloud_thresh);
//create a mask from high likelihood pixels
var cloudmask = LC8_image.mask().and(cloud01.not());
//mask those pixels from the image
var LC8_imageNoClouds = LC8_image.mask(cloudmask);
//Review the result
addToMap(LC8_imageNoClouds, {bands:['B6','B5','B4'],min:0.1, max:0.5},'Landsat8scene_cloudmasked');

2. Bây giờ chỉnh sửa lại để chuyển nó một thành hàm. Đầu tiên, phía trước “use add the cloud likelihood band to the image”, thêm một dòng như sau

var cloudfunction = function(){

3. Phía trước dòng ghi chú ‘Review the result’, chỉ thêm duy nhất một dấu ngoặc móc }. Phần code của bạn sẽ trông thế này:

 // Get an image and display in map window.
var LC8_image = ee.Image('LANDSAT/LC8_L1T_TOA/LC81290502013110LGN01');

//Specify the cloud likelihood threshold -
var cloud_thresh = 40;

var cloudfunction = function(){
//use add the cloud likelihood band to the image
var CloudScore = ee.Algorithms.Landsat.simpleCloudScore(LC8_image);
//isolate the cloud likelihood band
var quality = CloudScore.select('cloud');
//get pixels above the threshold
var cloud01 = quality.gt(cloud_thresh);
//create a mask from high likelihood pixels
var cloudmask = LC8_image.mask().and(cloud01.not());
//mask those pixels from the image
var LC8_imageNoClouds = LC8_image.mask(cloudmask);
}
//Review the result
addToMap(LC8_imageNoClouds, {bands:['B6','B5','B4'],min:0.1, max:0.5},'Landsat8scene_cloudmasked');

4. Tiếp theo bạn cần định rõ tham số đầu vào. Trong ngoặc đơn ở dòng thứ 7 (dòng khai báo hàm: var cloudfunction = function()), thêm từ image

5. Sau đó thay biến LC8_image  bằng image. Như vậy là có 3 vị trí LC8_image cần được thay thế, xem ví dụ code dưới đây

// Get an image and display in map window.
var LC8_image = ee.Image('LANDSAT/LC8_L1T_TOA/LC81290502013110LGN01');

//Specify the cloud likelihood threshold -
var cloud_thresh = 40;

var cloudfunction = function(image){
//use add the cloud likelihood band to the image
var CloudScore = ee.Algorithms.Landsat.simpleCloudScore(image);
//isolate the cloud likelihood band
var quality = CloudScore.select('cloud');
//get pixels above the threshold
var cloud01 = quality.gt(cloud_thresh);
//create a mask from high likelihood pixels
var cloudmask = image.mask().and(cloud01.not());
//mask those pixels from the image
var LC8_imageNoClouds = image.mask(cloudmask);
}
//Review the result
addToMap(LC8_imageNoClouds, {bands:['B6','B5','B4'],min:0.1, max:0.5},'Landsat8scene_cloudmasked');

6. Cuối cùng, chúng ta cần thêm một lệnh return để kết quả, LC8_imageNoClouds là một biến toàn cục mà chúng ta có thể sử dụng bên ngoài hàm. Thay dòng code dưới đây

  //mask those pixels from the image
var LC8_imageNoClouds = LC8_image.mask(cloudmask);

Bằng lệnh return như sau:

return LC8_image.mask(cloudmask);

7. Bây giờ chúng ra đã sẵn sàng để gọi hàm. Sau khi đóng ngoặc móc } tại vị trí kết thúc cuối hàm, tạo một biến mới tên là LC8_imageNoClouds. Sau đó chạy hàm trên ảnh chúng ta vừa lưu là LC8_image. Xem ví dụ dòng lệnh sau:

var LC8_imageNoClouds = cloudfunction(LC8_image);

8. Click Run để xem hàm của chúng ta có làm việc không. Tham khảo toàn bộ script dưới đây để đảm bảo những gì bạn viết là đúng

// Get an image and display in map window.
var LC8_image = ee.Image('LANDSAT/LC8_L1T_TOA/LC81290502013110LGN01');

//Specify the cloud likelihood threshold -
var cloud_thresh = 40;

var cloudfunction = function(image){
//use add the cloud likelihood band to the image
var CloudScore = ee.Algorithms.Landsat.simpleCloudScore(image);
//isolate the cloud likelihood band
var quality = CloudScore.select('cloud');
//get pixels above the threshold
var cloud01 = quality.gt(cloud_thresh);
//create a mask from high likelihood pixels
var cloudmask = image.mask().and(cloud01.not());
//mask those pixels from the image
return image.mask(cloudmask);
}


var LC8_imageNoClouds = cloudfunction(LC8_image);
//Review the result
addToMap(LC8_imageNoClouds, {bands:['B6','B5','B4'],min:0.1, max:0.5},'Landsat8scene_cloudmasked');

9. Lưu scrip của bạn với tên gọi

Leave a Reply