Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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

img = cv2.imread('lines.png')
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()

image description 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) image description

Finally, the output from HoughLines gives this. image description 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. image description 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.