OpenCV Q&A Forum - RSS feedhttp://answers.opencv.org/questions/OpenCV answersenCopyright <a href="http://www.opencv.org">OpenCV foundation</a>, 2012-2018.Thu, 26 Sep 2013 04:40:23 -0500Draw the lines detected by cv::HoughLineshttp://answers.opencv.org/question/21132/draw-the-lines-detected-by-cvhoughlines/On this site([tutorial](http://docs.opencv.org/doc/tutorials/imgproc/imgtrans/hough_lines/hough_lines.html)), it show us how to draw the lines detected by cv::HoughLines,but I can't understand how could it find out the Point between the lines.
for( size_t i = 0; i < lines.size(); i++ )
{
float rho = lines[i][0], theta = lines[i][1];
Point pt1, pt2;
double a = cos(theta), b = sin(theta);
double x0 = a*rho, y0 = b*rho;
pt1.x = cvRound(x0 + 1000*(-b)); //??
pt1.y = cvRound(y0 + 1000*(a)); //??
pt2.x = cvRound(x0 - 1000*(-b)); //??
pt2.y = cvRound(y0 - 1000*(a)); //??
line( cdst, pt1, pt2, Scalar(0,0,255), 3, CV_AA);
}
Another codes from the openCV2 cookbook.I could understand why the codes from the cookbook work but it is more verbose.
for(auto const &data : lines){
float const rho = data[0];
float const theta = data[1];
if((theta < PI/4. || theta > 3. * PI/4.)){
cv::Point pt1(rho / std::cos(theta), 0);
cv::Point pt2( (rho - result.rows * std::sin(theta))/std::cos(theta), result.rows);
cv::line(result, pt1, pt2, cv::Scalar(255), 1);
}else if{
cv::Point pt1(0, rho / std::sin(theta));
cv::Point pt2(result.cols, (rho - result.cols * std::cos(theta))/std::sin(theta));
cv::line(result, pt1, pt2, cv::Scalar(255), 1);
}
}Sun, 22 Sep 2013 11:38:40 -0500http://answers.opencv.org/question/21132/draw-the-lines-detected-by-cvhoughlines/Answer by ThomasB for <p>On this site(<a href="http://docs.opencv.org/doc/tutorials/imgproc/imgtrans/hough_lines/hough_lines.html">tutorial</a>), it show us how to draw the lines detected by cv::HoughLines,but I can't understand how could it find out the Point between the lines.</p>
<pre><code> for( size_t i = 0; i < lines.size(); i++ )
{
float rho = lines[i][0], theta = lines[i][1];
Point pt1, pt2;
double a = cos(theta), b = sin(theta);
double x0 = a*rho, y0 = b*rho;
pt1.x = cvRound(x0 + 1000*(-b)); //??
pt1.y = cvRound(y0 + 1000*(a)); //??
pt2.x = cvRound(x0 - 1000*(-b)); //??
pt2.y = cvRound(y0 - 1000*(a)); //??
line( cdst, pt1, pt2, Scalar(0,0,255), 3, CV_AA);
}
</code></pre>
<p>Another codes from the openCV2 cookbook.I could understand why the codes from the cookbook work but it is more verbose.</p>
<pre><code>for(auto const &data : lines){
float const rho = data[0];
float const theta = data[1];
if((theta < PI/4. || theta > 3. * PI/4.)){
cv::Point pt1(rho / std::cos(theta), 0);
cv::Point pt2( (rho - result.rows * std::sin(theta))/std::cos(theta), result.rows);
cv::line(result, pt1, pt2, cv::Scalar(255), 1);
}else if{
cv::Point pt1(0, rho / std::sin(theta));
cv::Point pt2(result.cols, (rho - result.cols * std::cos(theta))/std::sin(theta));
cv::line(result, pt1, pt2, cv::Scalar(255), 1);
}
}
</code></pre>
http://answers.opencv.org/question/21132/draw-the-lines-detected-by-cvhoughlines/?answer=21166#post-id-21166I think he tries to understand what the code in the tutorial does.
double a = cos(theta), b = sin(theta);
double x0 = a*rho, y0 = b*rho;
Is just the transformation from polar coordinates to Cartesian coordinates. This is the point where the blue and the red line meets.
![Illustration from Tutorial](http://docs.opencv.org/_images/Hough_Lines_Tutorial_Theory_0.jpg)
pt1.x = cvRound(x0 + 1000*(-b));
pt1.y = cvRound(y0 + 1000*(a));
pt2.x = cvRound(x0 - 1000*(-b));
pt2.y = cvRound(y0 - 1000*(a));
These next four lines of code "calculate" the points x and y, which are then used to draw. I wrote "calculate" because they don't really do, but just move a 1000 pixels in both directions, horizontally and vertically. If you have an image much larger than thousand pixels, you'll find that most lines won't reach the outer borders of the image but end somewhere with a total x-distance of 2000 pixels and a total y-distance of 2000 pixels from end to end. However, all lines include the point (x0,y0), th e one where blue and red line meet. From this point to x, deltaY is 1000 and deltaX is also 1000, same is valid for the distance between this point and y, just deltaY is -1000 and deltaX is also -1000
Mon, 23 Sep 2013 02:05:14 -0500http://answers.opencv.org/question/21132/draw-the-lines-detected-by-cvhoughlines/?answer=21166#post-id-21166Comment by ThomasB for <p>I think he tries to understand what the code in the tutorial does.</p>
<pre><code>double a = cos(theta), b = sin(theta);
double x0 = a*rho, y0 = b*rho;
</code></pre>
<p>Is just the transformation from polar coordinates to Cartesian coordinates. This is the point where the blue and the red line meets.</p>
<p><img alt="Illustration from Tutorial" src="http://docs.opencv.org/_images/Hough_Lines_Tutorial_Theory_0.jpg"></p>
<pre><code>pt1.x = cvRound(x0 + 1000*(-b));
pt1.y = cvRound(y0 + 1000*(a));
pt2.x = cvRound(x0 - 1000*(-b));
pt2.y = cvRound(y0 - 1000*(a));
</code></pre>
<p>These next four lines of code "calculate" the points x and y, which are then used to draw. I wrote "calculate" because they don't really do, but just move a 1000 pixels in both directions, horizontally and vertically. If you have an image much larger than thousand pixels, you'll find that most lines won't reach the outer borders of the image but end somewhere with a total x-distance of 2000 pixels and a total y-distance of 2000 pixels from end to end. However, all lines include the point (x0,y0), th e one where blue and red line meet. From this point to x, deltaY is 1000 and deltaX is also 1000, same is valid for the distance between this point and y, just deltaY is -1000 and deltaX is also -1000</p>
http://answers.opencv.org/question/21132/draw-the-lines-detected-by-cvhoughlines/?comment=21405#post-id-21405These are no pointers, they are POINTS, to be more precise the start and end-point of a line. OpenCV's drawLine-Algorithm simply draws a line between two given points. By respecting the angle theta and r one can construct the line with some simple geometry and the knowledge that a line (red) defined by (r, theta) is normal to the vector r (blue). Since the polar form does not hold any information on the length of the line, the author of this code used a large-enough number (1000) to get the illusion of an "endless" line since it usually exceeds the image's width and height. Thu, 26 Sep 2013 04:40:23 -0500http://answers.opencv.org/question/21132/draw-the-lines-detected-by-cvhoughlines/?comment=21405#post-id-21405Comment by StevenPuttemans for <p>I think he tries to understand what the code in the tutorial does.</p>
<pre><code>double a = cos(theta), b = sin(theta);
double x0 = a*rho, y0 = b*rho;
</code></pre>
<p>Is just the transformation from polar coordinates to Cartesian coordinates. This is the point where the blue and the red line meets.</p>
<p><img alt="Illustration from Tutorial" src="http://docs.opencv.org/_images/Hough_Lines_Tutorial_Theory_0.jpg"></p>
<pre><code>pt1.x = cvRound(x0 + 1000*(-b));
pt1.y = cvRound(y0 + 1000*(a));
pt2.x = cvRound(x0 - 1000*(-b));
pt2.y = cvRound(y0 - 1000*(a));
</code></pre>
<p>These next four lines of code "calculate" the points x and y, which are then used to draw. I wrote "calculate" because they don't really do, but just move a 1000 pixels in both directions, horizontally and vertically. If you have an image much larger than thousand pixels, you'll find that most lines won't reach the outer borders of the image but end somewhere with a total x-distance of 2000 pixels and a total y-distance of 2000 pixels from end to end. However, all lines include the point (x0,y0), th e one where blue and red line meet. From this point to x, deltaY is 1000 and deltaX is also 1000, same is valid for the distance between this point and y, just deltaY is -1000 and deltaX is also -1000</p>
http://answers.opencv.org/question/21132/draw-the-lines-detected-by-cvhoughlines/?comment=21179#post-id-21179Nice explanation!Mon, 23 Sep 2013 04:12:35 -0500http://answers.opencv.org/question/21132/draw-the-lines-detected-by-cvhoughlines/?comment=21179#post-id-21179Comment by stereomatching for <p>I think he tries to understand what the code in the tutorial does.</p>
<pre><code>double a = cos(theta), b = sin(theta);
double x0 = a*rho, y0 = b*rho;
</code></pre>
<p>Is just the transformation from polar coordinates to Cartesian coordinates. This is the point where the blue and the red line meets.</p>
<p><img alt="Illustration from Tutorial" src="http://docs.opencv.org/_images/Hough_Lines_Tutorial_Theory_0.jpg"></p>
<pre><code>pt1.x = cvRound(x0 + 1000*(-b));
pt1.y = cvRound(y0 + 1000*(a));
pt2.x = cvRound(x0 - 1000*(-b));
pt2.y = cvRound(y0 - 1000*(a));
</code></pre>
<p>These next four lines of code "calculate" the points x and y, which are then used to draw. I wrote "calculate" because they don't really do, but just move a 1000 pixels in both directions, horizontally and vertically. If you have an image much larger than thousand pixels, you'll find that most lines won't reach the outer borders of the image but end somewhere with a total x-distance of 2000 pixels and a total y-distance of 2000 pixels from end to end. However, all lines include the point (x0,y0), th e one where blue and red line meet. From this point to x, deltaY is 1000 and deltaX is also 1000, same is valid for the distance between this point and y, just deltaY is -1000 and deltaX is also -1000</p>
http://answers.opencv.org/question/21132/draw-the-lines-detected-by-cvhoughlines/?comment=21297#post-id-21297I still have a question, how do you know the ptr1 and ptr2 are moving on the same line?How to prove it?Tue, 24 Sep 2013 11:35:07 -0500http://answers.opencv.org/question/21132/draw-the-lines-detected-by-cvhoughlines/?comment=21297#post-id-21297Answer by Mostafa Sataki for <p>On this site(<a href="http://docs.opencv.org/doc/tutorials/imgproc/imgtrans/hough_lines/hough_lines.html">tutorial</a>), it show us how to draw the lines detected by cv::HoughLines,but I can't understand how could it find out the Point between the lines.</p>
<pre><code> for( size_t i = 0; i < lines.size(); i++ )
{
float rho = lines[i][0], theta = lines[i][1];
Point pt1, pt2;
double a = cos(theta), b = sin(theta);
double x0 = a*rho, y0 = b*rho;
pt1.x = cvRound(x0 + 1000*(-b)); //??
pt1.y = cvRound(y0 + 1000*(a)); //??
pt2.x = cvRound(x0 - 1000*(-b)); //??
pt2.y = cvRound(y0 - 1000*(a)); //??
line( cdst, pt1, pt2, Scalar(0,0,255), 3, CV_AA);
}
</code></pre>
<p>Another codes from the openCV2 cookbook.I could understand why the codes from the cookbook work but it is more verbose.</p>
<pre><code>for(auto const &data : lines){
float const rho = data[0];
float const theta = data[1];
if((theta < PI/4. || theta > 3. * PI/4.)){
cv::Point pt1(rho / std::cos(theta), 0);
cv::Point pt2( (rho - result.rows * std::sin(theta))/std::cos(theta), result.rows);
cv::line(result, pt1, pt2, cv::Scalar(255), 1);
}else if{
cv::Point pt1(0, rho / std::sin(theta));
cv::Point pt2(result.cols, (rho - result.cols * std::cos(theta))/std::sin(theta));
cv::line(result, pt1, pt2, cv::Scalar(255), 1);
}
}
</code></pre>
http://answers.opencv.org/question/21132/draw-the-lines-detected-by-cvhoughlines/?answer=21164#post-id-21164For process a line you can use the below code.
**code:**
void darwLine(InputOutputArray _src,const Point &point1,const Point &point2)
{
Mat src = _src.getMat();
LineIterator it = LineIterator(src,point1,point2);
for (size_t i=0; i < it.count;i++,it++)
{
cv::Point cur_point = it.pos();
src.at<uchar>(cur_point.x,cur_point.y) = 255;
}
}
void main()
{
Mat src = Mat::zeros(Size(550,500),CV_8UC1);
darwLine(src,Point(0,0),Point(100,150));
imshow("view",src);
waitKey(0);
}
Mon, 23 Sep 2013 01:50:16 -0500http://answers.opencv.org/question/21132/draw-the-lines-detected-by-cvhoughlines/?answer=21164#post-id-21164