我正在计算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)));
然后,我计算了我的统计数据,最小值,最大值,平均值,中位数,sd。因此,从我的输出中,我得到了一段时间内每个像素的NDVI和NDWI指数的最小值、最大值、平均值、中值、sd。
// 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();
然而,我注意到一些由于云而留下的异常值。在计算统计数据之前,我想删除NDVI和NDWI低于第5百分位和高于第95百分位的值。为了确保我的统计数据不会被异常值所偏置。
谢谢
我建议使用减缩器(即ee。减缩器。百分位数([95]((计算第95个和第5个百分位数,然后屏蔽图像:
results = results.updateMask(results.gt(bottomPercentile).and(results.lt(topPercentile)));