Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Python | OpenCV BGR color palette with trackbars
Next article icon
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.Python3 1==
importcv2
Python3 1==
# 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.Python3 1==
gray = cv2.imread(path, 0)
The functioncv2.thresholdworks 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.Python3 1==
# 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 incv2.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 -Python3 1==
# 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.Python3 1==
#filterbyareas1=3s2=20xcnts=[]forcntincnts:ifs1<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.Python3 1==
print("\nDots number: {}".format(len(xcnts)))
Output :
23

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 cv2.THRESH_BINARY instead ofcv2.THRESH_BINARY_INV because we are counting white values on the black surface. The other process are the same. We can change thes1ands2values to check for the best result.Input Image:Python3 1==
importcv2path="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=[]forcntincnts:ifs1<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.html https://docs.opencv.org/3.1.0/d4/d73/tutorial_py_contours_begin.html

Improve
Practice Tags :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp