如何在霍夫变换后仅过滤最长的行 [英] How to filter only the longest line after Hough Transform

查看:77
本文介绍了如何在霍夫变换后仅过滤最长的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用霍夫变换来获取直线.但是检测到很多线路.我可以知道如何过滤并仅从输出中获取最长的行吗?

I'm currently using the Hough Transform to get the straight lines. But there are a lot of lines detected. Can I know how to filter and only get the longest line from the output?

      HoughLinesP(dst, lines, 1, CV_PI/180, 50, 20, 10 ); //left lane

      for( size_t i = 0; i < lines.size(); i++ )
      {
        Vec4i l = lines[i];
        double theta1,theta2, hyp, result;

        theta1 = (l[3]-l[1]);
        theta2 = (l[2]-l[0]);
        hyp = hypot(theta1,theta2);

        line( cdst, Point(l[0], l[1]), Point(l[2], l[3]), Scalar(255,0,0), 3, CV_AA);

        }

      imshow("detected lines", cdst);

}

推荐答案

据我所知,您实际上已经走了一步:

As far as I can see, you're literally a step away:

hypot功能为您提供起点和终点之间的距离.现在,只需找到最长的这样的距离,相应的线就是最长的.

The hypot function gives you the distance between the start and end points. Now, simply find the longest such distance, and the corresponding line is the longest.

Vec4i max_l;
double max_dist = -1.0;

for( size_t i = 0; i < lines.size(); i++ )
{
    Vec4i l = lines[i];
    double theta1,theta2, hyp, result;

    theta1 = (l[3]-l[1]);
    theta2 = (l[2]-l[0]);
    hyp = hypot(theta1,theta2);

    if (max_dist < hyp) {
        max_l = l;
        max_dist = hyp;
    }           
}

// max_l now has the line of maximum length
line( cdst, Point(max_l[0], max_l[1]), Point(max_l[2], max_l[3]), Scalar(255,0,0), 3, CV_AA);
// do something else with max_l

这篇关于如何在霍夫变换后仅过滤最长的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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