6

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() ?

maggle
  • 485
  • 4
  • 11
  • What if you use `cv2.ximgproc_FastLineDetector().detect(img)`? – Willem Van Onsem Jul 13 '19 at 09:44
  • Still doesn't work. It shows me this error : ```cv2.ximgproc_FastLineDetector().detect(img) Traceback (most recent call last): File "", line 1, in TypeError: Incorrect type of self (must be 'ximgproc_FastLineDetector' or its derivative)``` – maggle Jul 15 '19 at 07:18

1 Answers1

13

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
epistemophiliac
  • 829
  • 8
  • 20
  • 4
    Works for me, thanks. For those who don't know, you need to have opencv-contrib-python to implement this code, not opencv-python. – Doğuş Oct 19 '19 at 12:37
  • @Doğuş, in case you're using Anaconda, their default [opencv](https://anaconda.org/main/opencv) package seems to include the contrib modules, so you don't need a special package. – lapis Jan 13 '20 at 11:01