在opencv中突出显示所有可能的圆圈(气泡纸选项)

Sha*_*tef 3 python opencv image-processing opencv3.0

我正在努力自动纠正扫描的气泡表测试.目前,我可以提取工作表的解决方案部分并修复其旋转.

所以我有这个形象. 输入图像

检测到轮廓的输出图像 输出图像

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

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")
Run Code Online (Sandbox Code Playgroud)

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

那么有没有更好的想法/算法来完成这个特定的任务?

Jer*_*uke 6

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

adapt_thresh = cv2.adaptiveThreshold(equ, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 11, 2)
cv2.imshow('adapt_thresh.jpg', adapt_thresh)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

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

更新:

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

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

equalized_img =  cv2.equalizeHist(gray)
cv2.imshow('Equalized Image.jpg', equalized_img )
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

然后我所获得的中值使用均衡的图像np.median(equalized_img),并且通过选择以下的所有像素值施加一个二进制阈值[0.6*值]

ret, thresh = cv2.threshold(equalized_img, lower, 255, 1)
cv2.imwrite("Final Image.jpg", thresh)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

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

希望能帮助到你 .. :)