5

Im using opencv hough transform to attempt to detect shapes. The longer lines are all very nicely detected using the HoughLines method.but the shorter lines are completely ignored. Is there any way to also detect the shorter lines?

the code I'm using is described on this page http://opencv-python-tutroals.readthedocs.org/en/latest/py_tutorials/py_imgproc/py_houghlines/py_houghlines.html

I'm more interested in lines such as the corner of the house etc. which parameter should I modify to do this with Hough transform? or is there a different algorithm I should be looking at

Hough transform with OpenCV python tutorial

hippietrail
  • 15,848
  • 18
  • 99
  • 158
Pita
  • 1,444
  • 1
  • 19
  • 29

1 Answers1

3

On the link you provide look at HoughLinesP

import cv2
import numpy as np

img = cv2.imread('beach.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray, 50, 150, apertureSize=3)
minLineLength = 100
maxLineGap = 5
lines = cv2.HoughLinesP(edges, 1, np.pi/180, 50, minLineLength, maxLineGap)
for x1, y1, x2, y2 in lines[0]:
    cv2.line(img, (x1, y1), (x2, y2), (0, 255, 0), 2)
cv2.imwrite('canny5.jpg', edges)
cv2.imwrite('houghlines5.jpg', img)

Also look at the edge image generated from Canny. You should only be able to find lines where lines exist in the edge image.

enter image description here

and here is the line detection output overlaid on your image: enter image description here

Play around with variables minLineLength and maxLineGap to get a more desirable output. This method also does not give you the long lines that HoughLines does, but looking at the Canny image, maybe those long lines are not desirable in the first place.

Scott
  • 6,089
  • 4
  • 34
  • 51
  • Thanks for the hint, can you provide what values you used as those two parameters in you image? – Pita May 30 '15 at 08:27
  • @Pita the values you see in the code I posted are the ones that generated the image above. recall, it's only the green lines as I didn't have the original image. – Scott May 30 '15 at 15:55
  • Note that your parameters are not what you think they are. Try passing minLineLength and maxLineGap as named parameters to see this. You can see in the documentation that the parameter after threshold is lines, so minLineLength is working as lines (doing nothing) and maxLineGap is working as minLineLength (and, indeed, increasing maxLineGap returns the same lines, filtering out smaller ones). – Pablo Sep 16 '20 at 08:58