5

I'd like to count the number of pixels with the value=[0,100,234] in an image.

Without using loops over the image, could you please propose a method of how to determine the number of these particular pixels?

brad
  • 930
  • 9
  • 22
Kristan
  • 387
  • 6
  • 19
  • 1
    Maybe [this](http://stackoverflow.com/questions/19623132/calculating-the-number-of-blue-pixels-in-a-picture) answer helps. – NAmorim Feb 15 '17 at 16:53
  • Thanks, I checked this one already, but could not make it work with countNonzero function... – Kristan Feb 15 '17 at 16:56
  • 1
    _could not make it work with countNonZero_ ... why? What's the problem? @Namorim link is correct. You can alternatively get the length of the list returned by (something like): `np.where((image == [0, 100, 234]).all(axis = 2))` – Miki Feb 15 '17 at 17:21
  • SOrry, I dont get it...how to you proceed then to count the pixels? – Kristan Feb 15 '17 at 17:35
  • 1
    Use `np.count_nonzero` replacing `np.where`. – Divakar Feb 15 '17 at 17:53
  • This is one of those (many) cases in which telling us what you've tried and why those approaches didn't work for you would really be helpful. It saves a lot of people suggesting things that you've already been through and makes it easier to address the real problem. – beaker Feb 16 '17 at 16:06
  • The solution suggested by @Miki and Divakar works well! Using np.count_nonzero((img == [0, 100, 234]).all(axis = 2)) gives the number of pixels with the given colors on an image image. – Kristan Feb 17 '17 at 11:53
  • Glad you make it work. Posted an answer (as Community Wiki since it's been contributed also by Divakar), so you can accept it and we can consider this question closed – Miki Feb 17 '17 at 12:06

1 Answers1

9

To count the number of pixels with the value = [0,100,234] you can use:

np.count_nonzero((img == [0, 100, 234]).all(axis = 2))
Miki
  • 40,887
  • 13
  • 123
  • 202