Opencv 3 中的 LineSegmentDetector Python

LineSegmentDetector in Opencv 3 with Python

能否提供一个示例实现代码或指针,用于使用opencv 3.0和python实现LSD? HoughLines 和 HoughLinesP 在 python 中没有给出预期的结果,想在 python 中测试 LSD,但没有取得任何进展。

我已尝试执行以下操作:

LSD=cv2.createLineSegmentDetector(0) lines_std=LSD.detect(mixChl) LSD.drawSegments(mask,lines_std)

然而,当我在蒙版上画线时,出现错误: LSD.drawSegments(mask,lines_std) TypeError: 行不是数字元组

有人可以帮我解决这个问题吗? 提前致谢。

我能够在 OpenCV 3.2.0 中使用以下方法绘制线条:

lsd = cv2.createLineSegmentDetector(0)
dlines = lsd.detect(gray_image)
  for dline in dlines[0]:
    x0 = int(round(dline[0][0]))
    y0 = int(round(dline[0][1]))
    x1 = int(round(dline[0][2]))
    y1 = int(round(dline[0][3]))
    cv2.line(mask, (x0, y0), (x1,y1), 255, 1, cv2.LINE_AA)

我不确定为什么所有额外的 [0] 间接寻址,但这似乎是提取坐标所需要的。

解压 OpenCV returns 时,我发现仅在控制台上打印内容很有帮助。在这种情况下,我做了

print(dlines)

从所有嵌套的方括号中,我通常可以想出一个解决方案,而不必太担心这一切的原因和原因。

我以前使用过 LSD 的 Windows DLL 版本,我从作者的源代码编译并使用 ctypes 调用。

您可以像这样使用 cv2.drawSegments 函数:

#Read gray image
img = cv2.imread("test.png",0)

#Create default parametrization LSD
lsd = cv2.createLineSegmentDetector(0)

#Detect lines in the image
lines = lsd.detect(img)[0] #Position 0 of the returned tuple are the detected lines

#Draw detected lines in the image
drawn_img = lsd.drawSegments(img,lines)

#Show image
cv2.imshow("LSD",drawn_img )
cv2.waitKey(0)

你可以查看 OpenCV documentation

如果您只是想划清界线,那么接受的答案就可以正常工作。但是,如果您想要对线条及其绘图进行更精细的控制,您可以遍历这些线条并按照建议单独处理它们。更简洁的版本是:

mask = np.zeros((img.shape),np.uint8)
lsd = cv2.createLineSegmentDetector(0)
lines = lsd.detect(img)[0]
for l in lines:
    x0, y0, x1, y1 = l.flatten()
    //do whatever and plot using:
    cv2.line(mask, (x0, y0), (x1,y1), 255, 1, cv2.LINE_AA)

旧的实现不可用。 现在可用如下:

fld = cv2.ximgproc.createFastLineDetector() 行 = fld.detect(图像)