3

I have been learning Hough transforms recently and I believe that I have a good grasp on how they work, but I have run into an issue with the probabilistic Hough transform. I know that it is supposed to return segments, but my issue is that for the segments returned, several clearly belong to the same line. The code below is primarily from the OpenCV Python tutorial, with a few edits.

As can be seen in the input and output, There are several segments of the same lines that appear. I have exhausted my efforts trying to fine tune the program parameters (canny options, line length, line gap, resolution, and threshold) but no matter what I try I can't seem to get connected lines. I know that Hough transforms use a binning or voting system, but I either misunderstand it or I am using it incorrectly. To me it seems like the binning should take lines that have roughly the same rho and theta and group them together, therefore merging the lines.

Essentially that is all that I would like to do: keep the min x,y and max x,y so that I can merge the segments which "should" be merged (as decided by some bin or percent difference). If I keep pushing through I may be able to find a complex way to do it but is there a simple/better way for me to solve this problem?

Sorry if this is a repost. I tried to find other resources online but most questions did not address this aspect of Hough transforms.

import cv2
import numpy as np
import math
from matplotlib import pyplot as plt

img = cv2.imread('hough_test.jpg')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(img,50,200,apertureSize = 3)
minLineLength = 250
#maximum allowed distance bewteen two points on a line to count them as connected
maxLineGap = 10
#will return several segments of the same line
lines = cv2.HoughLinesP(edges,1,np.pi/180,80,minLineLength,maxLineGap)
edges=np.dstack((edges,edges,edges))
i=0
theta = np.zeros(lines.shape[0])
for x1,y1,x2,y2 in lines[:,0]:
    theta[i] = math.atan2(y2-y1,x2-x1)
    cv2.line(edges,(x1,y1),(x2,y2),(0,0,255),2)
    i+=1
print sorted(theta)
plt.show()
cv2.namedWindow('frame',0)
cv2.imshow('frame',edges)
cv2.namedWindow('frame1',0)
cv2.imshow('frame1',img)

cv2.waitKey(0)
cv2.destroyAllWindows()

Here are the input and output images:

hippietrail
  • 15,848
  • 18
  • 99
  • 158
atjenk
  • 65
  • 7
  • 1
    Looking at the [documentation](http://docs.opencv.org/2.4/modules/imgproc/doc/feature_detection.html?highlight=houghlinesp#houghlinesp) of `HoughLinesP()`, the key parameters seem to be `threshold`, `minLineLength` and `maxLineGap`. To get long lines, i would try to decrease the threashold, increase `minLineLength` and `maxLineGap`. – francis Feb 09 '16 at 21:08
  • Thank you for answering francis, but I have already tried this exhaustively as I mentioned in my post. I may go back and create trackbars and an update loop to try even more but from my initial trials that did not seem to solve the issue. – atjenk Feb 09 '16 at 22:25

0 Answers0