ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2019-08-23 19:55:51  阅读:188  来源: 互联网

标签:opencv3-0 python opencv image-processing


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

所以我有这个形象.
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

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

希望能帮助到你 .. :)

标签:opencv3-0,python,opencv,image-processing
来源: https://codeday.me/bug/20190823/1700838.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有