How to test proximity of lines (Hough transform) in OpenCV
Asked Answered
D

2

7

(This is a follow-up from this previous question).

I was able to successfully use OpenCV / Hough transforms to detect lines in pictures (scanned text); at first it would detect many many lines (at least one line per line of text), but by adjusting the 'threshold' parameter via trial-and-error, it now only detects "real" lines.

(The 'threshold' parameter is dependant on image size, which is a bit of a problem if one has to deal with images of different resolutions, but that's another story).

My problem is that the Hough transform sometimes detects two lines where there is only one; those two lines are very near one another and (apparently) parallel.

=> How can I identify that two lines are almost parallel and very near one another? (so that I can keep only one).

Decato answered 3/9, 2009 at 8:7 Comment(1)
what data do you have from the lines?Nacre
J
9

If you use the standard or multiscale hough, you will end up with the rho and theta coordinates of the lines in polar coordinates. Rho is the distance to the origin, and theta is normally the angle between the detected line and the Y axis. Without looking into the details of the hough transform in opencv, this is a general rule in those coordinates: two lines will be almost parallel and very near one another when: - their thetas are nearly identical AND their rhos are nearly identical OR - their thetas are near 180 degrees apart AND their rhos are near each other's negative

I hope that makes sense.

Judd answered 14/10, 2009 at 9:24 Comment(3)
Yes you're absolutely right; I did find this out after I posted my question; in fact it's really straightforward in OpenCV. Thanks!Decato
Also, if one sets the 'resolution' parameter higher, less lines are found; and this is what I needed actually.Decato
How can we set a threshold to the 'nearly' keyword with rho and theta parameters ? For exemple if I want to know if two lines are nearly parallel with maximum angle between them of 0.5 radian (threshold = 0.5).Hooknosed
S
0

That's interesting about the theta being the angle between the line and the y-axis.

Generally, the rho and theta values are visualized as being the angle from the x-axis to the line perpendicular to the line in question. The rho is then the length of this perpendicular line. Thus, a theta = 90 and rho = 20 would mean a horizontal line 20 pixels up from the origin. A nice image is shown on Hough Transform question

Spam answered 9/12, 2010 at 0:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.