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

标签 python opencv image-processing opencv3.0

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

所以我有这张图片。 The input image

检测到轮廓的输出图像 The output image

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

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 选择。我无法检测到其中一些圆圈。 .

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

最佳答案

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

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

enter image description here

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

更新:

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

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

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

enter image description here

然后我使用 np.median(equalized_img) 获得了均衡图像的中值,并通过选择低于 [0.6 *中位数]

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

enter image description here

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

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

关于python - 在 opencv 中突出显示所有可能的圆圈(气泡纸选择),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43083473/

相关文章:

python - 循环使用 Rocket.Chat API

python - 基于numpy 1D数组的2D numpy数组中的智能分配

c++ - Opencv 平均滤波器给出与 Matlab 平均滤波器不同的输出

从 opencv 中的浮点像素坐标重新映射图像

c++ - 使用不同样本多次调用 OpenCV trainEM 函数

image-processing - 使用 AWS Lambda、Amazon API Gateway 即时调整 S3 图像的大小 - HTML IMG 标签的重定向太多

python - 如何在Python中用其区域指定的颜色填充openCV轮廓?

python - 如何在Python中比较字符和换行符

c# - EMGU CV SURF图像匹配

python - 在没有图形界面的集群上使用Python和opencv(ImportError : libXdmcp. so.6)