开发者

How to compare Hough Line positions in OpenCV?

开发者 https://www.devze.com 2023-02-07 05:12 出处:网络
Using VC++ and Open CV. Here\'s what I\'m trying to do: find the first three nearly-horizontal hough lines and draw them.

Using VC++ and Open CV. Here's what I'm trying to do: find the first three nearly-horizontal hough lines and draw them. find all the开发者_StackOverflow中文版 nearly-vertical lines and draw them if any vertical line is above the horizontal line then a FLAG is set to 0 if there is no vertical Hough line above (all are below) the horizontal line then FLAG=1

    int n, i,c=0;
    int flag = 0;

    cvCanny( src, dst, 50, 150, 3 );
    lines = cvHoughLines2( dst, storage, CV_HOUGH_PROBABILISTIC, 1, CV_PI/180, 10, 5, 5 );
    n = lines->total;

    for( i = 0; i < n; i++ )
    {
        CvPoint* line = (CvPoint*)cvGetSeqElem(lines,i);
        CvPoint pt1, pt2, hpt1, hpt2, vpt1, vpt2;
        int hy = 0, vy = 0;         
        pt1 = line[0];
        pt2 = line[1];   
        theta = atan( (double)(pt2.y - pt1.y)/(pt2.x - pt1.x) ); /*slope of line*/
        degree = theta*180/CV_PI;  
        if( fabs(degree) < 8) //checking for near horizontal line
        {
            c++;
            if( c > 0 && c <5)  /*main horizontal lines come first*/
            {
                cvLine( out, pt1, pt2, CV_RGB(255, 255,255), 1, CV_AA, 0 );
                hpt1 = line[0];
                hpt2 = line[1];

                if( hpt1.y > hpt2.y ) //finds out lower end-point
                    hy = hpt1.y;
                else
                    hy = hpt2.y;
            }
        }

        if( fabs(degree) > 70 ) /*near vertical lines*/
        {
            cvLine( out, pt1, pt2, CV_RGB(255, 255,255), 1, CV_AA, 0 );

            vpt1 = line[0];
            vpt2 = line[1];

            if( vpt1.y > vpt2.y ) //finds upper end-pt of vertical line
                    vy = vpt1.y;
            else
                vy = vpt2.y;

            if( vy >= hy ) //if vert line is lower than horizontal line
                flag = 1;
            else 
                flag = 0;

        }

    }
    display( out, "hough lines" );
    return flag;
}

However for an image even if vertical lines are detected above the horizontal line -still the flag is returning 1. So am i counting along the axis wrongly? Please help me out.


The if( fabs(degree) > 70 ) and if( fabs(degree) < 8 ) lines look wrong. An angle of about 180 means almost horizontal ... you probably want to change that, and bear in mind the periodicity of angles (so about 360 is also almost horizontal). A way to nicely handle that would be to use if (fabs(cos(angle - desired_angle)) > 0.996), which means roughly "if angle and desired_angle are within 5 degrees of each other, disregarding direction". 0.996 is roughly the cosine of 5 degrees, if you need it more exact put more digits there - 0.9961946980917455 is a nearer match.

Also, your loop order is off. You don't find the first three nearly-horizontal hough lines and draw them. find all the nearly-vertical lines and draw them if any vertical line is above the horizontal line in this sequence, you loop over all lines, in any order, and process them independently - the vertical ones could come before the horizontal ones, so you wouldn't know what to check for.

Third,

            if( hpt1.y > hpt2.y ) //finds out lower end-point
                hy = hpt1.y;
            else
                hy = hpt2.y;

vs

        if( vpt1.y > vpt2.y ) //finds upper end-pt of vertical line
                vy = vpt1.y;
        else
            vy = vpt2.y;

You use the same code to find the lower coordinate as to find the higher one. Do you think that could work?

Fourth,

        if( vy >= hy ) //if vert line is lower than horizontal line
            flag = 1;
        else 
            flag = 0;

The value of flag depends on the LAST pass through this piece of code. This doesn't match the any in your description.


A much easier approche is to not use the PPHL (progressive probabilistic Hough Lines algorithm) but the SHL (standard HL) ! So that you get the polar form of a line with angle and radius ! You can then just check the angle, without calculating it yourself.

If the output angle is around 0° or 180° it's a vertical line, and if it's around 90° it's a horizontal line....

0

精彩评论

暂无评论...
验证码 换一张
取 消