You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Google Earth Engine中bicubic重采样Sentinel-2A ImageCollection异常求助

修复Sentinel-2影像双三次重采样显示异常的方案

问题根源

Sentinel-2的波段是多分辨率的(10m、20m、60m不等),你当前的代码仅给镶嵌后的影像指定了重采样方法,但未将所有波段统一到同一分辨率,混合分辨率的影像加载时就会出现显示异常。另外,重采样操作应该在镶嵌前完成,否则单张影像分辨率不一致,镶嵌结果本身就存在问题。

修改后的完整代码

/**
 * Function to mask clouds using the Sentinel-2 QA band
 * @param {ee.Image} image Sentinel-2 image
 * @return {ee.Image} cloud masked Sentinel-2 image
 */
function maskS2clouds(image) {
  var qa = image.select('QA60');

  // Bits 10 and 11 are clouds and cirrus, respectively.
  var cloudBitMask = 1 << 10;
  var cirrusBitMask = 1 << 11;

  // Both flags should be set to zero, indicating clear conditions.
  var mask = qa.bitwiseAnd(cloudBitMask).eq(0)
      .and(qa.bitwiseAnd(cirrusBitMask).eq(0));

  return image.updateMask(mask).divide(10000);
}

var aoi = 
    /* color: #98ff00 */
    /* displayProperties: [
      {
        "type": "rectangle"
      }
    ] */
    ee.Geometry.Polygon(
        [[[-82.14606816037433, 42.900619674967885],
          [-82.14606816037433, 42.48476153151389],
          [-81.48963505490558, 42.48476153151389],
          [-81.48963505490558, 42.900619674967885]]], null, false);
   
// Show aoi on map
Map.addLayer(aoi, {color: 'yellow'});

// 统一影像分辨率到10m(以B2波段投影为基准),同时裁剪到AOI
function resampleAndClip(image) {
  var targetProjection = image.select('B2').projection();
  return image.resample('bicubic').reproject(targetProjection).clip(aoi);
}

// 获取指定日期范围的Sentinel-2影像
function getSentinel2WithinDateRange(start,end,aoi){
  var sentinel2 = ee.ImageCollection('COPERNICUS/S2_SR_HARMONIZED')
                    .filterBounds(aoi)
                    .filterDate(start, end)
                    .filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 20))
                    .map(maskS2clouds)
                    .map(resampleAndClip); // 替换原裁剪函数,先重采样再裁剪
                    
  return sentinel2.mosaic();
}

var date_start = '2024-05-29'
var date_end = '2024-06-30'

var collections = getSentinel2WithinDateRange(date_start, date_end,aoi)
print('Collection info', collections.getInfo())

var visualization = {
  min: 0.0,
  max: 0.3,
  bands: ['B4', 'B3', 'B2'],
};

Map.centerObject(aoi)

Map.addLayer(collections, visualization, 'RGB');

// 若需调整到其他分辨率(比如20m),可启用以下代码
// var resampledTo20m = collections.resample('bicubic').reproject(collections.select('B5').projection());
// Map.addLayer(resampledTo20m, visualization, 'RGB 20m');

关键改动说明

  • 替换原clipping函数为resampleAndClip:先给影像设置双三次重采样方法,再将所有波段统一到10m分辨率(Sentinel-2最高分辨率),最后裁剪到AOI。
  • 影像集合处理链中替换map(clipping)为map(resampleAndClip):确保每一张参与镶嵌的影像都已完成分辨率统一,避免镶嵌后出现分辨率混乱。
  • 删除原代码中对镶嵌后影像的单独重采样操作:因为统一分辨率的步骤已在镶嵌前完成,直接使用处理好的影像即可。

可选调整

如果需要重采样到其他分辨率,只需修改targetProjection对应的波段:

  • 20m分辨率:使用image.select('B5').projection()
  • 60m分辨率:使用image.select('B1').projection()

内容的提问来源于stack exchange,提问作者hamid mohebzadeh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 12:54:55