I'm trying to figure out why I get different answers when calculating the total number of pixels in an image in different ways.

  1. Calculate a histogram on all possible values, then add together all of the categories to get the total number of pixels. This won't count NA's.
  2. Use the ee.Reducer.count() function. This also doesn't count NA's, so I would expect this to give the same answer as number 1.
  3. Use unmask().ee.Reducer.count() function. This DOES count NA's, so it will be larger than number 2 if there are NA's.
  4. print the image, look at the dimensions, and multiply the 2 dimensions to get the number of pixels. This should count NA's and I would expect it to give the same answer as number 3.

Why do these methods give different counts of the total number of pixels in an image?

Sample code: https://code.earthengine.google.com/2c4767f13001604254fa7d431af14b86

1 Answer 1


I don't have all the details to explain all of the differences you are seeing, but I can say that (1) is substantially different from (2) and (3) because the reduceRegion call for (1) has a scale specified and the other two do not. I don't recall exactly how the default scale is determined, to compare the (2) and (3) cases with (4). But the documentation does say that it's a good idea to always explicitly specify a scale. Once you do, the exact pixel resolution of the image no longer affects what you're measuring, only the area it covers.

Also, (1) is not an integer apparently because of weighting of partially masked pixels, in that adding .unweighted() to the histogram reducer gives an integer consistent with the count(). The partially masked pixels will come from the original image pixels not exactly fitting up to the edges of the pixels of the reduction.

  • Interestingly, adding the .unweighted() option to the reducer makes the estimate from the histogram even further from the other estimates! It decreases the number of pixels calculated from 4801.474509803917 with the weighting to 4800 without.
    – filups21
    Commented May 23, 2019 at 19:02
  • 1
    @filups21 You need to set the same scale before comparing different reductions, as I said in the first paragraph. They all give 4800 under the modifications I mentioned with a scale parameter of 1000, or 5620 with the default scale.
    – Kevin Reid
    Commented May 23, 2019 at 19:11

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.