简介

本次的归一化教程,优化了数据去云,预处理等过程,同事将landsat 5/7/8集合分别进行了数据整合,也就是原始波段的处理,从而我们可以调用1985-至今任何一个时期的影像进行归一化处理。具体的原文介绍 请看原始的博客

function maskL457sr(image) {
  // 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);
}


function rename57 (image) {
return image.select(['SR_B1','SR_B2','SR_B3','SR_B4',"SR_B5","SR_B7"],['blue', 'green', 'red','nir',"swir1","swir2"]);

}
function rename89 (image) {
return image.select(['SR_B2','SR_B3','SR_B4',"SR_B5","SR_B6","SR_B7"],['blue', 'green', 'red','nir',"swir1","swir2"]);

}



//其他指数可以参照一下函数
//NDVI  
function NDVI(image) {  
  return image.addBands(  
    image.normalizedDifference(["nir", "red"])  
         .rename("NDVI"));  
}  
  
//NDWI  
function NDWI(image) {  
  return image.addBands(  
    image.normalizedDifference(["red", "swir1"])  
         .rename("NDWI"));  
}  
    
//NDBI  
function NDBI(image) {  
  return image.addBands(  
    image.normalizedDifference(["swir2", "swir1"])  
         .rename("NDBI"));  
} 
     
function EVI(image) { 
var evi = image.expression(  
    '2.5 * ((NIR - RED) / (NIR + 6 * RED - 7.5 * BLUE + 1))', {  
      'NIR': image.select('nir'),  
      'RED': image.select('red'),  
      'BLUE': image.select('blue')  
}); 
return image.addBands(evi.rename('EVI'));
}
;



// Map the function over one year of data.
var collection1 = ee.ImageCollection('LANDSAT/LT05/C02/T1_L2').filterBounds(geometry)
                     .filterDate('1985-01-01', '2012-01-01')
                     .map(maskL457sr).map(rename57).map(NDVI).map(NDWI).map(NDBI).map(EVI);

var collection2 = ee.ImageCollection('LANDSAT/LE07/C02/T1_L2').filterBounds(geometry)
                     .filterDate('1985-01-01', '2012-01-01')
                     .map(maskL457sr).map(rename57).map(NDVI).map(NDWI).map(NDBI).map(EVI);

var collection3 = ee.ImageCollection('LANDSAT/LC08/C02/T1_L2').filterBounds(geometry)
                     .filterDate('2013-01-01', '2024-01-01')
                     .map(maskL8sr).map(rename89).map(NDVI).map(NDWI).map(NDBI).map(EVI);

//如果需要可以进行融合
var col = collection1.merge(collection2).merge(collection3)

var image = col.first()

var scol_clip = image.clip(geometry)


function normalization(image,region,scale){
var mean_std = image.reduceRegion({
  reducer: ee.Reducer.mean()
            .combine(ee.Reducer.stdDev(),null, true),
  geometry: region,
  scale: scale,
  maxPixels: 10e9,
  // tileScale: 16
}); 
// use unit scale to normalize the pixel values
var unitScale = ee.ImageCollection.fromImages(
  image.bandNames().map(function(name){
    name = ee.String(name);
    var band = image.select(name);
    var mean=ee.Number(mean_std.get(name.cat('_mean')));
    var std=ee.Number(mean_std.get(name.cat('_stdDev')));
    var max=mean.add(std.multiply(3));
    var min=mean.subtract(std.multiply(3));
    var band1=ee.Image(min).multiply(band.lt(min)).add(ee.Image(max).multiply(band.gt(max)))
                        .add(band.multiply(ee.Image(1).subtract(band.lt(min)).subtract(band.gt(max))));
    var result_band=band1.subtract(min).divide(max.subtract(min));
    return result_band;
})).toBands().rename(image.bandNames());
  return unitScale;
}


//归一化前的结果
var before_chart=ui.Chart.image.histogram(scol_clip.select(["nir","EVI"]), geometry, 1000);
print(before_chart);
//让影像进行归一化函数处理后的结果
var normal_image=normalization(scol_clip,geometry,1000)
print(normal_image)
//归一化后的结果
var after_chart=ui.Chart.image.histogram(normal_image.select(["nir","EVI"]), geometry, 1000)
print(after_chart)


print("image",image);
Map.centerObject(geometry, 3);

结果

GEE 更新和优化:利用GEE在线处理1985-2024年NDVI、EVI、SAVI、NDMI等指数归一化教程!(Landsat C02 数据)_数据库