Search code examples
pythonpython-3.xopencvdetectionsegment

How to use OpenCV4's FastLineDetector in Python 3?


I'm trying to use FastLineDetector from OpenCV4 library in Python 3 in order to detect lines and segments from images but it seems that there is no way to make it work. I've read the documentation here : https://docs.opencv.org/3.4/df/d4c/classcv_1_1ximgproc_1_1FastLineDetector.html still nothing seems clear to me. I've installed OpenCV 4.1.0 and I'm running Python 3.6 in Ubuntu 18.0.4.

Here is the code I've tried separately :

img = cv2.imread('/tmp/output0.png', cv2.CV_8UC1)
fld = cv2.ximgproc_FastLineDetector.detect(img)
fld = cv2.ximgproc_FastLineDetector.detect(cv2.ximgproc_FastLineDetector(img))
fld.detect(img)

Here are the output errors :

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'
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)

Does someone know how to use FastLineDetector or has an exemple ?

By the way, is there any difference with cv.ximgproc.createFastLineDetector() ?


Solution

  • Here you go

    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