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

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

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

原图:

我尝试了以下代码。

  1. import cv2
  2. import numpy as np
  3. image = cv2.imread(r'D:\\photo\\11.jpg')
  4. height,width,_=image.shape
  5. Gauss = cv2.GaussianBlur(image, (3, 3), 0)
  6. gray = cv2.cvtColor(Gauss, cv2.COLOR_BGR2GRAY)
  7. edges = cv2.Canny(gray, 50, 150, apertureSize=3)
  8. cv2.imshow('edges',edges)
  9. lines = cv2.HoughLines(edges, 1, np.pi / 180, 130)
  10. cnt=0
  11. if lines is not None:
  12. for line in lines:
  13. rho, theta = line[0]
  14. a = np.cos(theta)
  15. b = np.sin(theta)
  16. x0 = a * rho
  17. y0 = b * rho
  18. x1 = int(x0 + 1000 * (-b))
  19. y1 = int(y0 + 1000 * (a))
  20. x2 = int(x0 - 1000 * (-b))
  21. y2 = int(y0 - 1000 * (a))
  22. cv2.line(image, (x1, y1), (x2, y2), (0, 0, 255), 2)
  23. cnt=cnt+1
  24. cv2.imshow('Detected Lines', image)
  25. cv2.waitKey(0)
  26. cv2.destroyAllWindows()

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

ovfsdjhp

ovfsdjhp1#

来,看看你怎么想。我觉得你的高斯模糊对你一点帮助都没有,所以我把它去掉了。
我反转灰度并重新缩放它,使白色变成接近黑色。然后我对中间十字的大小(41x41)进行卷积,只强调十字形状。然后我将卷积后的图像通过Hough线检测,它做得很好。

  1. import cv2
  2. import numpy as np
  3. image = cv2.imread('crosstab.png')
  4. height,width,_=image.shape
  5. Gauss = cv2.GaussianBlur(image, (3, 3), 0)
  6. gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  7. gray = gray.max()-gray
  8. gray[:,0:260] = 0
  9. kern = np.ones((41,41),dtype=int)*-1
  10. kern[19:21,:] = 20
  11. kern[:,19:21] = 20
  12. kern = kern / kern.sum()
  13. gray = cv2.filter2D(gray, ddepth=-1, kernel=kern )
  14. edges = cv2.Canny(gray, 50, 150, apertureSize=3)
  15. cv2.imshow('edges',edges)
  16. lines = cv2.HoughLines(edges, 1, np.pi / 180, 130)
  17. cnt=0
  18. if lines is not None:
  19. for line in lines:
  20. rho, theta = line[0]
  21. a = np.cos(theta)
  22. b = np.sin(theta)
  23. x0 = a * rho
  24. y0 = b * rho
  25. x1 = int(x0 + 1000 * (-b))
  26. y1 = int(y0 + 1000 * (a))
  27. x2 = int(x0 - 1000 * (-b))
  28. y2 = int(y0 - 1000 * (a))
  29. cv2.line(image, (x1, y1), (x2, y2), (0, 0, 255), 2)
  30. cnt=cnt+1
  31. cv2.imshow('Detected Lines', image)
  32. cv2.waitKey(0)
  33. cv2.destroyAllWindows()

输出量:

展开查看全部

相关问题