Ask Your Question

How do the rho and theta values work in HoughLines?

asked 2012-10-07 20:12:56 -0500

ZachTM gravatar image







edit retag flag offensive close merge delete

3 answers

Sort by » oldest newest most voted

answered 2012-10-08 04:11:19 -0500

elmiguelao gravatar image

updated 2012-10-08 04:13:21 -0500

The resulting rho and theta are indeed one step away from the line(s) you are looking for in the image. They represent a line passing through the origin that is perpendicular to the line you want to find. This page has a great introduction to Hough transform concepts, and explains this point.

You might want to use cvHoughLines2 instead, which will allow finding segments instead of lines and a couple of other improvements.

In this example you find some code to draw the lines from rho and theta; the idea is simple: calculate a point of that line, namely x0 = rho cos(theta), y0 = rho sin(theta), and notice that the slope of the line is (-theta), cos(-theta)=cos(theta), sin(-theta)=-sin(theta), and the very large numbers are there to use integer arithmetic.

    float rho = lines[i][0];
    float theta = lines[i][1];
    Point pt1(cvRound(x0 + 1000*(-b)),
              cvRound(y0 + 1000*(a)));
    Point pt2(cvRound(x0 - 1000*(-b)),
              cvRound(y0 - 1000*(a)));
    line( color_dst, pt1, pt2, Scalar(0,0,255), 3, 8 );
edit flag offensive delete link more


Great resource it was very helpful. Thank you for your answer!

ZachTM gravatar imageZachTM ( 2012-10-08 08:48:10 -0500 )edit

answered 2017-09-08 12:23:02 -0500 gravatar image

Rho is the size of the 'bucket' or accumulator array for all incoming rho, or distance from origin. The longest possible distance in an image is from the top left to bottom right, so for maximum accuracy, plug that value into houghlines rho.

Theta is the size of the bucket for all incoming theta. For maximum accuracy you need a value of 180. To only detect even angles, theta should be 90. to only detect angles divisible by 30, theta should be 6

edit flag offensive delete link more

answered 2019-03-11 10:46:04 -0500

twasnow gravatar image

I hate this method for finding points it doesn't take in to account the size of the image. And makes obvious translations way harder.

Instead I recommend solving for X given Y or Y given X

Y = (rho - aX)/b

X= (rho - bX)/a

(Where a = cos(theta) and b = sin(theta)

Now just sub in the intersects you want to find

[fX(Y=0), fY(X=0) | fX(Y=image height), fY(X=image width)]

Better yet let's say you need to know where the line crosses in the middle of the screen. Now you have the formula.

edit flag offensive delete link more
Login/Signup to Answer

Question Tools


Asked: 2012-10-07 20:12:56 -0500

Seen: 14,631 times

Last updated: Mar 11