【发布时间】:2020-11-14 23:24:15
【问题描述】:
我一直在尝试检测复选框。虽然我能够检测到其他图像中的方形轮廓,但我无法获得这个特定图像的轮廓。请帮我检测复选框。
这是我的代码,
for myfile in files:
image=cv2.imread(myfile)
image = cv2.resize(image, (180,60), interpolation = cv2.INTER_AREA)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
#apply otsu's threshold
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
#setting up threshold values
threshold_max_area = 300
threshold_min_area = 10
#finding contours in the image
cnts = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
#getting the coordinates for each checkbox
count=0
centers=[]
for c in cnts:
peri = cv2.arcLength(c, True)
approx = cv2.approxPolyDP(c, 0.035 * peri, True)
x,y,w,h = cv2.boundingRect(approx)
aspect_ratio = w / float(h)
area = cv2.contourArea(c)
if len(approx) == 4 and area < threshold_max_area and area > threshold_min_area and (aspect_ratio >= 0.9 and aspect_ratio <= 1.1):
centers.append([x,y,x+w,y+h])
count=count+1
print(centers)
cv2.imshow(" ",image)
cv2.waitKey()
【问题讨论】:
-
您可以从之前提出的问题link得到答案。
-
@Abhishek 当您的复选框中没有勾选标记时,您提到的链接很好。就我而言,有一个刻度线,因此我无法检测到方形轮廓。
-
二值图像上的模板匹配怎么样?如果您知道框的大小,则可以搜索矩形。
标签: python opencv image-processing computer-vision contour