cvFindContours crash with wxWidgets Topic is solved

Do you have a question about makefiles, a compiler or IDE you are using and need to know how to set it up for wxWidgets or why it doesn't compile but other IDE's do ? Post your questions here.
Post Reply
guyanqiu
Knows some wx things
Knows some wx things
Posts: 25
Joined: Sun Jun 13, 2010 2:44 pm

cvFindContours crash with wxWidgets

Post by guyanqiu » Wed Jul 11, 2012 9:18 am

When I use cvFindContours with wxWidgets
I copy the code in the OpenCV dir sampe/c/squares.c,
than change the GUI to wxWidgtes,
But the cvPyrUp and cvFindContours is crashed.
And

Code: Select all

cvCanny( tgray, gray, 0, thresh, 3 );
Only 3 is well ,5 and 7 crashed.
Please forgive my pool English.

Code: Select all

// returns sequence of squares detected on the image.
// the sequence is stored in the specified memory storage
CvSeq* TestDlg::findSquares4( IplImage* img, CvMemStorage* storage )
{
    CvSeq* contours = NULL;
    int i, c, l, N = 11;
    CvSize sz = cvSize( img->width & -2, img->height & -2 );//末尾清零,变位2的整数倍
    IplImage* timg = cvCloneImage( img ); // make a copy of input image
    IplImage* gray = cvCreateImage( sz, 8, 1 );
    IplImage* pyr = cvCreateImage( cvSize(sz.width/2, sz.height/2), 8, 3 );
    IplImage* tgray = cvCreateImage( sz, 8, 1 );
    //wxMessageBox("cvCreateImage");
    CvSeq* result;
    double s, t;
    // create empty sequence that will contain points -
    // 4 points per square (the square's vertices)
    storage = cvCreateMemStorage(0);
    CvSeq* squares = cvCreateSeq( 0, sizeof(CvSeq), sizeof(CvPoint), storage );

    // select the maximum ROI in the image
    // with the width and height divisible by 2
    cvSetImageROI( timg, cvRect( 0, 0, sz.width, sz.height ));

    // down-scale and upscale the image to filter out the noise
    //cvPyrDown( timg, pyr, 7 );
    //cvPyrUp( pyr, timg, 7 );

    // find squares in every color plane of the image
    for( c = 0; c < 3; c++ )
    {
        // extract the c-th color plane
        cvSetImageCOI( timg, c+1 );
        cvCopy( timg, tgray, 0 );
        DrawTowxWindow(WxPanel_Rect, tgray);
        // try several threshold levels
        for( l = 0; l < N; l++ )
        {
            // hack: use Canny instead of zero threshold level.
            // Canny helps to catch squares with gradient shading
            if( l == 0 )
            {
                // apply Canny. Take the upper threshold from slider
                // and set the lower to 0 (which forces edges merging)
                cvCanny( tgray, gray, 0, thresh, 3 );
                // dilate canny output to remove potential
                // holes between edge segments
                cvDilate( gray, gray, 0, 1 );
                DrawTowxWindow(WxPanel_Rect, gray);
            }
            else
            {
                // apply threshold if l!=0:
                //     tgray(x,y) = gray(x,y) < (l+1)*255/N ? 255 : 0
                cvThreshold( tgray, gray, (l+1)*255/N, 255, CV_THRESH_BINARY );
            }

			//Debug here
            wxMessageBox(wxT("Pre cvFindContours"));
///////////////////////////////crash here//////////////////////////////////
            cvFindContours( gray, storage, &contours, sizeof(CvContour),
                CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0) );
            wxMessageBox(wxT("cvFindContours"));

            // test each contour
            while( contours )
            {
                // approximate contour with accuracy proportional
                // to the contour perimeter
                result = cvApproxPoly( contours, sizeof(CvContour), storage,
                    CV_POLY_APPROX_DP, cvContourPerimeter(contours)*0.02, 0 );
                wxMessageBox(wxT("cvApproxPoly"));
                // square contours should have 4 vertices after approximation
                // relatively large area (to filter out noisy contours)
                // and be convex.
                // Note: absolute value of an area is used because
                // area may be positive or negative - in accordance with the
                // contour orientation
                if( result->total == 4 &&
                    cvContourArea(result,CV_WHOLE_SEQ,0) > 1000 &&
                    cvCheckContourConvexity(result) )
                {
                    s = 0;

                    for( i = 0; i < 5; i++ )
                    {
                        // find minimum angle between joint
                        // edges (maximum of cosine)
                        if( i >= 2 )
                        {
                            t = fabs(angle(
                            (CvPoint*)cvGetSeqElem( result, i ),
                            (CvPoint*)cvGetSeqElem( result, i-2 ),
                            (CvPoint*)cvGetSeqElem( result, i-1 )));
                            s = s > t ? s : t;
                        }
                    }

                    // if cosines of all angles are small
                    // (all angles are ~90 degree) then write quandrange
                    // vertices to resultant sequence
                    if( s < 0.3 )//88.3或91.7度的余弦值为0.3,如果s<0.3,那么夹角在88.3-91.7度之间。
                        for( i = 0; i < 4; i++ )
                            cvSeqPush( squares,
                                (CvPoint*)cvGetSeqElem( result, i ));
                }

                // take the next contour
                contours = contours->h_next;
            }
        }
    }
    wxMessageBox(wxT("End Loop"));
    // release all the temporary images
    cvReleaseImage( &gray );
    cvReleaseImage( &pyr );
    cvReleaseImage( &tgray );
    cvReleaseImage( &timg );

    return squares;//返回储存矩形顶点的数据序列
}

Attachments
Test_Code.rar
Here is the test code.
(7.91 KiB) Downloaded 180 times

guyanqiu
Knows some wx things
Knows some wx things
Posts: 25
Joined: Sun Jun 13, 2010 2:44 pm

Re: cvFindContours crash with wxWidgets

Post by guyanqiu » Fri Jul 13, 2012 1:02 am

I solved it by use the OpenCV2.4.2.

Post Reply