gpt4 book ai didi

opencv - Hough Lines P - 如何知道是否没有检测到线?

转载 作者:太空宇宙 更新时间:2023-11-03 23:03:46 25 4
gpt4 key购买 nike

我使用 HoughLinesP 来识别线条。但是,我的问题是我怎么知道什么时候没有检测到线。我尝试将矢量线与 null 进行比较,但没有任何反应。准确地说,这是我的代码:

for(size_t k=0; k<lines.size(); k++){
Vec4i l = lines[k];
line(gray, Point(l[0], l[1]), Point(l[2], l[3]),Scalar(0,0,255), 3, CV_AA);
double x = l[2]-l[0];
double y = l[3]-l[1];
double slope = (x/y);
double rad = atan(slope);
double pi = 3.1416;
double deg = (rad*180)/pi;
double fin = deg+90;

int part = 0;
if (lines.empty()){printf("NO LINE IN THIS PORTION OF THE IMAGE!");part = 0;}
else{
if (fin>=0 && fin<=45){part = 1;}
else if (fin>45 && fin<=90){part = 2;}
else if (fin>90 && fin<=135){part = 3;}
else if (fin>135 && fin<=180){part = 4;}
}
printf("portion number = %d angle = %f PART = %d\n",j, fin, part);
}

我正在使用 part 变量标识特定扇区。如果没有检测到任何行,我希望 part == 0 为真。但是,我找不到正确的方法来知道是否没有检测到线条。如果图像中已经有一条线,则仅打印角度。谢谢!

最佳答案

您必须检查 lines 向量在循环外是否为空。您现在设置它的方式 lines 将永远不会被检测为空,因为循环仅在 lines.size() > 0 时执行。

这是一个修复:

int part = 0;
double fin = -1.0; // just an arbitrary sentinel value (can be whatever you want here).

if(lines.empty())
{
part = 0;
fin = -1.0;
printf("portion number = %d angle = %f PART = %d\n",j, fin, part);
printf("NO LINE IN THIS PORTION OF THE IMAGE!");
}
else
{
for(size_t k=0; k<lines.size(); k++){
Vec4i l = lines[k];
line(gray, Point(l[0], l[1]), Point(l[2], l[3]),Scalar(0,0,255), 3, CV_AA);
double x = l[2]-l[0];
double y = l[3]-l[1];
double slope = (x/y);
double rad = atan(slope);
double pi = 3.1416;
double deg = (rad*180)/pi;
fin = deg+90;

if (fin>=0 && fin<=45){part = 1;}
else if (fin>45 && fin<=90){part = 2;}
else if (fin>90 && fin<=135){part = 3;}
else if (fin>135 && fin<=180){part = 4;}

printf("portion number = %d angle = %f PART = %d\n",j, fin, part);
}
}

关于opencv - Hough Lines P - 如何知道是否没有检测到线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8416780/

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