Quantcast
Channel: OpenCV Q&A Forum - RSS feed
Viewing all articles
Browse latest Browse all 2088

HoughLinesP line gap parameter eliminating lines

$
0
0
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](http://docs.opencv.org/3.0-beta/doc/py_tutorials/py_imgproc/py_houghlines/py_houghlines.html) 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](/upfiles/146679389076277.png) 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](/upfiles/14667939558072475.png) Finally, the output from HoughLines gives this. ![image description](/upfiles/14667942046985803.png) 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](/upfiles/14667944159131553.png) 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.

Viewing all articles
Browse latest Browse all 2088

Trending Articles