1

I used HoughLinesP to identify lines. However, my problem is how do I know when there are no lines detected. I have tried comparing the vector lines to null but nothing happens. To be precise, here is my code:

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

I am identifying a particular sector with the part variable. If there are no lines detected, I would like for part == 0 to be true. But, I can't find the correct way to know if there are no lines detected. It is only printing angles if there is already a line in the image. Thanks!

mevatron
  • 13,911
  • 4
  • 55
  • 72
cmsl
  • 271
  • 2
  • 7
  • 17

1 Answers1

1

You will have to check if the lines vector is empty outside of the loop. The way you have it setup now lines will never be detected as empty because the loop only executes if lines.size() > 0.

Here is a fix:

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);
    }
}
mevatron
  • 13,911
  • 4
  • 55
  • 72