如何在 Python 3 中使用 OpenCV4 的 FastLineDetector?

mag*_*gle 4 python opencv detection segment python-3.x

我试图在 Python 3 中使用 OpenCV4 库中的 FastLineDetector 来检测图像中的线条和段,但似乎没有办法让它工作。我在这里阅读了文档:https : //docs.opencv.org/3.4/df/d4c/classcv_1_1ximgproc_1_1FastLineDetector.html对我来说仍然没有什么清楚。我已经安装了 OpenCV 4.1.0,并且在 Ubuntu 18.0.4 中运行 Python 3.6。

这是我单独尝试过的代码:

img = cv2.imread('/tmp/output0.png', cv2.CV_8UC1)
fld = cv2.ximgproc_FastLineDetector.detect(img)
Run Code Online (Sandbox Code Playgroud)
fld = cv2.ximgproc_FastLineDetector.detect(cv2.ximgproc_FastLineDetector(img))
fld.detect(img)
Run Code Online (Sandbox Code Playgroud)

以下是输出错误:

fld = cv2.ximgproc_FastLineDetector.detect(img)
Traceback (most recent call last):
  File "<input>", line 1, in <module>
TypeError: descriptor 'detect' requires a 'cv2.ximgproc_FastLineDetector' object but received a 'numpy.ndarray'
Run Code Online (Sandbox Code Playgroud)
fld = cv2.ximgproc_FastLineDetector.detect(cv2.ximgproc_FastLineDetector(img))
Traceback (most recent call last):
  File "<input>", line 1, in <module>
TypeError: Incorrect type of self (must be 'ximgproc_FastLineDetector' or its derivative)
Run Code Online (Sandbox Code Playgroud)

有人知道如何使用 FastLineDetector 或有一个例子吗?

顺便说一下,与 cv.ximgproc.createFastLineDetector() 有什么区别吗?

epi*_*iac 9

干得好

def FLD(image):
    # Create default Fast Line Detector class
    fld = cv2.ximgproc.createFastLineDetector()
    # Get line vectors from the image
    lines = fld.detect(image)
    # Draw lines on the image
    line_on_image = fld.drawSegments(image, lines)
    # Plot
    plt.imshow(line_on_image, interpolation='nearest', aspect='auto')
    plt.show()
    return line_on_image
Run Code Online (Sandbox Code Playgroud)

  • 对我有用,谢谢。对于那些不知道的人,您需要有 opencv-contrib-python 来实现此代码,而不是 opencv-python。 (4认同)