Google Earth Engine(GEE)填补缺失影像

今日分享:

Google Earth Engine(GEE)填补缺失影像

之前在做月合成NDVI的过程中,发现如果研究区较大时,一个月的影像覆盖不了整个研究区,就会有缺失的地方,还有就是去云之后,有云量的地区变成空值。例如:

942d6439be82e216b08610c6d8c98ef6.png

所以今天来用一种插值的方法,来填补缺失的影像,以NDVI为例。

主要实现原理其实就是用前后两个月的NDVI的均值进行填补

01

GEE部分实现代码

选择研究区

var roi = ee.FeatureCollection("users/lilei655123/shanxi");
Map.centerObject(roi,7)
var styling = {color:"red",fillColor:"00000000"};
Map.addLayer(roi.style(styling),{},"geometry")
var img_normalize = function(img){ 
  var minMax = img.reduceRegion({ 
    reducer:ee.Reducer.minMax(), 
    geometry: roi, 
    scale: 30, 
    maxPixels: 10e13, 
    tileScale: 16 }) 
var year = img.get('year') 
var normalize = ee.ImageCollection.fromImages( 
  img.bandNames().map(function(name){ 
    name = ee.String(name); 
    var band = img.select(name); 
    return band.unitScale(ee.Number(minMax.get(name.cat('_min'))), ee.Number(minMax.get(name.cat('_max')))); }) 
        ).toBands().rename(img.bandNames()); 
        return normalize;
  
}

landat5的去云函数,加载数据集LANDSAT/LT05/C02/T1_L2

function maskL457sr(image) {//l57去云
  // Bit 0 - Fill
  // Bit 1 - Dilated Cloud
  // Bit 2 - Unused
  // Bit 3 - Cloud
  // Bit 4 - Cloud Shadow
  var qaMask = image.select('QA_PIXEL').bitwiseAnd(parseInt('11111', 2)).eq(0);
  var saturationMask = image.select('QA_RADSAT').eq(0);


  // Apply the scaling factors to the appropriate bands.
  var opticalBands = image.select('SR_B.').multiply(0.0000275).add(-0.2);
  var thermalBand = image.select('ST_B6').multiply(0.00341802).add(149.0);


  // Replace the original bands with the scaled ones and apply the masks.
  return image.addBands(opticalBands, null, true)
      .addBands(thermalBand, null, true)
      .updateMask(qaMask)
      .updateMask(saturationMask);
}
var imageCollection = ee.ImageCollection('LANDSAT/LT05/C02/T1_L2').filterBounds(roi);

筛选影像,合成原始影像的月NDVI

var composites = ee.ImageCollection.fromImages(monthCount.map(function(m) {
  var startMonth = 1; // 从1月开始
  var startYear = ee.Number(2000); 
  var month = ee.Date.fromYMD(startYear, startMonth, 1).advance(m,'month').get('month');
  var year = ee.Date.fromYMD(startYear, startMonth, 1).advance(m,'month').get('year')
  // 按年筛选,然后按月筛选
  var filtered = imageCollection.filter(ee.Filter.calendarRange({
    start: year.subtract(1), // 
    end: year,
    field: 'year'
  })).filter(ee.Filter.calendarRange({
    start: month,
    field: 'month'
  }));
  // mask for clouds and then take the median///
  var composite = filtered.map(maskL457sr).median().clip(roi);
  return composite.normalizedDifference(['SR_B4', 'SR_B3']).rename('NDVI')
      .set('month', ee.Date.fromYMD(startYear, startMonth, 1).advance(m,'month'))
      .set('system:time_start', ee.Date.fromYMD(startYear, startMonth, 1).advance(m,'month').millis());
}));
print(composites)

然后用前后两个月的平均值替换被遮挡的像素

var replacedVals = composites.map(function(image){
  var currentDate = ee.Date(image.get('system:time_start'));
  var meanImage = composites.filterDate(
                currentDate.advance(-2,'month'), currentDate.advance(2, 'month')).mean();//33333333333333333333333max min median
  // 替换所有被屏蔽的值
  return meanImage.where(image, image);
});

显示插值后的结果,并导出结果

var Vis = {
  min: -1,
  max: 1.0,
  palette: [
    'FFFFFF', 'CE7E45', 'DF923D', 'F1B555', 'FCD163', '99B718', '74A901',
    '66A000', '529400', '3E8601', '207401', '056201', '004C00', '023B01',
    '012E01', '011D01', '011301'
  ],
};
Map.addLayer(compos.select(6), Vis, '插值前');
Map.addLayer(stacked.select(6), Vis, 'NDVI');
Export.image.toDrive({
  image: stacked.select(0),//选择导出影像的波段0-11  分别代表1-12个月
  description: 'NDVI',//选择导出云盘的文件夹名称
  crs: "EPSG:4326",//坐标系
  scale: 30,//空间分辨率
  region: roi,//研究区
  maxPixels: 1e13,//最大像元个数
  folder: 'NDVI'
});

02


结果显示

73f0eee166fe42767dc29c94cff444cd.png

填补前

56513b9e590747181d5a5f3aa122497f.png

填补后

可以看出,填补的效果还是非常明显的。

完整代码请在公众号后台私信“0914填补缺失影像”

感谢关注,欢迎转发!

声明:仅供学习使用!

希望关注的朋友们转发,如果对你有帮助的话记得给小编点个赞或者在看

  • 13
    点赞
  • 117
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 9
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

生态遥感监测笔记

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值