0

I have a image collection which holds multiple images of the same area. I can apply mask individually to those images but how do I apply a mask to all the images in the collection. Say the mask i have to apply is to filter out all the pixels with value more than a given value. Since i have multiple images for the same location, I want to 'and' the logic so that i get a single image with pixels displayed which satisfy the condition for all the images. Here is some code i wrote which works for fine when i have single image:

    Map.setOptions('SATELLITE')
    var image = ee.ImageCollection("imagesForIrvineCalifornia");


    var min = image.aggregate_min('min');
    var max = image.aggregate_max('max');


    var VIS_OPTIONS = {
      'b1': {
        description: 'b1' +
                     'b1 descripion',
        visParams1: {min:min.getInfo(), max:max.getInfo(), palette: ['DCF5E9', 'BDDEC9', '9DC7AA', '82B38E','689E75', '4E8A5B', '387847'], bands: ['b1']}
      }
    };

    var maskingFunc = function(val){
      return function(i){
        var band = i.select('b1');
        var mask = band.lte(val);
        return i.updateMask(mask);
      }
    }

    var maskedImage = image;
    var cutoffVal=3000 ;
    maskedImage = image.map(maskingFunc(cutoffVal));

    Map.addLayer(maskedImage.select('b1'), VIS_OPTIONS['b1'].visParams1, '(b1)', true);
    Map.centerObject(Map.layers().get(0).get('eeObject'));
2
  • 1
    I have the code ready to give you a mask with pixels that fulfill the condition in all images (0: at least 1 pixels does not fulfill, 1: all pixels fulfill), but how are you planning to get 1 value out of many images? a mean value? or you want one image with as many bands as images in the collection? Commented Jul 13, 2018 at 11:03
  • Thanks. I do not want the values from individual images in this case, just the visualization showing which areas satisfy the condition in all the images from the image collection.
    – umbersar
    Commented Jul 14, 2018 at 7:00

1 Answer 1

0

As I don't have your image collection, I tested it with a fantasy collection made of randomly generated images. Worked for me.

EDIT: This code only works when images inside the collection are overlapped.

Map.setOptions('SATELLITE')
var image = ee.ImageCollection("imagesForIrvineCalifornia");

var min = image.aggregate_min('min');
var max = image.aggregate_max('max');


var VIS_OPTIONS = {
  'b1': {
    description: 'b1' +
                 'b1 descripion',
    visParams1: {min:min.getInfo(), max:max.getInfo(), palette: ['DCF5E9', 'BDDEC9', '9DC7AA', '82B38E','689E75', '4E8A5B', '387847'], bands: ['b1']}
  }
};

var maskingFunc = function(collection, val, band){
  // convert col to list
  var collist = collection.toList(collection.size())
  // get first img of collection and compute condition
  var ini = ee.Image(collist.get(0)).lte(val)
  // get collection without first image
  var rest = ee.ImageCollection(collist.slice(1))

  // function to iterate over the 'rest' collection to 'and' logic
  var wrap = function(i, first){
    var f = ee.Image(first).select(band)
    var image = i.select(band);
    var mask = image.lte(val);
    return mask.and(f)
  }

  // iterate over the collection and return a single image
  return ee.Image(collection.iterate(wrap, ini))
}

var cutoffVal = 3000;
var maskedImage = maskingFunc(image, cutoffVal, 'b1');

Map.addLayer(maskedImage.select('b1'), {min:0, max:1}, '(b1)', true);
Map.centerObject(Map.layers().get(0).get('eeObject'));
8
  • Thanks for the answer Rodrigo. I will test soon and Mark it as accepted soon.
    – umbersar
    Commented Jul 17, 2018 at 4:28
  • I tested it. Unfortunately it did not work for me. Do not know what i am missing here.
    – umbersar
    Commented Jul 18, 2018 at 0:46
  • @umbersar, this is how I tested it: code.earthengine.google.com/7df2fa0bb768f8c4169043daa9835348 but I think I know the error. I left your VIS_OPTIONS which makes no sense because what you want (and get with the code) is a binary mask (0 or 1). I changed the code, see what happens now. Commented Jul 18, 2018 at 11:51
  • Thanks for your help but the script is not working. I do not see anything on the map.
    – umbersar
    Commented Jul 18, 2018 at 23:20
  • You've have to share your imagecollection then. Have you seen the code of the link? Commented Jul 19, 2018 at 0:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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