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

时间:2011-12-07 14:19:20

标签: opencv lines identify

我使用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为真。但是,我找不到正确的方法来知道是否没有检测到线路。如果图像中已有一条线,则仅打印角度。谢谢!

1 个答案:

答案 0 :(得分:1)

您必须检查循环外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);
    }
}