python - 检测复选框是否被勾选的最佳方法

标签 python opencv image-processing cv2

我的工作:

  • 扫描论文
  • 检查横竖线
  • 检测复选框
  • 如何知道复选框是否被勾选

  • 在这一点上,我想我可以通过使用层次结构和轮廓找到它:下面是我的工作
    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)
    
    enter image description here
    我得到了这个结果(一些高轮廓,一些高层次)
    -----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
    


    另一个结果:
    enter image description here
    低轮廓,低层次
    -----Hierarchy-----
    [[[-1 -1  1 -1]
      [ 2 -1 -1  0]
      [-1  1 -1  0]]]
    -----Number of Contours : 3
    


    然而,在某些情况下它并不完美,它没有被勾选但仍然得到了非常高的结果
    [[[-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
    

    enter image description here

    总的来说,在查看整个数据后,打勾和不打勾之间的差距并不令人信服。大约 30% 的盒子给出了错误的结果。所以,真希望有更好的方法。

    最佳答案

    我认为侵 eclipse 功能可以帮助您。使用腐 eclipse 使刻度更大,然后计算非零像素。
    您可以在这里找到基础知识:

    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)
    

    关于python - 检测复选框是否被勾选的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62907802/

    相关文章:

    Python MySQL - 带参数和其他占位符的 mysqldb 查询

    python - 访问 Flask-Restful 资源中的 API 记录器

    opencv - opencv正方形检测辐射地板概率

    c++ - 访问 opencv 矩阵 CV_32S 元素

    algorithm - 在图像上搜索指定项目的最快方法?

    python 读取视频

    python - 在 Python Pandas Dataframe 中动态添加列的数据处理

    python - 通过PIP在opencv-python中没有最新的(4.3.0。)openCV库

    c++ - 使用 opencv 和 c++ 在检测到的圆周围绘制一个矩形

    visual-c++ - 使用英特尔实感保存流