提问人:Ethereal 提问时间:9/30/2023 最后编辑:Ethereal 更新时间:10/2/2023 访问量:97
有没有办法使用 opencv-python 检测虚线交叉线?
Is there a method to use opencv-python to detect dashed cross lines?
问:
我目前面临一个问题。我想检测此图像中的虚线,但我的检测始终受到其他因素的影响。任何建议将不胜感激。
原始图像:
我尝试了以下代码。
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()
问题:它始终检测灰色和白色边缘线。此外,它只检测一条虚线交叉线。
输出如下:
答:
0赞
Tim Roberts
10/2/2023
#1
在这里,看看你对此有何看法。我认为你的高斯模糊在这里对你没有任何帮助,所以我把它删除了。
我反转灰度并重新缩放它,使白色变得接近黑色。 然后,我对中间十字架的大小(41x41)进行卷积,该卷积仅强调十字架形状。然后,我将卷积图像通过 Hough 线检测,它做得很好。
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()
输出:
评论