Ask Your Question

err1101's profile - activity

2020-02-15 13:33:01 -0600 received badge  Notable Question (source)
2019-06-17 07:15:01 -0600 received badge  Popular Question (source)
2016-06-27 11:45:29 -0600 received badge  Scholar (source)
2016-06-27 11:45:25 -0600 answered a question HoughLinesP line gap parameter eliminating lines

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)
2016-06-24 14:09:51 -0600 asked a question 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.