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?

8
  • 1
    Maybe this answer helps. Commented Feb 15, 2017 at 16:53
  • Thanks, I checked this one already, but could not make it work with countNonzero function... Commented Feb 15, 2017 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)) Commented Feb 15, 2017 at 17:21
  • SOrry, I dont get it...how to you proceed then to count the pixels? Commented Feb 15, 2017 at 17:35
  • 1
    Use np.count_nonzero replacing np.where. Commented Feb 15, 2017 at 17:53

1 Answer 1

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))
Sign up to request clarification or add additional context in comments.

1 Comment

np.count_nonzero(img == value) can be used in the grayscale case

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.