在 opencv 中高亮显示所有可能的圆圈(Bubble sheet choices)

Highlight all possible circles ( Bubble sheet choices ) in opencv

我正在致力于自动更正已扫描的气泡-sheet 测试。 目前,我可以提取 sheet 的解决方案部分并修复其旋转。

所以我有这张图片。

检测到轮廓的输出图像

运行 以下代码在输出图像中生成

def get_answers(image):
    display_normal("Just image",image)
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    blurry = cv2.GaussianBlur(gray, (3, 3), 1)
    thresh = cv2.threshold(blurry, 225, 255,
                       cv2.THRESH_BINARY_INV)[1]

    display_normal("Binary", thresh)
    # find contours in the thresholded image, then initialize
    # the list of contours that correspond to questions
    cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
                        cv2.CHAIN_APPROX_SIMPLE)
    cnts = cnts[1]

    questionCnts = []

    # loop over the contours
    for c in cnts:
        # compute the bounding box of the contour, then use the
        # bounding box to derive the aspect ratio
        (x, y, w, h) = cv2.boundingRect(c)
        ar = w / float(h)

        # in order to label the contour as a question, region
        # should be sufficiently wide, sufficiently tall, and
        # have an aspect ratio approximately equal to 1
        if w >= 18 and h >= 18 and 0.9 <= ar and ar <= 1.2:
            questionCnts.append(c)


    cv2.drawContours(image, questionCnts, -1, (255, 0, 0), 1)
    display_normal("Image with contours",image.copy())
    if(questionCnts < 45*4):
        raise Exception("Didn't found all possible answers")

问题是:我将输入图像转换为二进制并尝试找到看起来像圆形的轮廓,但我找不到全部可能的 45*4 选择。我未能检测到其中一些圈子..

那么有没有更好的idea/algorithm来完成这个特定的任务?

您可以尝试使用自适应阈值:

adapt_thresh = cv2.adaptiveThreshold(equ, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 11, 2)
cv2.imshow('adapt_thresh.jpg', adapt_thresh)

(我调整了原始图像的大小以使其变小)

更新:

我刚刚执行的另一种方法......

我使用直方图均衡均衡了灰度图像:

equalized_img =  cv2.equalizeHist(gray)
cv2.imshow('Equalized Image.jpg', equalized_img )

然后我使用 np.median(equalized_img) 获得均衡图像的 中值 并通过选择低于 [0.6 * 中值的所有像素值应用二进制阈值]

ret, thresh = cv2.threshold(equalized_img, lower, 255, 1)
cv2.imwrite("Final Image.jpg", thresh)

现在您可以继续在此图像上找到所需的轮廓。

希望对您有所帮助.. :)