I am working on Sentinel -2 image collection over my study region in Google Earth Engine. I can calculate Mean NDVI for an image using ee.Reducer.mean() and mask pixels using update mask .gt(mean value number). Now I want to do this to each image of the collection. Since mean NDVI value of each image will be different I am facing difficulty to use .map function.I attempted this but I think I have got it all wrong and now I am stuck. How can I iterate it to each image in image collection?

    /////////////Sentinel-2 Burned Area Detection/////////////////////////////////////
    ////Get image collection with specified cloud percentage threshold.
    var s2= ee.ImageCollection("COPERNICUS/S2_SR").filter(ee.Filter.lt("CLOUDY_PIXEL_PERCENTAGE", 10));
    var admin2 = ee.FeatureCollection("FAO/GAUL_SIMPLIFIED_500m/2015/level2");
    var Bhopal = admin2.filter(ee.Filter.eq('ADM2_NAME', 'Bhopal'))
    var geometry = Bhopal.geometry()
    ////vis parameters.
    var vis = {bands: ['B4', 'B3', 'B2'], max: 2000, gamma: 1.5};
    ////print fire occurance.
    // print(ee.String('Fire incident occurred between ').cat(fire_start).cat(' and ').cat(fire_end));
    ////Define Study area
    var area = ee.FeatureCollection(geometry);
    var opacity = 0.5; // number [0-1]
    Map.centerObject(area, 6.5);
    ////Filter Dates for rabi season
    var fireImColA1 = ee.ImageCollection(s2.filterDate('2019-03-01', '2019-03-07')  //// Filter by dates.
    //// Creat a Cloud Mask.
    function maskS2sr(image) {
      var cloudBitMask = ee.Number(2).pow(10).int(); ////cloud band
      var cirrusBitMask = ee.Number(2).pow(11).int();//// cirrus band
      var qa = image.select('QA60'); //// Get the pixel QA band.
      var mask = qa.bitwiseAnd(cloudBitMask).eq(0)  ////  Flags set to zero for clear conditions.
          .and(qa.bitwiseAnd(cirrusBitMask).eq(0)); ////  Flags set to zero for clear conditions.
      return image.updateMask(mask) //// Return the masked image, scaled to TOA reflectance, without the QA bands.
          .copyProperties(image, ["system:time_start"]);
    //// Apply cloud mask to pre and post fire image collections
    var fire_CM_ImColA1 = fireImColA1.map(maskS2sr);
    ///Function  add NDVI band to image collection
    var addNDVI = function(image) {
      return image.addBands(image.normalizedDifference(['B8', 'B4']).rename('NDVI'));
    /////Add bands//////
    var fire_WM_ImColA1 = fire_CM_ImColA1.map(addNDVI);
    var mean_ndvi = function(image){
      var ndvi = image.select(['NDVI']);
    var ndvi_mean = ndvi.reduceRegion({
      reducer: ee.Reducer.mean(),
      geometry: geometry,
      scale: 20,
      maxPixels: 10e18,
    return ndvi_mean;
    var fire_ImColA1 = fire_WM_ImColA1.map(mean_ndvi);
    var mean_mask = function(image){
    var mask = image.select(['mean_ndvi']);
      return image.addBands(ee.Image(1).updateMask(mean_mask.gt(mean_mask)).rename('mean_mask'));
print (fire_ImColA1); 

This can be done simply with one function that you map to every image of the collection.

var apply_Mean_Mask = function(image){
    var ndvi = image.select('NDVI');
    var ndvi_mean = ndvi.reduceRegion({
        reducer: ee.Reducer.mean(),
        geometry: geometry,
        scale: 20,
        maxPixels: 10e18,
    return image.updateMask(ndvi.gt(ndvi_mean));

var maskedImageCollection = fire_WM_ImColA1.map(apply_Mean_Mask);

You were nearly there, just needed to add one step to your mean_ndvi function.

  • I tried your solution but its showing following error: ImageCollection (Error) Error in map(ID=20190301T052741_20190301T053122_T43QGF): Image.gt, argument 'image2': Invalid type. Expected type: Image<unknown bands>. Actual type: Dictionary<Float>. Actual value: {NDVI=0.45379570079906945}. But my input type is same as earlier then what is happing now
    – Monish
    Jul 28 at 15:04
  • You are right. I had 2 errors. one was the reducer name --it should be ee.Reducer.mean()-- the other was that I didn't extract the dictionary value to a number. Fixed it in the code above. Jul 28 at 15:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.