White and black dot detection using OpenCV | Python

19 Jul, 2019
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).
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.
import cv2
# path ="C:/Users/Personal/Downloads/black dot.jpg"path ="black dot.jpg"
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.
gray = cv2.imread(path, 0)
The function 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.
# thresholdth, threshed = cv2.threshold(gray, 100, 255,       cv2.THRESH_BINARY_INV|cv2.THRESH_OTSU)
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 –
# findcontourscnts = cv2.findContours(threshed, cv2.RETR_LIST,                    cv2.CHAIN_APPROX_SIMPLE)[-2]
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.
# filter by areas1 = 3s2 = 20xcnts = []for cnt in cnts:    if s1<cv2.contourArea(cnt) <s2:        xcnts.append(cnt)
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.
print("\nDots number: {}".format(len(xcnts)))
Output :
23
Now for counting the white dots we need to change the threshold a little bit. we have to use cv2.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.
Input Image:
import cv2path ="white dot.png"  # reading the image in grayscale modegray = cv2.imread(path, 0)  # thresholdth, threshed = cv2.threshold(gray, 100, 255,           cv2.THRESH_BINARY|cv2.THRESH_OTSU)  # findcontourscnts = cv2.findContours(threshed, cv2.RETR_LIST,                     cv2.CHAIN_APPROX_SIMPLE)[-2]  # filter by areas1 = 3s2 = 20xcnts = []  for cnt in cnts:    if s1<cv2.contourArea(cnt) <s2:        xcnts.append(cnt)  # printing outputprint("\nDots number: {}".format(len(xcnts)))
Output :
583
 
References:https://docs.opencv.org/3.0-beta/doc/py_tutorials/py_imgproc/py_thresholding/py_thresholding.htmlhttps://docs.opencv.org/3.1.0/d4/d73/tutorial_py_contours_begin.html
Image-Processing
OpenCV
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Different ways to create Pandas Dataframe
Enumerate() in Python
Python String | replace()
How to Install PIP on Windows ?
*args and **kwargs in Python
Python Classes and Objects
Python OOPs Concepts
Convert integer to string in Python
Introduction To PYTHON