GEE-mask/unmask/upmask/selfmask

1. mask函数

// imageA、imageB为影像
var maska = imageA.mask();
var imageBNew = imageB.updateMask(maska);
//使用imageA将imageB掩膜掉,也就是将imageB中imageA的部分提取出来。
//如果只是单纯的使用image.mask(),会将影像的掩膜区域与非掩膜区域用0和1区分开来,其中1代表为掩膜,0代表未掩膜。
//ma

sk(只能单波段)

2.unmask函数

//使用方法,image.unmask(number),unmask的作用在于它可以将掩膜掉的区域影像值替换为任意值, 而未掩膜的区域保持原值。
//例如image.unmask(-9999)会将掩膜掉的区域中的值替换为-9999,这样影像就可以与arcgis无缝兼容了。
//此外,还可以将特定条件的掩膜替换为指定的值,例如想要将imageB中小于0的部分统一替换为0,
var a = imageB.mask(imageB.gte(0)).unmask(0);

3.updateMask函数

//使用方法:image.updateMask(),updateMask会将影像上为0的区域掩膜掉,
//在现有掩码不为零的所有位置更新图像的掩码。输出图像保留输入图像的元数据和足迹。
//图像的新掩码,作为0, 1范围内的浮点值。如果此图像有一个波段,则用于输入图像中的所有波段;否则,必须具有与输入图像相同的波段数。
//如果你希望某一景影像与另一景影像具有相同的掩膜,可以使用image2.updateMask(image1.mask().not())。

4 ee.Image.selfMask

//Updates an image's mask at all positions where the existing mask is not zero using the value of the image as the new mask value. The output image retains the metadata and footprint of the input image.
使用图像的值作为新的掩码值,在现有掩码不为零的所有位置更新图像的掩码。输出图像保留输入图像的元数据和占用空间。

example 1

// A Sentinel-2 surface reflectance image.
var img = ee.Image('COPERNICUS/S2_SR/20210109T185751_20210109T185931_T10SEG');
var trueColor = {
  bands: ['B4', 'B3', 'B2'],
  min: 0,
  max: 2700,
  gamma: 1.3
};
print('Sentinel-2 image', img);
Map.setCenter(-122.36, 37.47, 10);
Map.addLayer(img, trueColor, 's2 image')
Map.centerObject(img)
 // 多波段掩膜结果只有黑白即01; each band has an independent mask.
// Valid pixels are value 1, invalid are 0.
var multiBandMaskImg = img.mask();
print('Multi-band mask image', multiBandMaskImg);
Map.addLayer(multiBandMaskImg,null, 'Multi-band mask image');

// 单波段掩膜结果只有黑白即01
var singleBandMaskImg = img.select('B1').mask();
print('Single-band mask image', singleBandMaskImg);
Map.addLayer(singleBandMaskImg,null, 'Single-band mask image');


//# **********掩膜掉(没有)不符合条件的像素,使用updateMask**********
// 利用SWIR1创建land的布尔波段,其中water为0,land为1
// 利用SWIR1创建land的布尔波段,其中water为0-blue,land为1-lightgreen
var landMask = img.select('B11').gt(100);
print('Land mask', landMask);
Map.addLayer(landMask, {palette: ['blue', 'lightgreen']}, 'Land mask');

// Apply the single-band land mask to all image bands; pixel values equal to 0
// in the mask become invalid in the image.

//应用掩膜波段进行更新原影像,将使得影像上只要是掩膜等于0的像元均变为无效值
var imgMasked = img.updateMask(landMask);//updateMask会将影像上为0的区域掩膜掉,只保留了land,非0区不显示
print('Image, land only', imgMasked);
  Map.addLayer(imgMasked,trueColor, 'Image, land only');
// **********将掩膜区域全部替换为某一值,使用unmask**********
// 设置无效的掩膜值,将非掩膜区域替换为32767空值出现
var imageUnmasked = imgMasked.unmask(32767)
Map.addLayer(imageUnmasked, trueColor, 'image unmasked')
 
// 将掩膜像素重置为有效,使用默认值 0 填充,输入足迹
var maskResetFootprint = imgMasked.unmask()
Map.addLayer(maskResetFootprint, trueColor, 'maskResetFootprint')
 
// 用另外1幅影像像元数值替换
var fill = ee.Image('COPERNICUS/S2_SR/20200618T184919_20200618T190341_T10SEG')
var imgFill = imgMasked.unmask(fill)
Map.addLayer(fill, trueColor, 'new s2')
Map.addLayer(imgFill, trueColor, 'image filled')

