有没有一种方法可以使用opencv-python来检测虚线交叉线?

6ioyuze2  于 2023-10-24  发布在  Python
关注(0)|答案(1)|浏览(170)

我目前正面临一个问题。我想检测这个图像中的虚线交叉线,但我的检测总是受到其他因素的影响。任何建议都将受到欢迎。

原图:

我尝试了以下代码。

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()

**问题:**总是检测到灰色和白色边缘线,而且只检测到其中一条虚线交叉线。
输出如下:

ovfsdjhp

ovfsdjhp1#

来,看看你怎么想。我觉得你的高斯模糊对你一点帮助都没有,所以我把它去掉了。
我反转灰度并重新缩放它,使白色变成接近黑色。然后我对中间十字的大小(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()

输出量:

相关问题