[英]How to count number of dots in an image using python and Opencv?
thredhold
使用THRESH_BINARY_INV
标志,更改为black-background-white-foreground。 findContours
,按面积过滤轮廓(由contourArea
计算) import cv2
gray = cv2.imread("dots.jpg", 0)
## threshold
th, threshed = cv2.threshold(gray, 100, 255,cv2.THRESH_BINARY_INV|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<cv2.contourArea(cnt) <s2:
xcnts.append(cnt)
print("Dots number: {}".format(len(xcnts)))
#Dots number: 23
您指向的答案是将彩色图像转换为灰度图像,因此您应该对图像应用HoughCircles()
并找出结果数组的长度。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.