I have code that calculate NDVI for Landsat 5,7 and 8. I would like to know how can I "filter" my image collection so I have only images that have a certain number of pixels. For that I thought maybe I can first count the number of pixels in the image and then use gt but I'm not sure how to put it inside the imageCollection.

To do that I think I need to take my imageCollection and change it into a list and then iterate through this list- for every image to count the numbers of pixels using something like this-

//count the number of total pixels
var countpixels = image1.reduceRegion({
  reducer: ee.Reducer.count(),
  geometry: table,
  crs: 'EPSG:4326',
  scale: 30,

and then filter according to the number of pixels.

My problems are-

  1. I have around 250 images in my imageCollection. I don't want to create list and to type by myself all the numbers like this-
var listOfNumbers = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19];
  1. How can I filter image by number of pixels so I don't get very small rasters?

My end goal is to calculate statistic for my polygon for many years and for that I can't have the small rasters or the ones that look like cheese with many holes due to the cloud mask.

Here is one of my codes-

 * Function to mask clouds based on the pixel_qa band of Landsat SR data.
 * @param {ee.Image} image Input Landsat SR image
 * @return {ee.Image} Cloudmasked Landsat image
var cloudMaskL457 = function(image) {
  var qa = image.select('pixel_qa');
  // If the cloud bit (5) is set and the cloud confidence (7) is high
  // or the cloud shadow bit is set (3), then it's a bad pixel.
  var cloud = qa.bitwiseAnd(1 << 5)
                  .and(qa.bitwiseAnd(1 << 7))
                  .or(qa.bitwiseAnd(1 << 3));
  // Remove edge pixels that don't occur in all bands
  var mask2 = image.mask().reduce(ee.Reducer.min());
  return image.updateMask(cloud.not()).updateMask(mask2).divide(10000)
  .copyProperties(image, ['system:time_start']);

//Create LANDSAT7 dataset

var dataset = ee.ImageCollection('LANDSAT/LE07/C01/T1_SR')
                  .filterDate('1999-01-01', '2013-04-29')

//RGB visualization

var visParams = {
  bands: ['B3', 'B2', 'B1'],
  min: 0,
  max: 1,
  gamma: 1.4,

//clip the dataset according to the geometry
var clippedCol=dataset.map(function(im){ 
  return im.clip(geometry);

// Get the number of images.
var count = dataset.size();
print('Count: ',count);

//function to calculate NDWI in LANDSAT7
var addNDVI = function(image) {
  var NDVI = image.normalizedDifference(['B4', 'B3'])
  return image.addBands(NDVI);


//NDWI to the clipped image collection
var withNDVI = clippedCol.map(addNDVI).select('NDVI');

var NDVIcolor = {
  min: -1,
  palette: ['FFFFFF', 'CE7E45', 'DF923D', 'F1B555', 'FCD163', '99B718', '74A901',
    '66A000', '529400', '3E8601', '207401', '056201', '004C00', '023B01',
    '012E01', '011D01', '011301'],
print(ui.Chart.image.series(withNDVI, geometry, ee.Reducer.mean(), 30));
print(ui.Chart.image.series(withNDVI, geometry, ee.Reducer.max(), 30));
print(ui.Chart.image.series(withNDVI, geometry, ee.Reducer.min(), 30));
print(ui.Chart.image.series(withNDVI, geometry, ee.Reducer.stdDev(), 30));

1 Answer 1


You are on the right track;

The calculation you already created should be build into a function that maps over the entire image collection, when you add the result as an image property you can then filter on it:

var ndviWithCount = withNDVI.map(function(image){
  var countpixels = ee.Number(image.reduceRegion({
  reducer: ee.Reducer.count(),
  geometry: geometry,
  crs: 'EPSG:4326',
  scale: 30,

  return image.set('count', countpixels)

print(ndviWithCount, 'ndviWithCount')

//filter between a range
var filter = ndviWithCount.filter(ee.Filter.rangeContains(
          'count', 0, 10))
print(filter, 'filtered')

var max = ndviWithCount.reduceColumns(ee.Reducer.max(),  ["count"])

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.