2016-12-26 656 views
0

可以通過opencv 3.0和python提供一個示例實現代碼或指針來實現LSD嗎? HoughLines和HoughLinesP在python中沒有給出想要的結果,並且想要在python中測試LSD,但是沒有獲得任何地方。Opencv 3中的LineSegmentDetector與Python

我試圖做到以下幾點:

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

然而,當我畫的掩膜線我得到一個錯誤是: LSD.drawSegments(面具,lines_std)類型錯誤:行不一個數字元組

有人可以幫我這個嗎? 在此先感謝。

回答

2

您可以使用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

+0

無論如何,你知道'lsd.detect(img)[1]'的內容嗎?他們是描述符嗎? – eshirima

+0

所以,如果我想發送檢測到的行的寬度到函數,它不會返回其寬度相等或更大的行,但它會返回所有行? lines = lsd.detect(warped,10,10)所以你知道如何只檢測寬度大於特定寬度的行嗎? – sara

+0

你可以應用一個過濾器來刪除所有低於10px的行 – Flayn

3

我可以用下面畫線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調用。

+0

這是一個很好的手工解決方案,但LSD具有drawSegment功能。 – Flayn