Detecting White and Black Points Using OpenCV | python

Image Processing Using Python — one of the most pressing topics in the modern world. But image processing is a bit tricky and beginners get bored with the first approach. So, in this article, we have a very simple Python image processing program that counts black points on a white surface and white points on a black surface using OpenCV functions ( cv2.imread, cv2.threshold, cv2.findContours, cv2.contourArea ).




Count the black points on the white surface —

First we need to import the OpenCV library. All functions related to image processing are in this library. To store the image path, we're going to handle the variable path.

import cv2

# path = & quot; C: / Users / Personal / Downloads / black dot.jpg & quot;

path = "black dot.jpg"

Input Image — 

Loads a grayscale image. Grayscale mode converts the image to a grayscale black and white image.

gray = cv2.imread (path, 0 )

The cv2.threshold function works as follows: if the pixel value exceeds threshold value, it is assigned one value (may be white), otherwise it is assigned a different value (may be black). The first argument is the original image, which should be a grayscale image (done earlier). The second argument — this is the threshold value that is used to classify pixel values. For the threshold, just enter zero. The algorithm then finds the optimal threshold and returns you as the second output, th. If no Otsu threshold is used, th is the same as the threshold you used.

# threshold

th, threshed = cv2.threshold (gray, 100 , 255 ,

cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)

Outlines can be explained simply as a curve connecting all continuous points (along the border) that have the same color or intensity. Contours are a useful tool for shape analysis and object detection and recognition. Contours give the best precision when using binary images. The cv2.findContours () function has three arguments: the first — original image, second — contour search mode, third — contour approximation method. It draws outlines and hierarchy. Contours — it is a Python list of all the contours of the image. Each individual path is a Numpy array (x, y) of the coordinates of the object's boundary points.

It basically connects the black points of the image to count —

# findcontours

cnts = cv2.findContours (threshed, cv2.RETR_LIST,

  cv2.CHAIN_APPROX_SIMPLE) [ - 2 ]

cv2.contourArea () can calculate the area of ​​an object's outline. Black dots are the object here. when it gets a black point, it calculates the area, and if it meets the minimum area condition to be considered a point, then it puts the value of its area in the xcnts list.

# filter by area

s1 = 3

s2 = 20

xcnts = []

for cnt in cnts:

  if s1 & lt ; cv2.contourArea (cnt) & lt; s2:

xcnts.append (c nt)

Finally, we don't need districts. If it is considered a point, then its scope is included in the xcnts list. Thus, we get the number of points if we calculate the length of the list.

print ( "Dots number: {}" . format ( len (xcnts)))

Output:

 23 



Count the white dots on black background —

Now we need to change the threshold a little to count the white points. we have to use c v2.THRESH_BINARY instead of cv2.THRESH_BINARY_INV because we count white values ​​on a black surface. Other processes are the same. We can change the values ​​of s1 and s2 to test the best result.

Input image:

import cv2

path = "white dot.png"

 
# read grayscale image

gray = cv2.imread (path, 0 )

 
#threshold

th, threshed = cv2.threshold (gray, < code class = "value"> 100 , 255

cv2.THRESH_BINARY | cv2.THRESH_OTSU)

 
# findcontours

cnts = cv2.findContours (threshed, cv2.RETR_LIST, 

cv2.CHAIN_APPROX_SIMPLE) [ - 2 ]

 
# filter by area

s1 = 3

s2 = 20

xcnts = []

  

for cnt in cnts:

if s1 & lt; cv2.contourArea (cnt ) & lt; s2:

xcnts.append (cnt)

 
# printout

print ( " Dots number: {} " . format ( len ( xcnts)))

Output:

 583 

Links:
https://docs.opencv.org/3.0-beta/doc/py_tutorials/py_imgproc/py_thresholding/py_thresholding.html br > https://docs.opencv.org/3.1.0/d4/d73/tutorial_py_contours_begin.html





Get Solution for free from DataCamp guru