classy
classy

Reputation: 11

Counting a certain pixel value in Python

How can I count the number of pixels in Python when Gray value = 1 and gray value = 100 in an RGB image?

Upvotes: 1

Views: 434

Answers (1)

queeg
queeg

Reputation: 9473

A pixel is somewhere between white and black (and shades of gray are inbetween) if the three values for R, G and B are equal. Assuming 8 bit values:

(0,0,0) -> black (80,80,80) -> dark gray (160,160,160) -> light gray (255,255,255) -> white

I think you get the point. Now all you need to do is to iterate over the picture, line for line and within that pixel for pixel and evaluate the color to eventually count it.

There may be functions creating histograms that are optimized in this direction already.

Upvotes: 1

Related Questions