0

该代码用于运动跟踪。它跟踪 2 张图像的不同点。但是,每次调用该函数时,内存大小都会增加很多。我知道这可能与 cvPoints 有关。有什么好的方法来解决它?谢谢

 float calcorient(IplImage *imgA, IplImage *imgB) {
    // Initialize, load two images from the file system, and
    // allocate the images and other structures we will need for
    // results.
    //
    const int MAX_CORNERS = 500;
    CvSize    img_sz   = cvGetSize( imgA );
    int       win_size = 10;
    IplImage* imgC = cvCreateImage( img_sz, IPL_DEPTH_32F, 3 );
    // The first thing we need to do is get the features
    // we want to track.
    //
    IplImage* eig_image = cvCreateImage( img_sz, IPL_DEPTH_32F, 1 );
    IplImage* tmp_image = cvCreateImage( img_sz, IPL_DEPTH_32F, 1 );
    int           corner_count = MAX_CORNERS;
    CvPoint2D32f* cornersA     = new CvPoint2D32f[ MAX_CORNERS ];
    cvGoodFeaturesToTrack(
        imgA,
        eig_image,
        tmp_image,
        cornersA,
        &corner_count,
        0.01,
        5.0,
        0,
        3,
        0,
        0.04
        );
    cvFindCornerSubPix(
        imgA,
        cornersA,
        corner_count,
        cvSize(win_size,win_size),
        cvSize(-1,-1),
        cvTermCriteria(CV_TERMCRIT_ITER|CV_TERMCRIT_EPS,20,0.03)
        );
    // Call the Lucas Kanade algorithm
    //
    char  features_found[ MAX_CORNERS ];
    float feature_errors[ MAX_CORNERS ];
    CvSize pyr_sz = cvSize( imgA->width+8, imgB->height/3 );
    IplImage* pyrA = cvCreateImage( pyr_sz, IPL_DEPTH_32F, 1 );
    IplImage* pyrB = cvCreateImage( pyr_sz, IPL_DEPTH_32F, 1 );
    CvPoint2D32f* cornersB     = new CvPoint2D32f[ MAX_CORNERS ];
    cvCalcOpticalFlowPyrLK(
        imgA,
        imgB, 
        pyrA,
        pyrB,
        cornersA,
        cornersB,
        corner_count,
        cvSize( win_size,win_size ),
        5,
        features_found,
        feature_errors,
        cvTermCriteria( CV_TERMCRIT_ITER | CV_TERMCRIT_EPS, 20, .3 ),
        0
        );
    // Now make some image of what we are looking at:
    //
    float sum=0;

    for( int i=0; i<corner_count; i++ ) {
        if( features_found[i]==0|| feature_errors[i]>550 ) {
            //printf("Error is %f\n",feature_errors[i]);
            continue;
        }
        //printf("Got it\n");
        sum+=cornersA[i].x-cornersB[i].x;
        //printf("%f\n",sum);
        CvPoint p0 = cvPoint(
            cvRound( cornersA[i].x ),
            cvRound( cornersA[i].y )
            );
        CvPoint p1 = cvPoint(
            cvRound( cornersB[i].x ),
            cvRound( cornersB[i].y )
            );
        cvLine( imgC, p0, p1, CV_RGB(255,0,0),2 );
    }
    //cvNamedWindow("ImageA",0);
    //cvNamedWindow("ImageB",0);
    //cvNamedWindow("LKpyr_OpticalFlow",0);
    //cvShowImage("ImageA",imgA);
    //cvShowImage("ImageB",imgB);
    //cvShowImage("LKpyr_OpticalFlow",imgC);
    //cvWaitKey(1);
    return sum;
}
4

2 回答 2

4

看起来你正在分配一堆内存而不是删除它。

尝试添加以下行:

delete [] cornersA; 
delete [] cornersB;

在您的功能结束时。

您还可以查看这些cvCreate调用——如果它们没有在其他地方得到释放,那可能是导致问题的原因。

于 2012-08-08T18:22:35.957 回答
3

由于 CvReleaseImage,您必须释放您的 IplImage。

于 2012-08-08T18:35:20.793 回答