Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to count the number of pixels with a certain pixel value in python opencv?

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?

like image 791
Kristan Avatar asked Feb 15 '17 16:02

Kristan


People also ask

How do I get the pixel value of an image in OpenCV?

Figure 5: In OpenCV, pixels are accessed by their (x, y)-coordinates. The origin, (0, 0), is located at the top-left of the image. OpenCV images are zero-indexed, where the x-values go left-to-right (column number) and y-values go top-to-bottom (row number). Here, we have the letter “I” on a piece of graph paper.

How do I know how many pixels a picture is?

The width and height of the image in pixels. To find the total number of pixels, multiply the width and height together. In this case, 4700 pixels x 3133 pixels = 14,725,100 pixels. That's a lot of pixels.


1 Answers

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))
like image 152
Miki Avatar answered Oct 16 '22 15:10

Miki