count number of black pixels in an image in Python with OpenCV

Aurélie JEAN

I have the following test code in Python to read, threshold and display an image:

import cv2
import numpy as np 
from matplotlib import pyplot as plt

# read image
img = cv2.imread('slice-309.png',0)
ret,thresh = cv2.threshold(img,0,230, cv2.THRESH_BINARY)
height, width = img.shape
print "height and width : ",height, width
size = img.size
print "size of the image in number of pixels", size 

# plot the binary image
imgplot = plt.imshow(img, 'gray')
plt.show()

I would like to count the number of pixels within the image with a certain label, for instance black. How can I do that ? I looked at tutorials of OpenCV but did not find any help :-(

Thanks!

Rick Smith

For black images you get the total number of pixels (rows*cols) and then subtract it from the result you get from cv2.countNonZero(mat).

For other values, you can create a mask using cv2.inRange() to return a binary mask showing all the locations of the color/label/value you want and then use cv2.countNonZero to count how many of them there are.

UPDATE (Per Miki's comment):

When trying to find the count of elements with a particular value, Python allows you to skip the cv2.inRange() call and just do:

cv2.countNonZero(img == scalar_value)  

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Count the black pixels using OpenCV

From Dev

Using OpenCV Python, How would you make all black pixels transparent, and then overlay it over original image

From Dev

opencv python copy mask region (black or white pixels) onto a BGR image region

From Dev

Applying original image pixels wherever the thresholded image is black in OpenCV/numpy?

From Dev

Divide the image into 4x4 blocks and Counts the number of Black/White Pixels in Python

From Dev

How to count the number of black and white pixels (linux, imagemagik, etc)

From Dev

Matlab: How can I find the number of black pixels in a color image?

From Dev

Get total number of the black and white pixels in bitmap image separately

From Dev

Matlab: How can I find the number of black pixels in a color image?

From Dev

how to Count the number of non zero pixels of the canny image in my python program

From Dev

Count black pixels within a circle

From Dev

Plot number of black pixels per row using python

From Dev

Python - Remove Black Pixels originatin from the border of an image

From Dev

Python - Remove(convert to white) non black pixels of an image

From Dev

OpenCV/Python: MedianBlur filter produces a black image

From Dev

Lower the resolution of an image to a specific number of pixels in Python

From Dev

Count the number of pixels by color from an image loaded into a numpy array

From Dev

Using Opencv. How can i increment the size of an image filling the new pixels with black(0)?

From Dev

How can i count number of black spaces in scikit image or mahotas?

From Dev

OpenCV/python: How to change image pixels' values using a formula?

From Dev

How to crop a region of small pixels in an image using OpenCv in Python

From Dev

Using opencv / Numpy to find white pixels in a color image using python

From Dev

Image pixels values after imwrite and/or imread in python opencv

From Dev

Count number of spaces ' ' in N pixels

From Dev

Remove black header section of image using Python OpenCV

From Dev

Detecting location of translucent black rectangluar area in image matrix Python OpenCV

From Dev

How to create an image with black and white pixels?

From Dev

How to remove black pixels inside the character image

From Dev

masking a certain region of the image with black pixels

Related Related

  1. 1

    Count the black pixels using OpenCV

  2. 2

    Using OpenCV Python, How would you make all black pixels transparent, and then overlay it over original image

  3. 3

    opencv python copy mask region (black or white pixels) onto a BGR image region

  4. 4

    Applying original image pixels wherever the thresholded image is black in OpenCV/numpy?

  5. 5

    Divide the image into 4x4 blocks and Counts the number of Black/White Pixels in Python

  6. 6

    How to count the number of black and white pixels (linux, imagemagik, etc)

  7. 7

    Matlab: How can I find the number of black pixels in a color image?

  8. 8

    Get total number of the black and white pixels in bitmap image separately

  9. 9

    Matlab: How can I find the number of black pixels in a color image?

  10. 10

    how to Count the number of non zero pixels of the canny image in my python program

  11. 11

    Count black pixels within a circle

  12. 12

    Plot number of black pixels per row using python

  13. 13

    Python - Remove Black Pixels originatin from the border of an image

  14. 14

    Python - Remove(convert to white) non black pixels of an image

  15. 15

    OpenCV/Python: MedianBlur filter produces a black image

  16. 16

    Lower the resolution of an image to a specific number of pixels in Python

  17. 17

    Count the number of pixels by color from an image loaded into a numpy array

  18. 18

    Using Opencv. How can i increment the size of an image filling the new pixels with black(0)?

  19. 19

    How can i count number of black spaces in scikit image or mahotas?

  20. 20

    OpenCV/python: How to change image pixels' values using a formula?

  21. 21

    How to crop a region of small pixels in an image using OpenCv in Python

  22. 22

    Using opencv / Numpy to find white pixels in a color image using python

  23. 23

    Image pixels values after imwrite and/or imread in python opencv

  24. 24

    Count number of spaces ' ' in N pixels

  25. 25

    Remove black header section of image using Python OpenCV

  26. 26

    Detecting location of translucent black rectangluar area in image matrix Python OpenCV

  27. 27

    How to create an image with black and white pixels?

  28. 28

    How to remove black pixels inside the character image

  29. 29

    masking a certain region of the image with black pixels

HotTag

Archive