I have created many rasters in Google Earth Engine that represent the NDVI. I want to apply statistics on them. I want to count how many pixels I have in every raster, and then I want to count how many of the pixels have values higher than 0.3 and 0.5.

This is the code I wrote, I have tried to use the ee.Reducer.count(), but I get error messege- ''Invalid argument specified for ee.List(): EPSG:4326'' and also I don't know how to make it count values that are bigger than 0.5.

var agri_7=agri_1;
var dataset = ee.ImageCollection('MODIS/006/MOD13A1')
.filter(ee.Filter.date('2000-03-01', '2002-03-31'))
// filter the image collection to contain only images from March
var onlyMarch = dataset.filter(ee.Filter.calendarRange(3, 3,'month'));
// make a composite (mean image) of the images of March and clip to the geometry
var meanImage = onlyMarch.mean().clip(agri_7).divide(10000);

var mean20002002 = meanImage.reduceRegion({
  reducer: ee.Reducer.mean(),
  geometry: agri_7,

//var ndvi = dataset.select('NDVI')
var ndviParams = {min: 0, max: 1, palette: [
    'FFFFFF', 'CE7E45', 'DF923D', 'F1B555', 'FCD163', '99B718', '74A901',
    '66A000', '529400', '3E8601', '207401', '056201', '004C00', '023B01',
    '012E01', '011D01', '011301'

Map.centerObject(agri_7, 9);
print(mean20002002,'mean NDVI 2000-2002');

//count the number of total pixels
var c = meanImage.reduceRegion({
  reducer: ee.Reducer.count(),
  geometry: agri_7,
  crs: 'EPSG:4326',
  crsTransform: 'EPSG:4326',

print(c,'Total Pixels 2000 2002');

You are getting the error because you have to specify a list for crsTransform instead of a string. However, as the MODIS images are on a 500m scale, I would recommend to set the scale at 500 (meter) and you reducer output should do fine.

You can make an image of NDVI greater than 0.3 and 0.5 using ee.Image().gt() and then apply selfMask to mask all pixels lower than those ranges. These pixels then won't be 'counted' by the reducer count.

// Make an image of NDVI > 0.3 and NDVI > 0.5 and mask low pixels
var gt03 = meanImage.gt(0.3).selfMask().rename('NDVI_gt03');
var gt05 = meanImage.gt(0.5).selfMask().rename('NDVI_gt05');

//count the number of total pixels
var c = gt03.addBands(gt05).reduceRegion({
  reducer: ee.Reducer.count(),
  geometry: agri_7,
  crs: 'EPSG:4326',
  scale: 500,
// print the dictionary output
print(c,'Total Pixels gt 0.3 and 0.5');

Note that I made a sample region somewhere in the US as that was not provided in your code.

Link code

  • Thank you for your answer, what does : " gt03.addNamds(gt05)" means (in the part you count the total number of pixels), does it mean you sum the pixels that are greater than 0.3 and 0.5? because then it may count it double, no? – Reut Jun 12 '19 at 15:26
  • It means I add both bands as seperately to one image. In that way, you only have to run reduceRegion() just once (which is faster). – Kuik Jun 12 '19 at 15:36
  • so if I want to count only the pixels that their value > than 0.5, and not the ones that their values is between 0.3 to 0.5, then I'll write "c=gt05(reduceRegion({......." ? – Reut Jun 12 '19 at 15:52
  • That's correct. – Kuik Jun 12 '19 at 21:21

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.