【问题标题】:Find checkbox contours using OpenCV使用 OpenCV 查找复选框轮廓
【发布时间】:2019-09-09 20:14:44
【问题描述】:

我有几张图像需要通过使用计算机视觉检测复选框来进行 OMR。

我使用findContours 仅在扫描文档的复选框上绘制轮廓。但是算法会提取文本的每一个轮廓。

from imutils.perspective import four_point_transform
from imutils import contours
import numpy as np
import argparse, imutils, cv2, matplotlib
import matplotlib.pyplot as plt
import matplotlib.image as mpimg

image = cv2.imread("1.jpg")
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
edged = cv2.Canny(blurred, 75, 200)

im_test = [blurred, cv2.GaussianBlur(gray, (7, 7), 0), cv2.GaussianBlur(gray, (5, 5), 5), cv2.GaussianBlur(gray, (11, 11), 0)]
im_thresh = [ cv2.threshold(i, 127, 255, 0)  for i in im_test ]
im_thresh_0 = [i[1] for i in im_thresh ]
im_cnt = [cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)[0] for thresh in im_thresh_0]

im_drawn = [cv2.drawContours(image.copy(), contours, -1, (0,255,0), 1) for contours in im_cnt]

plt.imshow(im_drawn[0])
plt.show()

输入图像:

【问题讨论】:

    标签: python image opencv image-processing computer-vision


    【解决方案1】:

    由于我们只想检测复选框,因此我们的想法是使用两种过滤方法将所需的框与单词隔离开来。在预处理和找到轮廓之后,我们可以遍历每个轮廓并应用过滤器。我们使用cv2.contourArea() 和最小和最大阈值水平,然后使用cv2.approxPolyDP() 计算纵横比,因为正方形的纵横比接近1。

    要检测图像中的边缘,我们可以使用cv2.Canny(),然后使用cv2.findContours() 抓取轮廓,从而生成此图像。请注意如何检测到包括单词和复选框在内的所有轮廓。

    接下来,我们遍历每个检测到的轮廓并使用阈值区域和纵横比进行过滤。使用这种方法,所有 52 个复选框都被检测到了。

    输出

    ('checkbox_contours', 52)

    为了防止潜在的误报,我们可以添加第三个过滤器以确保每个轮廓都有四个点(它是正方形的可能性更高)。如果输入图像是从一个角度,我们可以使用four point transform作为预处理步骤来获得图像的鸟瞰图。

    另一个输入图像集

    输出

    ('checkbox_contours', 2)

    代码

    import numpy as np
    import imutils, cv2
    
    original_image = cv2.imread("1.jpg")
    image = original_image.copy()
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    blurred = cv2.GaussianBlur(gray, (5, 5), 0)
    edged = cv2.Canny(blurred, 120, 255, 1)
    
    cv2.imshow("edged", edged)
    
    cnts = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    cnts = imutils.grab_contours(cnts)
    
    checkbox_contours = []
    
    threshold_max_area = 250
    threshold_min_area = 200
    contour_image = edged.copy()
    
    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 area < threshold_max_area and area > threshold_min_area and (aspect_ratio >= 0.9 and aspect_ratio <= 1.1):
            cv2.drawContours(original_image,[c], 0, (0,255,0), 3)
            checkbox_contours.append(c)
    
    print('checkbox_contours', len(checkbox_contours))
    cv2.imshow("checkboxes", original_image)
    cv2.waitKey(0)
    

    编辑:

    回到这个问题后,这里有一个更强大的解决方案。这个想法非常相似,只是我们使用Otsu's threshold而不是Canny边缘检测来获得二值图像。 Otsu 的阈值会自动计算阈值,因此应该会给出更好的结果。从这里我们找到轮廓,使用轮廓近似、纵横比和轮廓区域进行过滤。结果应该是一样的。

    import cv2
    
    # Load image, convert to grayscale, Otsu's threshold
    image = cv2.imread("1.jpg")
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
    
    # Find contours, filter using contour approximation, aspect ratio, and contour area
    threshold_max_area = 550
    threshold_min_area = 100
    cnts = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    cnts = cnts[0] if len(cnts) == 2 else cnts[1]
    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):
            cv2.rectangle(image, (x, y), (x + w, y + h), (36,255,12), 2)
    
    cv2.imshow("image", image)
    cv2.imshow("thresh", thresh)
    cv2.waitKey()
    

    【讨论】:

    • 谢谢。但是当我在真实数据上尝试代码时,它仍然不起作用。尝试调整阈值以及大约的 len。输入图片:imgur.com/LrzYXga
    • 它对我有用,尝试将 threshold_max_area 更改为 3000。我用这个新的输入图像添加了输出
    • @nathancy,这看起来很棒。然而,有什么方法可以识别一个框是否被选中?
    • @Kalaschnik 是的,同样的问题,你有什么办法吗?
    • @gig 假设复选框的大小相同,识别一个框是否已被选中的一个想法是:1)获取复选框轮廓 2)计算白色像素的数量(也许cv2.countNonZero) 并将其与基线值进行比较或仅比较像素百分比。所以如果说如果 80% 像素是白色的,那么它不会被检查,否则它会被检查
    【解决方案2】:

    嗯...复选框总是在图像的那个区域吗?复选框始终在图像上保持相同大小的区域?

    如果是,您可以仅在图像的该区域运行 findContours...

    或者可能是模板匹配多个对象,来自 OpenCV 文档的示例:https://docs.opencv.org/3.4.3/d4/dc6/tutorial_py_template_matching.html

    【讨论】:

    • 每个文档在不同区域都有复选框,其中一些与文本水平、垂直或组合对齐。此外,我正在尝试构建的解决方案需要是无模板的。
    • 这是一个形状检测器的示例,您可以使用该示例仅检测正方形(您的复选框):pyimagesearch.com/2016/02/08/opencv-shape-detection希望对您有所帮助...
    • 已经提到了那个例子和stackoverflow中的很多线程。但我仍然无法找出如何避免文本区域。
    猜你喜欢
    • 2012-11-06
    • 1970-01-01
    • 2011-06-13
    • 2017-11-11
    • 1970-01-01
    • 2015-06-24
    • 2018-01-18
    • 1970-01-01
    • 2012-12-28
    相关资源
    最近更新 更多