基于 GEE 利用插值方法填补缺失影像

目录

[1 完整代码](#1 完整代码)

[2 运行结果](#2 运行结果)


利用GEE合成NDVI时,如果研究区较大,一个月的影像覆盖不了整个研究区,就会有缺失的地方,还有就是去云之后,有云量的地区变成空值。

所以今天来用一种插值的方法来填补缺失的影像,以NDVI为例,主要实现原理其实就是用前后两个月的NDVI的均值进行填补。

1 完整代码

javascript 复制代码
var roi = table;
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;
  
}
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);
}
/*function maskL8sr(image) {
  // Bit 0 - Fill
  // Bit 1 - Dilated Cloud
  // Bit 2 - Cirrus
  // 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 thermalBands = image.select('ST_B.*').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(thermalBands, null, true)
      .updateMask(qaMask)
      .updateMask(saturationMask);
}*/
var imageCollection = ee.ImageCollection('LANDSAT/LT05/C02/T1_L2').filterBounds(roi);//1111111
var monthCount = ee.List.sequence(0, 11);



// 通过图像收集,生成每月NDVI中值图像
var composites = ee.ImageCollection.fromImages(monthCount.map(function(m) {
  var startMonth = 1; // 从1月开始
  var startYear = ee.Number(2000); // 1993-1
  
  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 stackCollection = function(collection) {
  // 创建一个初始图像.
  var first = ee.Image(collection.first()).select([]);

  // Write a function that appends a band to an image.
  var appendBands = function(image, previous) {
    return ee.Image(previous).addBands(image);
  };
  return ee.Image(collection.iterate(appendBands, first));
};
var compos = stackCollection(composites);
print('插值前', compos);


// 用上个月和下个月的平均值替换被遮挡的像素 
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);
});

// 将ImageCollection堆叠成一个多波段的光栅,以便下载
var stackCollection = function(collection) {
  // 创建一个初始图像.
  var first = ee.Image(collection.first()).select([]);

  // Write a function that appends a band to an image.
  var appendBands = function(image, previous) {
    return ee.Image(previous).addBands(image);
  };
  return ee.Image(collection.iterate(appendBands, first));
};
var stacked = stackCollection(replacedVals);
print('stacked image', stacked);
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, '插值前');
// .0-11  分别代表1-12个月
Map.addLayer(stacked.select(6), Vis, 'NDVI');//555555555

Export.image.toDrive({
  image: stacked.select(0),//选择导出影像的波段0-11  分别代表1-12个月
  description: 'NDVI',//选择导出云盘的文件夹名称
  crs: "EPSG:4326",//坐标系
  scale: 30,//空间分辨率
  region: roi,//研究区
  maxPixels: 1e13,//最大像元个数
  folder: 'NDVI'
});

2 运行结果

填补空值之前的效果
填补空值之后的效果

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

相关推荐
2501_9304724413 小时前
05_数据库迁移腾讯云_DTS评估与预检回滚清单
数据库·云计算·腾讯云
liyuanchao_blog1 天前
OVN/OVS场景下虚拟机新网卡通过 DHCP 获取 IP 地址的完整过程
网络·网络协议·tcp/ip·云计算
YOLO数据集集合1 天前
无人机高分辨率多树种单木分割数据集 | 单木分割 无人机林业 树种识别 实例分割 点云 遥感数据集 深度学习 精准林业9047期
人工智能·深度学习·数据集·无人机·遥感数据集·点云数据集·树木分割
论文复现现场1 天前
MiniMaxH3 生成视频速度慢、电脑带不动怎么办?用 RTX 5090 云端镜像快速运行
云计算·电脑·音视频·gpu算力
2501_930472441 天前
03_AWS迁移腾讯云_组件差异风险清单与工作量WBS
云计算·腾讯云·aws
sbjdhjd1 天前
PHP RCE 多层绕过实战:关键字过滤、空格替换与 php://filter 伪协议 | 02
网络·安全·web安全·网络安全·云计算·安全架构·rce
论文复现现场2 天前
RTX 4090 24GB 能跑 Qwen3.8-27B 吗?单卡显存计算与云端部署指南
人工智能·python·云计算·llama·gpu算力
EW Frontier2 天前
实测|8 种多载波调制识别数据集如何生成?原理 + 完整 MATLAB 流程拆解【文末附MATLAB代码链接】
数据集·多载波调制·ofdm、fbmc·ufmc、fofdm·wola、gfdm·otfs、afdm
聚搜云——JuSouClouD2 天前
杭州腾讯云代理商:腾讯云2核4G服务器够用吗?网站、API和开发测试怎么选
服务器·云计算·腾讯云
Sophnet云平台2 天前
从 IT 自嗨到业务可用,制造企业 AI 平台的落地实践
大数据·人工智能·llm·制造·token·云平台