【问题标题】:How to detect lines in noisy line images?如何检测嘈杂的线条图像中的线条?
【发布时间】:2019-10-28 18:55:02
【问题描述】:

我生成带有某些线条的嘈杂图像,比如这个:

我正在尝试使用 OpenCV 检测线条,但出了点问题。

这是我目前的代码,包括生成噪声图像的代码。

import cv2
import numpy as np

def draw_random_lines(img, w, n):
    for i in range(n):
        point1 = (np.random.randint(low = 0, high = w), np.random.randint(low = 0, high = w))
        point2 = (np.random.randint(low = 0, high = w), np.random.randint(low = 0, high = w))
        cv2.line(img,point1,point2,(255,0,0),5)
    x = y = 0
    while(y<w):
        while(x<w):
            if(np.any(img[x, y] != 0)):
                if(np.random.randint(low=0, high=100) < 60):
                    img[x, y] = [255, 255, 255] 
                else:
                    img[x, y] = [0, 0, 0]
            else:
                if(np.random.randint(low=0, high=100) < 95):
                    img[x, y] = [255, 255, 255] 
                else:
                    img[x, y] = [0, 0, 0]
            x+=1
        x=0
        y+=1
    return img

w = 512
img = np.zeros((w,w,3), np.uint8)
img = draw_random_lines(img, w, 5)
cv2.imshow("Original", img)
cv2.imwrite("alo.png", img)
img = cv2.imread("alo.png")

gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray,50,150,apertureSize = 3)

lines = cv2.HoughLines(edges,1,np.pi/180,200)
for line in lines:
    for rho,theta in line:
        a = np.cos(theta)
        b = np.sin(theta)
        x0 = a*rho
        y0 = b*rho
        x1 = int(x0 + 1000*(-b))
        y1 = int(y0 + 1000*(a))
        x2 = int(x0 - 1000*(-b))
        y2 = int(y0 - 1000*(a))

        cv2.line(img,(x1,y1),(x2,y2),(0,0,255),2)


cv2.imshow("Detectada", img)

cv2.waitKey(0)

这是我得到的结果(非常错误):

那么,我怎样才能正确检测这些嘈杂图像中的线条?

【问题讨论】:

    标签: python python-3.x image-processing opencv


    【解决方案1】:

    由于 OpenCV 的 Hough 变换实现在黑色背景上寻找白色像素,因此寻找线条的第一个重要步骤是反转您的嘈杂图像。

    轻微的median blurring 将进一步帮助去除噪声,从而提高霍夫变换的性能。

    对于我建议的解决方案,我还使用了HoughLinesP 方法而不是HoughLines。 (根据我的经验,你会得到“更好”的结果。)

    所以,这是我的代码 sn-p:

    import cv2
    import numpy as np
    
    # Read input
    img = cv2.imread('images/K9YLm.png', cv2.IMREAD_GRAYSCALE)
    
    # Initialize output
    out = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
    
    # Median blurring to get rid of the noise; invert image
    img = 255 - cv2.medianBlur(img, 3)
    
    # Detect and draw lines
    lines = cv2.HoughLinesP(img, 1, np.pi/180, 10, minLineLength=50, maxLineGap=30)
    for line in lines:
        for x1, y1, x2, y2 in line:
            cv2.line(out, (x1, y1), (x2, y2), (0, 0, 255), 2)
    
    cv2.imshow('out', out)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    

    out 的输出如下所示:

    由于HoughLinesP 的使用,您会得到一组相当大的(较小的)行。需要设置一种类似行的“分组”。 (或者,也许可以只在单独的图像上绘制红线,然后重新运行线检测。)

    希望有帮助!

    【讨论】:

    • “也许可以在单独的图像上画红线,然后重新运行线检测”这对我来说似乎是多余的。如果 OpenCV 允许访问参数空间,那么在此处进行一些平滑处理和更好的检测局部最大值的方法将大大有助于解决每条线的多次检测问题。
    • @CrisLuengo 我之前没有正确搜索过,但似乎有this promising Q&A。我将在下周测试他们的解决方案 - 然后将那段代码放在附近。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-15
    • 1970-01-01
    • 1970-01-01
    • 2014-06-16
    • 1970-01-01
    • 1970-01-01
    • 2013-06-02
    相关资源
    最近更新 更多