有没有使用opencv-python检测交叉虚线的方法?

问题描述 投票:0回答:1

我目前面临一个问题。我想检测该图像中的虚线十字线,但我的检测总是受到其他因素的影响。任何建议将不胜感激。

原图:

我尝试过以下代码。

import cv2
import numpy as np

image = cv2.imread(r'D:\\photo\\11.jpg')
height,width,_=image.shape
Gauss = cv2.GaussianBlur(image, (3, 3), 0)

gray = cv2.cvtColor(Gauss, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray, 50, 150, apertureSize=3)
cv2.imshow('edges',edges)
lines = cv2.HoughLines(edges, 1, np.pi / 180, 130)
cnt=0
if lines is not None:
    for line in lines:
        rho, theta = line[0]
        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(image, (x1, y1), (x2, y2), (0, 0, 255), 2)
        cnt=cnt+1

cv2.imshow('Detected Lines', image)
cv2.waitKey(0)
cv2.destroyAllWindows()

问题:它总是检测到灰色和白色边缘线。此外,它仅检测到十字虚线之一。

输出如下:

python opencv line detection
1个回答
0
投票

在这里,看看你对此有何看法。我认为你的高斯模糊在这里对你没有任何帮助,所以我把它删除了。

我反转灰度并重新缩放它,使白色变得接近黑色。 然后,我对中间十字的大小 (41x41) 进行卷积,仅强烈强调十字形状。然后我将该卷积图像通过霍夫线检测,它做得非常好。

import cv2
import numpy as np

image = cv2.imread('crosstab.png')
height,width,_=image.shape
Gauss = cv2.GaussianBlur(image, (3, 3), 0)

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = gray.max()-gray
gray[:,0:260] = 0

kern = np.ones((41,41),dtype=int)*-1
kern[19:21,:] = 20
kern[:,19:21] = 20
kern = kern / kern.sum()
gray = cv2.filter2D(gray, ddepth=-1, kernel=kern )


edges = cv2.Canny(gray, 50, 150, apertureSize=3)
cv2.imshow('edges',edges)
lines = cv2.HoughLines(edges, 1, np.pi / 180, 130)
cnt=0
if lines is not None:
    for line in lines:
        rho, theta = line[0]
        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(image, (x1, y1), (x2, y2), (0, 0, 255), 2)
        cnt=cnt+1

cv2.imshow('Detected Lines', image)
cv2.waitKey(0)
cv2.destroyAllWindows()

输出:

© www.soinside.com 2019 - 2024. All rights reserved.