How to filter only the longest line after Hough Transform

此生再无相见时 提交于 2019-12-01 20:18:16

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

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!