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 black surface using OpenCV functions (
cv2.imread, cv2.threshold, cv2.findContours, cv2.contourArea).
Count black dots on a white surface –
At first we need to import OpenCV library. All functions regarding image processing reside in this library. In order to store the path of the image we are going to process in a variable path.
Input Image –
Loading an image in grayscale mode. By grayscale mode, the image is converted to a black & white image composing by shades of gray.
cv2.threshold works as, if pixel value is greater than a threshold value, it is assigned one value (may be white), else it is assigned another value (may be black). First argument is the source image, which should be a grayscale image(done previously). Second argument is the threshold value which is used to classify the pixel values. For threshold value, simply pass zero. Then the algorithm finds the optimal threshold value and returns you as the second output, th. If Otsu thresholding is not used, th is same as the threshold value you used.
Contours can be explained simply as a curve joining all the continuous points (along the boundary), having same color or intensity. The contours are a useful tool for shape analysis and object detection and recognition. Contours give better accuracy for using binary images. There are three arguments in
cv2.findContours() function, first one is source image, second is contour retrieval mode, third is contour approximation method. It outputs the contours and hierarchy. Contours is a Python list of all the contours in the image. Each individual contour is a Numpy array of (x, y) coordinates of boundary points of the object.
It mainly connects the black dots of the image to count –
cv2.contourArea() can calculate the contour area of the object. Here the object is the black dots. when it gets a black dot it will calculate the area and if it satisfies the condition of minimum area to be count as a dot, then it will push the value of its area to the list xcnts.
At last, we don’t need the areas. If it is considered to be a dot, then its area is included in the list xcnts. So we will get the number of the dots if we calculate the length of the list.
Count white dots on a black background –
Now for counting the white dots we need to change the threshold a little bit. we have to use c
v2.THRESH_BINARY instead of
cv2.THRESH_BINARY_INV because we are counting white values on the black surface. The other process are the same. We can change the s1 and s2 values to check for the best result.
- Real-Time Edge Detection using OpenCV in Python | Canny edge detection method
- Top Hat and Black Hat Transform using Python-OpenCV
- Python | Corner detection with Harris Corner Detection method using OpenCV
- Python | Corner Detection with Shi-Tomasi Corner Detection Method using OpenCV
- Gun Detection using Python-OpenCV
- Pedestrian Detection using OpenCV-Python
- Circle Detection using OpenCV | Python
- Python | Smile detection using OpenCV
- OpenCV - Facial Landmarks and Face Detection using dlib and OpenCV
- Face Detection using Python and OpenCV with webcam
- Opencv Python program for Face Detection
- Line detection in python with OpenCV | Houghline method
- Detection of a specific color(blue here) using OpenCV with Python
- OpenCV Python program for Vehicle detection in a Video frame
- Multiple Color Detection in Real-Time using Python-OpenCV
- Python Program to detect the edges of an image using OpenCV | Sobel edge detection method
- Text Detection and Extraction using OpenCV and OCR
- OpenCV C++ Program for coin detection
- OpenCV C++ Program for Face Detection
- OpenCV | Real Time Road Lane Detection