python - 如何在嘈杂的线条图像中检测线条?

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

我生成带有特定线条的嘈杂图像,例如:

Generated image

我正在尝试使用 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)

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

Wrong Results

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

最佳答案

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

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

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

所以,这是我的代码片段:

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 如下所示:

Output

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

希望对您有所帮助!

关于python - 如何在嘈杂的线条图像中检测线条?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56593038/

相关文章:

java - OpenCV - Canny 边缘检测无法正常工作

java - Leptonica findSkew 返回 0.0

python - Pip 将软件包安装在错误的目录中

python - 代码在 IDE 中有效,但在终端控制台中无效

python - 如何计算一个序列在 python 中的给定字符串中出现了多少次?

python - 如何在 Python 中读取一个 100GB 的单行文本文件?

python - 在python中将SVG转换为灰度

python - 在 OpenERP7 中保持自定义模块同步

python-3.x - 模块未找到错误: No module named 'dateutil.parser' ; 'dateutil' is not a package

python - 如何修复函数 'cv::matchTemplate'