Học Google Earth Engine 15. Tạo mặt nạ đám mây trên gee Cloud Masking

Học Google Earth Engine 15. Tạo mặt nạ đám mây trên gee Cloud Masking

4.4. Mặt nạ đám mây – Cloud Masking

Việc che các pixel trong một hình ảnh làm cho các pixel đó trở nên trong suốt và loại trừ chúng khỏi phân tích và hình ảnh hóa. Để che dấu một hình ảnh, chúng ta có thể sử dụng hàm updateMask() và chuyển cho nó một hình ảnh với các giá trị 0 và 1. Tất cả các pixel có hình ảnh mặt nạ là 0 sẽ bị che đi.

Hầu hết các bộ dữ liệu viễn thám đều có dải QA hoặc Cloud Mask chứa thông tin về việc các pixel có bị vẩn đục hay không. Trình chỉnh sửa mã của bạn (Code Editor) chứa các chức năng được xác định trước (pre-defined) để tạo mặt nạ cho các đám mây cho các bộ dữ liệu phổ biến trong Scripts Tab → Examples → Cloud Masking.

Tập lệnh dưới đây sử dụng chức năng tạo mặt nạ Sentinel-2 và chỉ ra cách áp dụng nó trên một hình ảnh.

var image = ee.Image(‘COPERNICUS/S2/20190703T050701_20190703T052312_T43PGP’)

var rgbVis = {

  min: 0.0,

  max: 3000,

  bands: [‘B4’, ‘B3’, ‘B2’],

};

 

Map.centerObject(image)

Map.addLayer(image, rgbVis, ‘Full Image’, false)

 

// Write a function for Cloud masking

function maskS2clouds(image) {

  var qa = image.select(‘QA60’)

  var cloudBitMask = 1 << 10;

  var cirrusBitMask = 1 << 11;

  var mask = qa.bitwiseAnd(cloudBitMask).eq(0).and(

             qa.bitwiseAnd(cirrusBitMask).eq(0))

  return image.updateMask(mask)

      .select(“B.*”)

      .copyProperties(image, [“system:time_start”])

}

 

var maskedImage = ee.Image(maskS2clouds(image))

Map.addLayer(maskedImage, rgbVis, ‘Masked Image’)

 

Applying pixel-wise QA bitmask 

4.4.1. Bài tập

// Get the Level-2A Surface Reflectance image

var imageSR = ee.Image(‘COPERNICUS/S2_SR/20190703T050701_20190703T052312_T43PGP’)

var rgbVis = {

  min: 0.0,

  max: 3000,

  bands: [‘B4’, ‘B3’, ‘B2’],

};

Map.centerObject(imageSR)

Map.addLayer(imageSR, rgbVis, ‘SR Image’)

 

 

// Function to remove cloud and snow pixels from Sentinel-2 SR image

function maskCloudAndShadowsSR(image) {

  var cloudProb = image.select(‘MSK_CLDPRB’);

  var snowProb = image.select(‘MSK_SNWPRB’);

  var cloud = cloudProb.lt(5);

  var snow = snowProb.lt(5);

  var scl = image.select(‘SCL’); 

  var shadow = scl.eq(3); // 3 = cloud shadow

  var cirrus = scl.eq(10); // 10 = cirrus

  // Cloud probability less than 5% or cloud shadow classification

  var mask = (cloud.and(snow)).and(cirrus.neq(1)).and(shadow.neq(1));

  return image.updateMask(mask);

}

 

// Exercise

// Apply the above cloud masking function to SR image

// Add the masked image to the map

 

// Hint: After adding the masked image to the map, turn-off

// the original image layer to see the result of the masking function

 

// Áp dụng chức năng tạo mặt nạ đám mây ở trên cho hình ảnh SR

// Thêm hình ảnh mặt nạ vào bản đồ

 

// Gợi ý: Sau khi thêm hình ảnh mặt nạ vào bản đồ, hãy tắt

// lớp ảnh gốc để xem kết quả của hàm che

 

 

4.4.2. Kết quả

// Get the Level-2A Surface Reflectance image

var imageSR = ee.Image(‘COPERNICUS/S2_SR/20190703T050701_20190703T052312_T43PGP’)

var rgbVis = {

  min: 0.0,

  max: 3000,

  bands: [‘B4’, ‘B3’, ‘B2’],

};

Map.centerObject(imageSR)

Map.addLayer(imageSR, rgbVis, ‘SR Image’, false)

 

// Exercise

// Apply the above cloud masking function to SR image

// Add the masked image to the map

 

// Hint: After adding the masked image to the map, turn-off

// the original image layer to see the result of the masking function

 

// Áp dụng chức năng tạo mặt nạ đám mây ở trên cho hình ảnh SR

// Thêm hình ảnh mặt nạ vào bản đồ

 

// Gợi ý: Sau khi thêm hình ảnh mặt nạ vào bản đồ, hãy tắt

// lớp ảnh gốc để xem kết quả của hàm che

 

 

 

// Function to remove cloud and snow pixels from Sentinel-2 SR image

function maskCloudAndShadowsSR(image) {

  var cloudProb = image.select(‘MSK_CLDPRB’);

  var snowProb = image.select(‘MSK_SNWPRB’);

  var cloud = cloudProb.lt(5);

  var snow = snowProb.lt(5);

  var scl = image.select(‘SCL’); 

  var shadow = scl.eq(3); // 3 = cloud shadow

  var cirrus = scl.eq(10); // 10 = cirrus

  // Cloud probability less than 5% or cloud shadow classification

  var mask = (cloud.and(snow)).and(cirrus.neq(1)).and(shadow.neq(1));

  return image.updateMask(mask)

              .select(“B.*”)

              .copyProperties(imageSR,[“system:time_start”]);

}

 

var maskedImage = ee.Image(maskCloudAndShadowsSR(imageSR))

Map.addLayer(maskedImage, rgbVis, ‘Masked Image’)

 

 

Nếu bạn đang sử dụng dữ liệu Sentinel-2, hãy xem kỹ thuật tạo mặt nạ đám mây thay thế bằng cách sử dụng bộ dữ liệu S2 Cloudless. Tìm hiểu thêm 

var imageSR = ee.Image(‘COPERNICUS/S2_SR/20190703T050701_20190703T052312_T43PGP’)

var s2Cloudless = ee.Image(‘COPERNICUS/S2_CLOUD_PROBABILITY/20190703T050701_20190703T052312_T43PGP’)

var clouds = s2Cloudless.lt(50)

var cloudlessMasked = imageSR.mask(clouds)

var rgbVis = {min: 0.0, max: 3000, bands: [‘B4’, ‘B3’, ‘B2’]};

Map.addLayer(cloudlessMasked, rgbVis, ‘S2 Cloudless Masked Image’)

 

Xem video

Recommended For You

About the Author: Admin