# HoughLinesP line gap parameter eliminating lines

Hi, very new to OpenCV (and image processing), been working to get line detection up and running using HoughLinesP. My code is below, with my input image following, which is drawn in paint (although eventually I'll be moving on to images captured using mobile cameras). The code is mostly from the tutorial on HoughLines

import sys
sys.path.append('C:\Python27\Lib\site-packages')

import cv2
import numpy as np

ret,img = cv2.threshold(img,180,255,0)

element = cv2.getStructuringElement(cv2.MORPH_CROSS,(6,6))
eroded = cv2.erode(img,element)
dilate = cv2.dilate(eroded, element)
skeleton = cv2.subtract(img, dilate)
gray = cv2.cvtColor(skeleton,cv2.COLOR_BGR2GRAY)

minLineLength = 20
maxLineGap = 1

lines = cv2.HoughLinesP(gray, 1, np.pi/180, 10, minLineLength, maxLineGap)

for line in lines:
for x1,y1,x2,y2 in line:
cv2.line(img,(x1,y1),(x2,y2),(0,255,0),2)

cv2.imshow("result", img)
cv2.waitKey(0)
cv2.destroyAllWindows()


Before running the Hough transform, I skeletonize the image to get it down to 1 px (this isn't a perfect skeletonization as the lines get broken near the intersections)

Finally, the output from HoughLines gives this. Overall this image does the job pretty well, but I wanted to eliminate the gap between some of the line segments. Turning up the maxLineGap to 1, however, makes some of the lines disappear. Based on what I read my assumption is that any line that is within maxLineGap to another is joined with it--is this incorrect? Also, is there a good method to go about tuning the parameters of HoughLinesP? My input image is fairly trivial, so I'm not sure what is causing the small breaks that occur in some of the larger line segments.

Thanks for any help.

edit retag close merge delete

Sort by » oldest newest most voted

Alright, it turns out the HoughLinesP function was interpreting my parameters incorrectly. To solve this, I added the names of the parameters, and everything works fine!

lines = cv2.HoughLinesP(gray, rho=1, theta=np.pi/360, threshold=10, minLineLength=10, maxLineGap=20)

more

Official site

GitHub

Wiki

Documentation