OpenCV Q&A Forum - RSS feedhttp://answers.opencv.org/questions/OpenCV answersenCopyright <a href="http://www.opencv.org">OpenCV foundation</a>, 2012-2018.Mon, 11 Mar 2019 10:46:04 -0500How do the rho and theta values work in HoughLines?http://answers.opencv.org/question/2966/how-do-the-rho-and-theta-values-work-in-houghlines/I have found some source code that finds lines in an image like I want and it uses the following HoughLines:
HoughLines( edges, lines, 1, CV_PI/180, 50, 0, 0 );
What I want to do is add the top, left, right, and bottom borders into the lines vector after Houghlines. From what I read in the documentation:
> lines – The output vector of lines. Each line is represented by a two-element vector (rho, theta) . rho is the distance from the coordinate origin (0,0) (top-left corner of the image) and theta is the line rotation angle in radians
lines uses the rho and theta values to represent lines. And that the 1, and CV_PI/180 arguments are called rho and theta. So I did some research on this algorithm and found this diagram:
![image description](http://upload.wikimedia.org/wikipedia/commons/3/39/Hough_transform_diagram.png)
This looks like a good explanation of what im trying to understand but I still cant wrap my head around how to add the borders using the appropriate rho and theta values. Can somone explain this a little more so that I can possibly understand it? I would really appreciate it! Thanks.Sun, 07 Oct 2012 20:12:56 -0500http://answers.opencv.org/question/2966/how-do-the-rho-and-theta-values-work-in-houghlines/Answer by twasnow for <p>I have found some source code that finds lines in an image like I want and it uses the following HoughLines:</p>
<pre><code>HoughLines( edges, lines, 1, CV_PI/180, 50, 0, 0 );
</code></pre>
<p>What I want to do is add the top, left, right, and bottom borders into the lines vector after Houghlines. From what I read in the documentation:</p>
<blockquote>
<p>lines – The output vector of lines. Each line is represented by a two-element vector (rho, theta) . rho is the distance from the coordinate origin (0,0) (top-left corner of the image) and theta is the line rotation angle in radians</p>
</blockquote>
<p>lines uses the rho and theta values to represent lines. And that the 1, and CV_PI/180 arguments are called rho and theta. So I did some research on this algorithm and found this diagram:
<img alt="image description" src="http://upload.wikimedia.org/wikipedia/commons/3/39/Hough_transform_diagram.png"></p>
<p>This looks like a good explanation of what im trying to understand but I still cant wrap my head around how to add the borders using the appropriate rho and theta values. Can somone explain this a little more so that I can possibly understand it? I would really appreciate it! Thanks.</p>
http://answers.opencv.org/question/2966/how-do-the-rho-and-theta-values-work-in-houghlines/?answer=210119#post-id-210119I 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.
Mon, 11 Mar 2019 10:46:04 -0500http://answers.opencv.org/question/2966/how-do-the-rho-and-theta-values-work-in-houghlines/?answer=210119#post-id-210119Answer by paj006@gmail.com for <p>I have found some source code that finds lines in an image like I want and it uses the following HoughLines:</p>
<pre><code>HoughLines( edges, lines, 1, CV_PI/180, 50, 0, 0 );
</code></pre>
<p>What I want to do is add the top, left, right, and bottom borders into the lines vector after Houghlines. From what I read in the documentation:</p>
<blockquote>
<p>lines – The output vector of lines. Each line is represented by a two-element vector (rho, theta) . rho is the distance from the coordinate origin (0,0) (top-left corner of the image) and theta is the line rotation angle in radians</p>
</blockquote>
<p>lines uses the rho and theta values to represent lines. And that the 1, and CV_PI/180 arguments are called rho and theta. So I did some research on this algorithm and found this diagram:
<img alt="image description" src="http://upload.wikimedia.org/wikipedia/commons/3/39/Hough_transform_diagram.png"></p>
<p>This looks like a good explanation of what im trying to understand but I still cant wrap my head around how to add the borders using the appropriate rho and theta values. Can somone explain this a little more so that I can possibly understand it? I would really appreciate it! Thanks.</p>
http://answers.opencv.org/question/2966/how-do-the-rho-and-theta-values-work-in-houghlines/?answer=173878#post-id-173878Rho 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 6Fri, 08 Sep 2017 12:23:02 -0500http://answers.opencv.org/question/2966/how-do-the-rho-and-theta-values-work-in-houghlines/?answer=173878#post-id-173878Answer by elmiguelao for <p>I have found some source code that finds lines in an image like I want and it uses the following HoughLines:</p>
<pre><code>HoughLines( edges, lines, 1, CV_PI/180, 50, 0, 0 );
</code></pre>
<p>What I want to do is add the top, left, right, and bottom borders into the lines vector after Houghlines. From what I read in the documentation:</p>
<blockquote>
<p>lines – The output vector of lines. Each line is represented by a two-element vector (rho, theta) . rho is the distance from the coordinate origin (0,0) (top-left corner of the image) and theta is the line rotation angle in radians</p>
</blockquote>
<p>lines uses the rho and theta values to represent lines. And that the 1, and CV_PI/180 arguments are called rho and theta. So I did some research on this algorithm and found this diagram:
<img alt="image description" src="http://upload.wikimedia.org/wikipedia/commons/3/39/Hough_transform_diagram.png"></p>
<p>This looks like a good explanation of what im trying to understand but I still cant wrap my head around how to add the borders using the appropriate rho and theta values. Can somone explain this a little more so that I can possibly understand it? I would really appreciate it! Thanks.</p>
http://answers.opencv.org/question/2966/how-do-the-rho-and-theta-values-work-in-houghlines/?answer=2971#post-id-2971The 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](http://homepages.inf.ed.ac.uk/rbf/HIPR2/hough.htm) has a great introduction to Hough transform concepts, and explains this point.
You might want to use [cvHoughLines2](http://www710.univ-lyon1.fr/~bouakaz/OpenCV-0.9.5/docs/ref/OpenCVRef_ImageProcessing.htm#decl_cvHoughLines) instead, which will allow finding segments instead of lines and a couple of other improvements.
In [this example](http://docs.opencv.org/modules/imgproc/doc/feature_detection.html?highlight=houghlines#houghlinesp) 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.
<pre>
for( size_t i = 0; i < lines.size(); i++ )
{
float rho = lines[i][0];
float theta = lines[i][1];
double a = cos(theta), b = sin(theta);
double x0 = a*rho, y0 = b*rho;
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 );
}
</pre>Mon, 08 Oct 2012 04:11:19 -0500http://answers.opencv.org/question/2966/how-do-the-rho-and-theta-values-work-in-houghlines/?answer=2971#post-id-2971Comment by ZachTM for <p>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 <em>perpendicular</em> to the line you want to find. <a href="http://homepages.inf.ed.ac.uk/rbf/HIPR2/hough.htm">This page</a> has a great introduction to Hough transform concepts, and explains this point.</p>
<p>You might want to use <a href="http://www710.univ-lyon1.fr/~bouakaz/OpenCV-0.9.5/docs/ref/OpenCVRef_ImageProcessing.htm#decl_cvHoughLines">cvHoughLines2</a> instead, which will allow finding segments instead of lines and a couple of other improvements. </p>
<p>In <a href="http://docs.opencv.org/modules/imgproc/doc/feature_detection.html?highlight=houghlines#houghlinesp">this example</a> 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.</p>
<pre>for( size_t i = 0; i < lines.size(); i++ )
{
float rho = lines[i][0];
float theta = lines[i][1];
double a = cos(theta), b = sin(theta);
double x0 = a*rho, y0 = b*rho;
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 );
}
</pre>
http://answers.opencv.org/question/2966/how-do-the-rho-and-theta-values-work-in-houghlines/?comment=2982#post-id-2982Great resource it was very helpful. Thank you for your answer!Mon, 08 Oct 2012 08:48:10 -0500http://answers.opencv.org/question/2966/how-do-the-rho-and-theta-values-work-in-houghlines/?comment=2982#post-id-2982