Ask Your Question
0

HoughLinesP line gap parameter eliminating lines

asked 2016-06-24 13:55:37 -0600

err1101 gravatar image

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.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2016-06-27 11:45:25 -0600

err1101 gravatar image

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)
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2016-06-24 13:55:37 -0600

Seen: 6,214 times

Last updated: Jun 27 '16