首页 > 解决方案 > 如何在 Python 中使用 OpenCV 正确过滤此图像

问题描述

我有一个计算机视觉项目,我需要识别一些包含方向标志的图腾上的数字,示例图像在这里: 示例图像

所以我尝试了很多方法比如服用拉普拉斯,

img = cv2.imread(imgpath)
img = cv2.resize(img,(600,600))
imaj = cv2.GaussianBlur(img,(11,11),0)
imaj = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
laplas = cv2.Laplacian(imaj,cv2.CV_16SC1,ksize=5,scale=5,delta=1)

应用此功能后,我可以很好地区分背景和数字,但由于输出变为 16SC1,我无法获取图像中的轮廓。我尝试对此进行阈值处理,但仍然无法弄清楚任何事情。

这就是我从范围(5000,8000)阈值并将其转换为uint8后得到的,拉普拉斯算子和阈值之后

最后,我尝试在此处使用以下代码从中获取轮廓:

def drawcntMap(filteredimg):
    """
    Draws bounding boxes on the contour map for each image
    """
    contour = cv2.findContours(filteredimg, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[1]
    digitCnts = []
    # draw bounding boxes for contour areas, then filter them approximately on 
    # digit size and append filtered ones to a list
    for c in contour:
        (x, y, w, h) = cv2.boundingRect(c)
        if w >= 7 and h >= 10 and w <=50 and h <=70:
            digitCnts.append(c)
    #create another contour map with filtered contour areas
    cnt2 = cv2.drawContours(img.copy(), digitCnts, -1, (0,0,255),2)
    #draw bounding boxes again on the filtered contour map.
    for c in digitCnts:
        (x, y, w, h) = cv2.boundingRect(c)
        cv2.rectangle(cnt2, (x, y), (x+w, y+h), (0,255,0), 2)
    return cnt2,digitCnts

结果将是:

结果

我怎样才能改进我的解决方案来完成这项任务并获得所有数字?除了拉普拉斯滤波器,我尝试通过降低对比度和提取白色区域来使图像变暗(它确实有点好,但仍然无法获得所有数字),我尝试了高斯模糊和精巧边缘,但在图腾和背景的某些地方是在相同的像素值轮廓合并。

标签: python-3.xopencvcomputer-vision

解决方案


推荐阅读