//0到1之间的浮点掩码值将用于定义不透明度
var landMaskFloat = landMask.add(0.65);
var imgMaskedFloat = img.updateMask(landMaskFloat);
print('Image, partially transparent', imgMaskedFloat);
Map.addLayer(imgMaskedFloat, trueColor, 'Image, partially transparent');
// **********掩膜掉(没有)非0的像素,使用selfmask**********
var selfmask=landMask.selfMask()
Map.addLayer(selfmask, {palette: ['gold']},"selfmask")

exmaple 2 研究区NDVI提取

// 求NDVI公式
var getNDVI = function(image) {
  return image.normalizedDifference(['B4', 'B3']);
};
// 加载landsat5 两张间隔20年的影像
var image1 = ee.Image('LANDSAT/LT05/C02/T1_TOA/LT05_044034_19900604');
var image2 = ee.Image('LANDSAT/LT05/C02/T1_TOA/LT05_044034_20100611');

// 计算NDVI,一个图像直接调用即可
var ndvi1 = getNDVI(image1);
var ndvi2 = getNDVI(image2);

// 计算NDVI的差值
var ndviDifference = ndvi2.subtract(ndvi1);
// Load the land mask from the SRTM DEM.mask()区分出01
//var landMask= ee.Image('CGIAR/SRTM90_V4').mask(image1.select("B1")).mask();
var landMask = ee.Image('CGIAR/SRTM90_V4').mask()// 提取出有land的NDVI区域
var maskedDifference = ndviDifference.updateMask(landMask);

// 显示图像
var vizParams = {min: -0.5, max: 0.5, palette: ['FF0000', 'FFFFFF', '0000FF']};
Map.setCenter(-122.2531, 37.6295, 9);
Map.addLayer(maskedDifference, vizParams, 'NDVI difference');

example3 均值去云结合

// NDVI公式
var addNDVI = function(image) {
  return image.addBands(image.normalizedDifference(['B5', 'B4']));
};

// 云量公式
//ee.Algorithms.Landsat.simpleCloudScore(image)使用亮度、温度和NDSI的组合计算范围[0,100]内的简单云可能性分数。这不是一个强大的云探测器,主要用于比较在同一点的相对云可能性的多个观察。The Landsat TOA image to process.
var cloudMask = function(image) {
  var clouds = ee.Algorithms.Landsat.simpleCloudScore(image).select(['cloud']);
  return image.updateMask(clouds.lt(10));
};

// Load a Landsat collection, map the NDVI and cloud masking functions over it.
var collection = ee.ImageCollection('LANDSAT/LC08/C02/T1_TOA')
  .filterBounds(ee.Geometry.Point([-122.262, 37.8719]))
  .filterDate('2014-03-01', '2014-05-31')
  .map(addNDVI)
  .map(cloudMask);

// Reduce the collection to the mean of each pixel and display.多幅图像均值为一幅
var meanImage = collection.reduce(ee.Reducer.mean());
var vizParams = {bands: ['B5_mean', 'B4_mean', 'B3_mean'], min: 0.02, max: 0.4};
Map.addLayer(meanImage, vizParams, 'mean');

// 加载研究区通过城市数据筛选
var counties = ee.FeatureCollection('TIGER/2018/Counties');
var santaClara = ee.Feature(counties.filter(ee.Filter.eq('NAME', 'Santa Clara')).first());
Map.addLayer(santaClara);

// Get the mean of NDVI in the region.对一景图像求一个均值
var mean = meanImage.select(['nd_mean']).reduceRegion({
  reducer: ee.Reducer.mean(),
  geometry: santaClara.geometry(),
  scale: 30
});

// Print mean NDVI for the region.
print(mean,'Mean')
//直接显示出均值,print则是以字典形式
mean.get('nd_mean').evaluate(function(val){
  print('Santa Clara spring mean NDVI:', val);
});

均值
研究区
不同方式获得均值

//ee.Number.evaluate 
//异步地从服务器检索该对象的值,并将其传递给所提供的回调函数。
var numberServer = ee.Number(10.3);
// Use evaluate to transfer server-side number to the client.
numberServer.evaluate(function(numberClient) {
  print('Client-side primitive data type', typeof numberClient);  // number
  print('Client-side number', numberClient);  // 10.3
  print('Client-side number used in expression', numberClient + 10);  // 20.3
});

mask理解

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值