Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LineSegmentDetector in Opencv 3 with Python

Can a sample implementation code or a pointer be provided for implementing LSD with opencv 3.0 and python? HoughLines and HoughLinesP are not giving desired results in python and want to test LSD in python but am not getting anywhere.

I have tried to do the following:

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

However when I draw lines on the mask I get an error which is: LSD.drawSegments(mask,lines_std) TypeError: lines is not a numerical tuple

Can someone please help me with this? Thanks in advance.

like image 808
user1996684 Avatar asked Dec 26 '16 10:12

user1996684


4 Answers

You can use cv2.drawSegments function like this:

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

You can check OpenCV documentation.

like image 176
flaviussn Avatar answered Nov 08 '22 05:11

flaviussn


old implementation is not available. Now it is available as follows:

fld = cv2.ximgproc.createFastLineDetector() lines = fld.detect(image)

like image 26
Hacklavya Avatar answered Nov 08 '22 05:11

Hacklavya


I was able to draw the lines in OpenCV 3.2.0 with the following:

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)

I am not sure why all of the extra [0] indirection, but that seems to be what it takes to extract the coordinates.

When unpacking OpenCV returns, I have found it helpful to just print the thing on the console. In this case, I did

print(dlines)

From all of the nested square brackets, I can often work out a solution without having to worry too much about the why and wherefore of it all.

I had previously used a Windows DLL version of LSD that I compiled from the authors' source and called with ctypes.

like image 6
R. Strickland Avatar answered Nov 08 '22 03:11

R. Strickland


The accepted answer works just fine if all you're after is drawing the lines. However, if you want a finer grain control over the lines and their drawing, you can loop over the lines and deal with them individually as suggested. Just a cleaner version would be:

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)
like image 1
Jovan Jovancevic Avatar answered Nov 08 '22 03:11

Jovan Jovancevic