【问题标题】:Find coordinates of the lines detected by the Canny edge detector查找 Canny 边缘检测器检测到的线条的坐标
【发布时间】:2021-05-28 01:21:53
【问题描述】:

我一直在尝试提取图像中存在的线条的坐标。

我尝试了精明的边缘检测来检测线条。他们被成功检测到了。

我怎样才能找到这些线的坐标?

Input image

    img = cv2.imread('/content/sample_data/dilatedtest1.png',0)
    edges = cv2.Canny(img,100,200)
    
    plt.subplot(121),plt.imshow(img,cmap = 'gray')
    plt.title('Original Image'), plt.xticks([]), plt.yticks([])
    plt.subplot(122),plt.imshow(edges,cmap = 'gray')
    plt.title('Edge Image'), plt.xticks([]), plt.yticks([])
    
    plt.show()

【问题讨论】:

  • Canny 找到边缘,而不是线条。图像中的每条线都有两条边,您的输出应该包含两条平行线,每条输入线都靠在一起。

标签: python opencv image-processing computer-vision


【解决方案1】:

cv2.Canny 输出二值图像。您可以使用np.nonzero(edges)np.where(edges != 0) 获取坐标:

img = cv2.imread('/content/sample_data/dilatedtest1.png',0)
edges = cv2.Canny(img, 100, 200)

row_indexes, col_indexes = np.nonzero(edges)

【讨论】:

  • 感谢您的帮助。你能指导我如何找到这些线的交点吗?平行线与图像中的第二行相交。我正在寻找这些点的 x,y 坐标。提前致谢。
  • 例如查看mathopenref.com/coordintersection.html或在谷歌搜索类似的解释
【解决方案2】:

您可以使用cv2.HoughLines() 来检测边缘图像中的线条。这些行以参数形式返回:(rho,theta) 对,其中 rho = x * cos(theta) + y * sin(theta)。下面的代码(取自 OpenCV 的关于使用 cv2.HoughLines() 的教程)显示了如何获取直线 (x1,y1)(x2,y2) 中两点的像素坐标。两条线相交很容易,因为您将线作为点对(在网上搜索线-线交点)。

import cv2
import numpy as np

img = cv2.imread('dave.jpg')
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 rho,theta in lines[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(img,(x1,y1),(x2,y2),(0,0,255),2)

cv2.imwrite('houghlines3.jpg',img)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-30
    • 2012-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多