I have trouble handling a dataset (ArcticDEM) on google earth engine. The images in the ArcticDEM dataset come already masked out, therefore an imagecollection sometimes contains images which have very limited pixels, where many pixels have the value of "masked". I want to create a script, which would automatically remove an image from an imagecollection, if it contains less than a certain amount of unmasked pixels within the region of interest.

I also tried instead of removing images from imagecollection, to append the "good" images to a new imagecollection, however that didn't work for me either.

Does anyone know how to I should proceed? The image below is an example of an image containing too many masked pixels. Script: https://code.earthengine.google.com/b629febdaa1448b2a6847233526348b7

enter image description here

2 Answers 2


You need a function that counts the number of masked pixels within a ROI such as this one that was prepared for identifying cloud cover in Landsat images within a ROI: How to count the number of unmasked pixels per image in collection (Earth Engine)

Since your collection doesn't come with a mask band, you must create your own using the unmask() function.

var dem_new = dem.map(function(image){ 
  // set masked pixels to -999. pixels that are not -999 are "ok"
  var maskband = dem.unmask(-999).neq(-999).rename('mask')
  return image.addBands(maskband)

as @korndog said you could unmask the masked pixels, then reduce an histogram that counts unmasked pixels and finally set that result as a property so you can filter the collection. Following with your code:

var unmaskedCol = collection.map(function(img){
  // unmask each image in the collection
  var unmasked = img.unmask(-99).eq(-99);
  // reduce histogram on each image and set the keys as properties (key '1' will be masked pixels)
  var rR = unmasked.reduceRegion({reducer: ee.Reducer.frequencyHistogram(),
                               geometry: img.geometry(),
                               scale: 100,
                               bestEffort: true});
  var newProperties = ee.Dictionary(rR.get('elevation'));
  return img.set(newProperties)

//collection of dem without masked pixels
var colNotMasked = ee.ImageCollection(unmaskedCol).filter(ee.Filter.notNull(['1']).not())
print(colNotMasked, 'notmasked');
//collection of dem that have less than 200 pixeles masked
var colless200 = ee.ImageCollection(unmaskedCol).filterMetadata('1', 'less_than', 200);
print(colless200, 'colless200')

Your Answer

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

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