Google Earth Engine:从 S2 NDVI 图像集合中删除值(5 和 95 个百分点)

Google Earth Engine : Remove value from S2 NDVI image collection (5 and 95 percentile)

我正在计算 Sentinel2 上 NDVI 图像收集的统计数据。

我收集了 S2_SR 在定义的时间段和特定区域的图像集,然后移除了云层。

        var S2 = ee.ImageCollection('COPERNICUS/S2_SR')
    
    //filter start and end date
    .filterDate('2019-03-01', '2020-03-31')
    
    //filter according to drawn boundary
    .filterBounds(ROI);
    
    var palettes = require('users/gena/packages:palettes');
    var palette = palettes.colorbrewer.Spectral[8];
    
    // Function to mask cloud from built-in quality band
    // information on cloud
    var computeQAbits = function(image, start, end, newName) {
        var pattern = 0;
    
        for (var i=start; i<=end; i++) {
            pattern += Math.pow(2, i);
        }
    
        return image.select([0], [newName]).bitwiseAnd(pattern).rightShift(start);
    };
    
    var sentinel2 = function(image) {
    
      var cloud_mask = image.select("QA60");
      var opaque = computeQAbits(cloud_mask, 10, 10, "opaque");
      var cirrus = computeQAbits(cloud_mask, 11, 11, "cirrus");
      var mask = opaque.or(cirrus);
    
      return image.updateMask(mask.not());
    }

Then I've calculated NDVI and NDWI for each date;

       var addNDVI = function(image) {
          var ndvi = image.normalizedDifference(['B5', 'B4']).multiply(10000).rename('NDVI');
        
          return image.addBands(ndvi);
        };
        
        var addNDWI = function(image) {
          var ndwi = image.normalizedDifference(['B3', 'B8']).multiply(10000).rename('NDWI');
          return image.addBands(ndwi);
        };
        // Add NDVI band to image collection
        var S2 = S2.map(addNDVI);
        var S2 = S2.map(addNDWI);
        var NDVI = S2.select(['NDVI']);
        var NDWI = S2.select(['NDWI']);
var S2 = S2.map(addNDVI);
var S2 = S2.map(addNDWI);
var NDVI = S2.select(['NDVI']);
var NDWI = S2.select(['NDWI']);

var P_95_NDVI = NDVI.reduce(ee.Reducer.percentile([95]));
var P_05_NDVI = NDVI.reduce(ee.Reducer.percentile([5]));

NDVI_P = NDVI.updateMask(NDVI.gt(P_05_NDVI).and(NDVI.lt(P_95_NDVI)));


var P_95_NDWI = NDWI.reduce(ee.Reducer.percentile([95]));
var P_05_NDWI = NDWI.reduce(ee.Reducer.percentile([5]));

NDWI_P = NDWI.updateMask(NDWI.gt(P_05_NDWI).and(NDWI.lt(P_95_NDWI)));
    

然后,我计算了我的统计数据,最小值、最大值、平均值、中值、标准差。 因此,从我的输出中,我得到了我一段时间内每个像素的 NDVI 和 NDWI 指数的最小值、最大值、平均值、中值、标准差。

 // Add indices bands to image collection
    
    var reducer1 = ee.Reducer.mean();
    
    var reducers = reducer1.combine({reducer2: ee.Reducer.median(), sharedInputs: true})
                           .combine({reducer2: ee.Reducer.max(), sharedInputs: true})
                           .combine({reducer2: ee.Reducer.min(), sharedInputs: true})
                           .combine({reducer2: ee.Reducer.stdDev(), sharedInputs: true});
    
    
    var results_NDVI = NDVI.reduce(reducers);
    var results_NDWI = NDWI.reduce(reducers);
    
    var results = results_NDVI.addBands(results_NDWI);
    
    var results_integer = results.int16();

但是,我注意到由于云的缘故,一些剩余的离群值。我想在计算我的统计数据之前,删除低于第 5 个百分位和高于第 95 个百分位的 NDVI 和 NDWI 值。确保我的统计数据不会因异常值而产生偏差。

谢谢

我建议使用缩减器(即 ee.Reducer.percentile([95]))计算第 95 和第 5 个百分位数,然后屏蔽图像:

results = results.updateMask(results.gt(bottomPercentile).and(results.lt(topPercentile)));