OpenCV - Counting the number of black and white pixels in the image
Last Updated :
04 Jan, 2023
In this article, we will discuss counting the number of black pixels and white pixels in the image using OpenCV and NumPy.
Prerequisites:
The image we are using for the demonstration is shown below:
To display OpenCV provides the imshow() method for displaying the image we have recently read.
Counting Pixels
NumPy provides a function sum() that returns the sum of all array elements in the NumPy array. This sum() function can be used to count the number of pixels on the basis of the required criteria.
Now, a bit of knowledge of the pixel patterns comes into the picture. As we know that each pixel in a coloured image ranges from [0-255] all-inclusive, the pixel value for the black color being 0 and that for the white color is 255. This gives us a certain fixed condition of differentiation for the black and white pixels respectively from the other color pixels respectively.
This condition can be written in the NumPy as:
number_of_white_pix = np.sum(img == 255) # extracting only white pixels
number_of_black_pix = np.sum(img == 0) # extracting only black pixels
The first line says to extract and count all pixels from cv2 image object "img" whose pixel value is 255 i.e. white pixels. Similarly, the second line says to extract and count all pixels from cv2 image object "img" whose pixel value is 0 i.e. black pixels.
The condition inside sum() extracts only those pixels from the image which follow that condition and assigns them value True (1) and rest of the pixels are assigned False (0). Thus, the sum of all True (1s) gives us the count of those pixels that are satisfying the given condition within the parenthesis.
Code: Python implementation to count the number of black and white pixels in the image using OpenCV
Python3
# importing libraries
import cv2
import numpy as np
# reading the image data from desired directory
img = cv2.imread("chess5.png")
cv2.imshow('Image',img)
# counting the number of pixels
number_of_white_pix = np.sum(img == 255)
number_of_black_pix = np.sum(img == 0)
print('Number of white pixels:', number_of_white_pix)
print('Number of black pixels:', number_of_black_pix)
Output:
Image to Process
Count of black and white pixels from the above image
Thus, we can infer from the above results that the image can be processed to retrieve the pixels of any desired color with the help of their color code or obtain the pixels based on any other condition as required.
References:
Similar Reads
Detecting low contrast images with OpenCV, scikit-image, and Python In this article, we are going to see how to detect low contrast images with OpenCV, scikit-image using Python A low contrast image has the minimal distinction between light and dark parts, making it difficult to tell where an object's boundary begins and the scene's background begins. For example Th
2 min read
White and black dot detection using OpenCV | Python Image processing using Python is one of the hottest topics in today's world. But image processing is a bit complex and beginners get bored in their first approach. So in this article, we have a very basic image processing python program to count black dots in white surface and white dots in the blac
4 min read
How to Display an OpenCV image in Python with Matplotlib? The OpenCV module is an open-source computer vision and machine learning software library. It is a huge open-source library for computer vision, machine learning, and image processing. OpenCV supports a wide variety of programming languages like Python, C++, Java, etc. It can process images and vide
2 min read
Count number of Faces using Python - OpenCV Prerequisites: Face detection using dlib and openCV In this article, we will use image processing to detect and count the number of faces. We are not supposed to get all the features of the face. Instead, the objective is to obtain the bounding box through some methods i.e. coordinates of the face i
3 min read
Displaying the coordinates of the points clicked on the image using Python-OpenCV OpenCV helps us to control and manage different types of mouse events and gives us the flexibility to operate them. There are many types of mouse events. These events can be displayed by running the following code segment : import cv2 [print(i) for i in dir(cv2) if 'EVENT' in i] Output : EVENT_FLAG_
3 min read
Convert OpenCV image to PIL image in Python OpenCV is a huge open-source library for computer vision, machine learning, and image processing. OpenCV supports a wide variety of programming languages like Python, C++, Java, etc. It can process images and videos to identify objects, faces, or even the handwriting of a human. When it is integrate
3 min read