Thresholding, Masking, and Remapping Images
1 Implementing a Threshold-help us partition the variability of images into categories
filterBounds:通过与几何体的相交来过滤一个集合的快捷方式。集合中的项目如果不能与给定的几何体相交,就会被排除。
gt 方法来自布尔运算符系列,即 gt 是一个函数,它在每个像素中执行测试,如果测试的计算结果为 true,则返回值 1,否则返回0,此布尔系列中的其他运算符包括小于 (lt)、小于或等于 (lte)、等于 (eq)、不等于 (neq) 和大于或等于 (gte) 等。
// Create an NDVI image using Sentinel 2.
var seaPoint = ee.Geometry.Point(-122.2040, 47.6221);
var seaImage = ee.ImageCollection('COPERNICUS/S2')
.filterBounds(seaPoint)
.filterDate('2020-08-15', '2020-10-01')
.first();
var seaNDVI = seaImage.normalizedDifference(['B8', 'B4']);
// And map it.
Map.centerObject(seaPoint, 10);
var vegPalette = ['red', 'white', 'green'];
Map.addLayer(seaNDVI,
{ min: -1, max: 1,
palette: vegPalette },
'NDVI Seattle');
// Implement a threshold.
var seaVeg = seaNDVI.gt(0.5);
// Map the threshold.
Map.addLayer(seaVeg, {
min: 0,
max: 1,
palette: ['white', 'green'] },
'Non-forest vs. Forest');
where
我们可以使用 seaNDVI.gte(−0.1).and(seaNDVI.lt(0.5)) 来识别具有中间 NDVI 级别的像素。
// Implement .where.
// Create a starting image with all values = 1. v
var seaWhere = ee.Image(1)
// Use clip to constrain the size of the new image.
.clip(seaNDVI.geometry());
// Make all NDVI values less than -0.1 equal 0.
seaWhere = seaWhere.where(seaNDVI.lte(-0.1), 0);
// Make all NDVI values greater than 0.5 equal 2.
seaWhere = seaWhere.where(seaNDVI.gte(0.5), 2);
// Map our layer that has been divided into three classes.
Map.addLayer(seaWhere,
{ min: 0, max: 2,
palette: ['blue', 'white', 'green'] },
'Water, Non-forest, Forest');
2 Masking Specific Values in an Image
removes specific areas of an image—those covered by the mask—from being displayed or analyzed.
// Implement masking.
// View the seaVeg layer's current mask.
Map.centerObject(seaPoint, 9);
Map.addLayer(seaVeg.mask(), {}, 'seaVeg Mask');
// Create a binary mask of non-forest.
var vegMask = seaVeg.eq(1);
// Update the seaVeg mask with the non-forest mask.
var maskedVeg = seaVeg.updateMask(vegMask);
// Map the updated Veg layer
Map.addLayer(maskedVeg,
{ min: 0,
max: 1,
palette: ['green'] },
'Masked Forest Layer');
// Map the updated mask
Map.addLayer(maskedVeg.mask(), {}, 'maskedVeg Mask');
Remapping Values in an Image
采用图像中的特定值,并为其分配不同的值。这对于分类数据集特别有用
// Implement remapping.
// Remap the values from the seaWhere layer.
var seaRemap = seaWhere.remap([0, 1, 2], // Existing values.
[9, 11, 10]); // Remapped values.
Map.addLayer(seaRemap,
{ min: 9,
max: 11,
palette: ['blue', 'green', 'white'] },
'Remapped Values');