gpt4 book ai didi

c++ - 霍夫变换后如何只过滤最长的线

转载 作者:太空狗 更新时间:2023-10-29 21:41:05 24 4
gpt4 key购买 nike

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

      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);

最佳答案

就我所见,你真的离我只有一步之遥:

hypot 函数为您提供起点和终点之间的距离。现在,简单地找到最长的这样的距离,对应的线是最长的。

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

关于c++ - 霍夫变换后如何只过滤最长的线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29818406/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com