I wrote this script in GEE to calculate NDVI as well as the NDVI mean and standard deviation.
/**
* 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);
}
// This is the Sentinel 2 collection
var S2_collection = ee.ImageCollection("COPERNICUS/S2_SR_HARMONIZED")
.filterBounds(geometry)
.filterDate('2017-01-01', '2019-12-31')
.filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE',1))
.map(maskS2clouds)
.median();
var composite = S2_collection.clip(geometry);
var composite = composite.toFloat()
// Add map layers
Map.addLayer(composite , {bands: ['B11', 'B8', 'B4']}, "composite");
//Compute NDVI
var nir = S2_collection.select('B8');
var red = S2_collection.select('B4');
var ndvi = nir.subtract(red).divide(nir.add(red));
var ndvi = ndvi.clip(geometry);
// Add map layers
Map.addLayer(ndvi, {min: 0, max: 1, palette: ['black', 'yellow', 'green']}, 'continuous NDVI');
// Compute the mean and stdev of NDVI
var mean_ndvi = ndvi.reduceRegion({
reducer: ee.Reducer.mean(),
geometry: geometry,
scale: 10
});
var sd_ndvi = ndvi.reduceRegion({
reducer: ee.Reducer.stdDev(),
geometry: geometry,
scale: 10
});
print(mean_ndvi);
print(sd_ndvi);
I need to calculate the Gaussian variable of this index such as: ([NDVI - NDVI(mean)] / [NDVI_sd])
By using the reducer for mean and std dev, I create a dictionary , but if I want to visualize the mean and sd map results, I get the error:
Cannot add an object of type to the map
How can I calculate mean and sd for each pixel of my image and calculate this index and map it?
Update to my question:
I need the NDVI_gaussian value for a single image of 9th February 2019 over this geometry area:
0: [16.53793865386438,40.509788101097925]
1: [16.552594243921753,40.509788101097925]
2: [16.552594243921753,40.51740638148722]
3: [16.53793865386438,40.51740638148722]
4: [16.53793865386438,40.509788101097925]
and it should look like this:
And 2) I need the NDVI guassian formula in the February–November 2019 period, which should look like this:
And the index is defined as:
mean_ndvi
andsd_ndvi
, that's the mean/standard deviation of all pixels within your geometry, based on your slightly confusingly named median compositeS2_collection
. That's what you intended to do? Those are two numbers, so you cannot put them on a map (or at least, they're not very interesting to see on a map). Or did you want to calculate mean and standard deviation separately for every pixel in your geometry, based on your two year date range?