OpenCv内存清除以便重用

时间:2012-03-26 03:31:14

标签: c++ memory-management opencv

使用该函数后,openCV中的内存管理出现问题:

cvCreateMemStorage(0);

这就是我想要做的事情:我正在循环使用许多图像,我使用cvExtractSurf()来提取关键点和描述符。我为每一帧都做到这一点,并且每两帧我都在进行一定的处理。我只需要在内存中保留两个帧,因此我使用以下方法创建了内存:

cvCreateMemStorage(0);

我希望一旦在每个帧上完成处理就完全清理内存存储,以便重用它来存储下一帧的关键点和描述符。我需要这样做,因为我正在处理大量的帧,因此创建大量的内存存储不是最好的选择。

我尝试使用cvClearMemStorage(),cvClearSeq()和cvRelease(),但没有任何工作,我总是遇到错误。这里是重要部分的代码(我删除了与questin不直接相关的所有行,所以当然这段代码不会编译):

CvMemStorage* storageSrc = cvCreateMemStorage(0);
CvMemStorage* storageDest = cvCreateMemStorage(0);



// loop on all .bmp files in the selected directory
cout << "STARTING READING THE FRAME" << endl;
while(myHandle!=INVALID_HANDLE_VALUE && myFile.cFileName!= buffer){

    buffer=myFile.cFileName;
    fileLocation = dirName + buffer;
    frameNames.push_back(fileLocation);

    frame = cvLoadImage(fileLocation.c_str(), CV_LOAD_IMAGE_COLOR);
    frameResized = cvCreateImage(cvSize(processingSizeX, processingSizeY), 8, 3);
    cvResize(frame, frameResized, CV_INTER_AREA);

    resizedGray = cvCreateImage(cvSize(processingSizeX, processingSizeY), 8, 1);
    cvCvtColor( frameResized, resizedGray, CV_RGB2GRAY );

    if(!frame){
        fprintf(stderr, "Error when loading the images.");
        exit(-1);
    }


    if(nbFrameRead == 0){

        cout << endl;
        cout << "ZONE 1" << endl;
        cout << endl;

        cvSetImageROI( correspond, cvRect( 0, 0, processingSizeX, processingSizeY) );
        cvCopy( frameResized, correspond );
        cvResetImageROI( correspond );

        cvExtractSURF( resizedGray, 0, &srcFrameKeypoints, &srcFrameDescriptors, storageSrc, params );

        nbFrameRead++;


    }
    else if(nbFrameRead == 1){


        cout << endl;
        cout << "ZONE 2" << endl;
        cout << endl;



        cvExtractSURF( resizedGray, 0, &destFrameKeypoints, &destFrameDescriptors, storageDest, params );
        //printf("Nb Key Points in frame %d: %d\n", nbFrameRead, srcFrameDescriptors->total);


        // clear memory and switch current frame to last frame
        cvClearSeq(srcFrameKeypoints);
        cvClearSeq(srcFrameDescriptors);
        cvClearSeq(descriptorsOrderedSrc);
        cvClearMemStorage(storageSrc);
        srcFrameKeypoints = cvCloneSeq(destFrameKeypoints, storageSrc);
        descriptorsOrderedSrc = cvCloneSeq(descriptorsOrderedDest, storageSrc);
        cvClearSeq(destFrameKeypoints);
        cvClearSeq(destFrameDescriptors);
        cvClearSeq(descriptorsOrderedDest);
        cvClearMemStorage(storageDest);


        cvSetImageROI( correspond, cvRect( 0, 0, processingSizeX, processingSizeY) );
        cvCopy( frameResized, correspond );
        cvResetImageROI( correspond );

        nbFrameRead++;


    }
    else if(nbFrameRead < bufferSize + 2){

        cout << endl;
        cout << "ZONE 3" << endl;
        cout << endl;


        cvExtractSURF( resizedGray, 0, &destFrameKeypoints, &destFrameDescriptors, storageDest, params );
        //printf("Nb Key Points in frame %d: %d\n", nbFrameRead, srcFrameDescriptors->total);

        //clear memory and switch current frame to last frame
        cvClearSeq(srcFrameKeypoints);
        cvClearSeq(srcFrameDescriptors);
        cvClearSeq(descriptorsOrderedSrc);
        cvClearMemStorage(storageSrc);
        srcFrameKeypoints = cvCloneSeq(destFrameKeypoints, storageSrc);
        descriptorsOrderedSrc = cvCloneSeq(descriptorsOrderedDest, storageSrc);
        cvClearSeq(destFrameKeypoints);
        cvClearSeq(destFrameDescriptors);
        cvClearSeq(descriptorsOrderedDest);
        cvClearMemStorage(storageDest);

        nbFrameRead++;

    }
    else{

        cout << endl;
        cout << "ZONE 4" << endl;
        cout << endl;


        cvExtractSURF( resizedGray, 0, &destFrameKeypoints, &destFrameDescriptors, storageDest, params );


        // clear memory and switch current frame to last frame
        cvClearSeq(srcFrameKeypoints);
        cvClearSeq(srcFrameDescriptors);
        cvClearSeq(descriptorsOrderedSrc);
        cvClearMemStorage(storageSrc);
        srcFrameKeypoints = cvCloneSeq(destFrameKeypoints, storageSrc);
        descriptorsOrderedSrc = cvCloneSeq(descriptorsOrderedDest, storageSrc);
        cvClearSeq(destFrameKeypoints);
        cvClearSeq(destFrameDescriptors);
        cvClearSeq(descriptorsOrderedDest);
        cvClearMemStorage(storageDest);


        nbFrameRead++;
        nbFrameSmoothed++;


    }

    FindNextFile(myHandle,&myFile);
}

此代码有什么问题吗?如果是,我应该怎么做才能完全清理storageSrc和storageDest,以便根据需要多次重复使用它?

提前感谢您的回答。

1 个答案:

答案 0 :(得分:0)