python - opencv findContours 错过了一些区域。[没有得到所有正确的边界框]

标签 python opencv image-processing extract image-recognition

我是opencv的新手,开始通过从简单的验证码中提取字符来学习它。
经过一番努力,我得到了findContours和一些清理图像的方法,有时有效,但不是更频繁。

例如:

  • 我有一个原始图像(已经缩放到大尺寸):
    enter image description here
  • 转换为灰度并使用 cv2.threshold干净的:
    enter image description here
  • 使用 cv2.findContours获取边界框:

  • enter image description hereW只盖了一半,没有得到b .

    我的代码:
    from StringIO import StringIO
    import string
    
    from PIL import Image
    import requests
    import cv2
    import numpy as np
    import matplotlib.pyplot as plt
    
    def get_ysdm_captcha():
        url = 'http://www.ysdm.net/common/CleintCaptcha'
        r = requests.get(url)
        img = Image.open(StringIO(r.content))
        return img
    
    def scale_image(img, ratio):
        return img.resize((int(img.width*ratio), int(img.height*ratio)))
    
    def draw_rect(im):
        im = np.array(im)
    
        if len(im.shape) == 3 and im.shape[2] == 3:
            imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
        else:
            imgray = im
    
        #plt.imshow(Image.fromarray(imgray), 'gray')
        pilimg = Image.fromarray(imgray)
        ret,thresh = cv2.threshold(imgray,127,255,0)
    
        threimg = Image.fromarray(thresh)
    
        plt.figure(figsize=(4,3))
        plt.imshow(threimg, 'gray')
        plt.xticks([]), plt.yticks([])
    
        contours, hierarchy = cv2.findContours(np.array(thresh),cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
        areas = []
    
        for c in contours:
            rect = cv2.boundingRect(c)
            area = cv2.contourArea(c)
            areas.append(area)
            x,y,w,h = rect
    
            if area > 2000 or area < 200 : continue
    
            cv2.rectangle(thresh,(x,y),(x+w,y+h),(0,255,0),1)
            plt.figure(figsize=(1,1))
            plt.imshow(threimg.crop((x,y,x+w,y+h)), 'gray')
            plt.xticks([]), plt.yticks([])
    
        plt.figure(figsize=(10,10))
    
        plt.figure()
        plt.imshow(Image.fromarray(thresh), 'gray')
        plt.xticks([]), plt.yticks([])
    
    
    image = get_ysdm_captcha()
    im = scale_image(image, 3)
    im = np.array(im)
    
    imgray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
    imgray = cv2.GaussianBlur(imgray,(5,5),0)
    # im = cv2.medianBlur(imgray,9)
    # im = cv2.bilateralFilter(imgray,9,75,75)
    
    draw_rect(imgray)
    

    我尽力写了上面的代码。
    我想象的解决方案是:
  • 找到有什么办法告诉cv2.findContours我需要4一些尺寸的边界框
  • 尝试了一些不同的参数(我从 http://docs.opencv.org/2.4/modules/imgproc/doc/structural_analysis_and_shape_descriptors.html?highlight=findcontours#findcontours 尝试了所有,但仍然不起作用)

  • 现在我被卡住了,不知道如何改进cv2.findContours ...

    最佳答案

    您可以使用形态学操作来修改图像并填补空白,例如 erodedilate
    看这里:
    http://docs.opencv.org/2.4/doc/tutorials/imgproc/erosion_dilatation/erosion_dilatation.html

    原来的:

    enter image description here

    扩张:

    enter image description here

    顺便说一句:我将在原始图像中实现 HSV 分离步骤,删除所有“白色/灰色/黑色”内容(低饱和度)。这将减少 Blob 的数量。在转换为灰度之前执行此操作。

    这是过滤结果:saturation > 90
    enter image description here

    最终结果:(之前添加了一个模糊步骤)

    enter image description here

    此外,如果始终存在渐变,您可以检测到这一点并过滤掉更多颜色。但是,如果您刚刚开始图像处理,那就有点多了;)

    关于python - opencv findContours 错过了一些区域。[没有得到所有正确的边界框],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45052191/

    相关文章:

    python - 将 Pi 相机模块与 OpenCV Python 结合使用

    c++ - 提高图像对比度

    python - 如何使用 python matplotlib 估计卫星图像中的像素数

    python - 如何将月份名称更改为数字?

    python - 在两个列表中搜索项目

    android - Android 上使用 Termux 的 Jupyter 笔记本无法上传文件和识别目录

    python - OPENCV调整大小以获取图像平均值-与gimp不同的结果

    java - 如何使用 JAVA 将本地镜像而不是 URL 发送到 Microsoft 计算机视觉 API

    c++ - 使用递归算法在 OpenCV 中进行连通分量标记

    python - distutils、distutils2、pip 和要求