How to compare the positions of the Hough Line in OpenCV?

Using VC ++ and Open CV. Here's what I'm trying to do: find the first three horizontal-horizontal lines and draw them. find all almost vertical lines and draw them if any vertical line is above the horizontal line, then FLAG is set to 0 if there is no vertical Hough line above (all 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, until the flag returns 1. So what am I calculating on the axis incorrectly? Please help me.

+3
source share
2 answers

if( fabs(degree) > 70 ) if( fabs(degree) < 8 ) . 180 ... , , , ( 360 ). if (fabs(cos(angle - desired_angle)) > 0.996), " _ 5 , ". 0.996 5 , - 0.9961946980917455 .

, . 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, - , , .

-,

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

        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;

LAST . any .

+4

, PPHL ( Hough Lines), SHL ( HL)! ! , .

0 ° 180 °, , 90 °, ....

+1

Source: https://habr.com/ru/post/1788717/


All Articles