【问题标题】:LineSegmentDetector in Opencv 3 with Python带有 Python 的 Opencv 3 中的 LineSegmentDetector
【发布时间】:2016-12-26 10:15:14
【问题描述】:

能否提供示例实现代码或指针,用于使用 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:lines is not a numeric tuple

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

【问题讨论】:

  • 很遗憾,LSD 检测器从 4.1.0 版本开始被移除:github.com/opencv/opencv/commit/…
  • @MaksymGanenko,你知道类似 LSD 的线段检测器吗?
  • @tpk 不幸的是,LSD 很难被击败,但你可以尝试使用来自 contrib 模块的cv2.ximgproc.createFastLineDetector()
  • @MaksymGanenko 是的。不幸的是,他们不得不将其删除。感谢FastLineDetector 的提示。
  • 好消息!线段检测器回到 opencv 4.5.4

标签: python-2.7 opencv3.0


【解决方案1】:

你可以像这样使用 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

【讨论】:

  • 你知道lsd.detect(img)[1]的内容吗?它们是描述符吗?
  • 因此,如果我想将检测到的行的宽度发送给函数,它不会返回宽度等于或大于的行,而是返回所有行? lines = lsd.detect(warped, 10, 10) 那么你知道如何只检测宽度大于特定宽度的线条吗?
  • 您可以应用过滤器删除所有低于10px的行
  • 0 中的cv2.createLineSegmentDetector(0) 是什么意思?谢谢。
  • 0 是没有细化的线检测器模式。枚举在这里定义:docs.opencv.org/4.x/dd/d1a/…
【解决方案2】:

我能够在 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 返回时,我发现在控制台上打印它很有帮助。在这种情况下,我做到了

print(dlines)

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

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

【讨论】:

  • 这是一个很好的手工解决方案,但是 LSD 有一个 drawSegment 功能。
【解决方案3】:

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

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

【讨论】:

    【解决方案4】:

    如果您所追求的只是画线,那么接受的答案就可以了。但是,如果您想对线条及其绘图进行更精细的控制,您可以循环线条并按照建议单独处理它们。更简洁的版本是:

    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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-01
      • 2015-06-03
      • 1970-01-01
      • 2023-04-03
      • 2012-03-16
      • 2016-01-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多