StereoMatching
StereoMatching

Reputation: 5019

Draw the lines detected by cv::HoughLines

On this site(tutorial), 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);
  }

Example from the openCV cookbook, I could understand the reason these codes 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);
    }
}

Upvotes: 2

Views: 8712

Answers (1)

Simon G.
Simon G.

Reputation: 370

Hough Line transform returns Polar coordinates. To display the lines on 2D picture, coordinates have to be converted do Cartesian coordinates. Here is some more info about this: http://www.mathsisfun.com/polar-cartesian-coordinates.html

Lines, returned from Hough Transform have only one Cartesian point (intersect between blue and red line):

So to display the line author converts the coordinates to Cartesian and then calculates start and end points which are set to fixed position -1000 and +1000 pixels from the converted point:

pt1.x = cvRound(x0 + 1000*(-b)); //??
pt1.y = cvRound(y0 + 1000*(a)); //??
pt2.x = cvRound(x0 - 1000*(-b)); //??
pt2.y = cvRound(y0 - 1000*(a)); //??

One option to find intersection between returned lines is to use this equation: Line-line intersection

For more information about implementation of this see this tutorial.

Upvotes: 5

Related Questions