检测复选框是否被选中的最佳方法 [英] Best way to detect if checkbox is ticked
问题描述
我的工作
- 扫描纸张
- 检查水平线和垂直线
- 检测复选框
- 如何知道复选框是否已选中
- Scan the paper
- Check horizontal and vertical line
- Detect checkbox
- How to know checkbox is ticked or not
在这一点上,我认为我可以通过使用层次和轮廓来找到它:以下是我的工作
At this point, I thought I could find it by using Hierarchical and Contours: Below is my work
for i in range (len( contours_region)): #I already have X,Y,W,H of the checkbox through
#print(i) #cv2.connectedComponentsWithStats
x = contours_region[i][0][1] #when detecting checkbox
x_1 = contours_region[i][2][1]
y = contours_region[i][0][0]
y_1 = contours_region[i][2][0]
image_copy= image.copy()
X,Y,W,H = contours_info[i]
cv2.drawContours(image_copy, [numpy.array([[[X,Y]],[[X+W,Y]],[[X+W,Y+H]],[[X,Y+H]]])], 0, (0,0,255),2)
gray = cv2.cvtColor(image_copy, cv2.COLOR_BGR2GRAY)
ret,bw = cv2.threshold(gray,220,255,cv2.THRESH_BINARY_INV)
contours,hierarchy = cv2.findContours(bw[x:x_1, y:y_1], cv2.RETR_CCOMP,1)
print('-----Hierarchy-----')
print(hierarchy)
print('-----Number of Contours : '+ str(len(contours)))
cv2.imshow('a', image_copy)
cv2.waitKey(0)
我得到了这个结果(一些高轮廓,一些高层次)
I got this result (some high contours, some high hierarchy)
-----Hierarchy-----
[[[-1 -1 1 -1]
[ 2 -1 -1 0]
[ 3 1 -1 0]
[ 4 2 -1 0]
[ 5 3 -1 0]
[ 6 4 -1 0]
[ 7 5 -1 0]
[-1 6 -1 0]]]
-----Number of Contours : 8
另一个结果:
Another result:
轮廓低,层次低
-----Hierarchy-----
[[[-1 -1 1 -1]
[ 2 -1 -1 0]
[-1 1 -1 0]]]
-----Number of Contours : 3
但是,在某些情况下,它没有被打勾,但仍然获得了很高的结果,这并不完美
However, it's not perfect some case where it's not ticked but still got a really high result
[[[-1 -1 1 -1]
[ 2 -1 -1 0]
[ 3 1 -1 0]
[ 4 2 -1 0]
[ 5 3 -1 0]
[-1 4 -1 0]]]
-----Number of Contours : 6
通常,在检查了整个数据之后,刻度和未刻度之间的差距并不令人信服。大约30%的盒子,给出了错误的结果。因此,我真的希望有一个更好的方法。
In general, After review the whole data, the gap is not convincing between ticked and not ticked. Around 30% of boxes, giving the wrong result. Therefore, really wish to have a better method.
推荐答案
我认为腐蚀函数可以为您提供帮助。使用侵蚀使刻度线更大,然后计算非零像素。
在这里您可以找到基本知识:
I think erode function help you. Use erosion to make the ticks bigger then count the non zero pixels. Here You can find the basics:
import cv2
import numpy as np
from google.colab.patches import cv2_imshow
img = cv2.imread("image.png");
cv2_imshow(img)
kernel = np.ones((3, 3), np.uint8)
better_image = cv2.erode(img,kernel)
cv2_imshow(better_image)
这篇关于检测复选框是否被选中的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!