绘制由cv :: HoughLines检测到的线 [英] Draw the lines detected by cv::HoughLines

查看:595
本文介绍了绘制由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);
    }
}

解决方案

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:

For more information about implementation of this see this tutorial.

这篇关于绘制由cv :: HoughLines检测到的线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