Opencv Circle Detection - RAM加载

时间:2011-11-24 03:20:54

标签: c++ opencv

我最近在OpenCV中进行了一些循环检测,并取得了一些成功。下面的代码在32位平台上的C ++中运行得非常好。然而,我确实有一个小问题;当程序运行时,我的RAM负载继续加载。例如,当我启动程序时,我是250mb,30秒后,我大约是800mb。这继续并最终溢出到硬盘驱动器。我试图在while循环中移动cvReleaseCapture,但我只是得到旧的“内存位置的异常......”。我有什么想法可以清理它吗?非常感谢。这是代码:

#include "stdafx.h"
#include <cv.h>
#include <highgui.h>
#include <math.h>


int main( int argc, char **argv )
{
    CvCapture *capture = 0;
    IplImage  *img = 0;
    int       key = 0;
CvFont font;
cvInitFont(&font, CV_FONT_HERSHEY_PLAIN,1.0,1.0,0,1,CV_AA);

capture = cvCaptureFromCAM( 0 );

if ( !capture ) {
    fprintf( stderr, "Cannot open initialize webcam!\n" );
    return 1;
} 

cvNamedWindow( "result", CV_WINDOW_AUTOSIZE );

while( key != 'q' ) {
    img = cvQueryFrame( capture ); 
    IplImage* gray = cvCreateImage( cvGetSize(img), 8, 1 );
    CvMemStorage* storage = cvCreateMemStorage(0);
    cvCvtColor( img, gray, CV_BGR2GRAY );
    cvSmooth( gray, gray, CV_GAUSSIAN, 9, 9 );
    CvSeq* circles = cvHoughCircles( gray, storage, CV_HOUGH_GRADIENT, 2, gray-    >height/4, 200, 100, 20, 100 );
    int i;
    for( i = 0; i < circles->total; i++ )
    {
         float* p = (float*)cvGetSeqElem( circles, i );
         //cvCircle( img, cvPoint(cvRound(p[0]),cvRound(p[1])), 2, CV_RGB(0,255,0), -1, 8, 0 );
         cvCircle( img, cvPoint(cvRound(p[0]),cvRound(p[1])), cvRound(p[2]), CV_RGB(0,255,0), 2, 8, 0 );
         cvLine (img, cvPoint(cvRound(p[0]+40),cvRound(p[1])), cvPoint(cvRound(p[0]),cvRound(p[1])), CV_RGB(0,255,0), 1, CV_AA,0);
         cvLine (img, cvPoint(cvRound(p[0]),cvRound(p[1]+40)), cvPoint(cvRound(p[0]),cvRound(p[1])), CV_RGB(0,255,0), 1, CV_AA,0);
         cvLine (img, cvPoint(cvRound(p[0]-40),cvRound(p[1])), cvPoint(cvRound(p[0]),cvRound(p[1])), CV_RGB(0,255,0), 1, CV_AA,0);
         cvLine (img, cvPoint(cvRound(p[0]),cvRound(p[1]-40)), cvPoint(cvRound(p[0]),cvRound(p[1])), CV_RGB(0,255,0), 1, CV_AA,0);
         cvPutText(img, "Meow",cvPoint(cvRound(p[0]+45),cvRound(p[1]+45)), &font, CV_RGB(0,0,255)); 

    }


    if( !img ) break;       
    cvShowImage( "result", img ); 
    key = cvWaitKey( 1 );
    cvReleaseCapture( &capture );
}

cvDestroyWindow( "result" );
cvReleaseCapture( &capture );    

return 0;

}

1 个答案:

答案 0 :(得分:0)

摆脱while循环中的cvReleaseCapture。相反,你应该

cvReleaseMemStorage(storage);
cvReleaseImage(gray);

你可能应该移动if (!img) break;行,所以它显示为:

img = cvQueryFrame(capture);
if (!img) break;

修改

实际上,您应该在gray循环之外进行初始捕获(img = cvQueryFrame(capture);),而不是不断创建然后销毁while。然后以正常方式创建gray。然后你不必在while循环中继续创建它,这就是你内存不足的原因(你在每次迭代时创建一个新的图像而不释放它)。

所以

#include "stdafx.h"
#include <cv.h>
#include <highgui.h>
#include <math.h>


int main( int argc, char **argv )
{
    CvCapture *capture = 0;
    IplImage  *img = 0;
    int       key = 0;
    CvFont font;
    cvInitFont(&font, CV_FONT_HERSHEY_PLAIN,1.0,1.0,0,1,CV_AA);

    capture = cvCaptureFromCAM( 0 );

    if ( !capture ) {
        fprintf( stderr, "Cannot open initialize webcam!\n" );
        return 1;
    } 

    cvNamedWindow( "result", CV_WINDOW_AUTOSIZE );

    img = cvQueryFrame( capture );
    if (!img) 
        exit(1);
    IplImage* gray = cvCreateImage( cvGetSize(img), 8, 1 );
    CvMemStorage* storage = cvCreateMemStorage(0);

    while( key != 'q' ) {
        img = cvQueryFrame( capture ); 
        if( !img ) break;  


        cvCvtColor( img, gray, CV_BGR2GRAY );
        cvSmooth( gray, gray, CV_GAUSSIAN, 9, 9 );
        CvSeq* circles = cvHoughCircles( gray, storage, CV_HOUGH_GRADIENT, 2, gray-        >height/4, 200, 100, 20, 100 );
        int i;
        for( i = 0; i < circles->total; i++ )
        {
            float* p = (float*)cvGetSeqElem( circles, i );
            //cvCircle( img, cvPoint(cvRound(p[0]),cvRound(p[1])), 2, CV_RGB(0,255,0), -1, 8, 0 );
            cvCircle( img, cvPoint(cvRound(p[0]),cvRound(p[1])), cvRound(p[2]), CV_RGB(0,255,0), 2, 8, 0 );
            cvLine (img, cvPoint(cvRound(p[0]+40),cvRound(p[1])), cvPoint(cvRound(p[0]),cvRound(p[1])), CV_RGB(0,255,0), 1, CV_AA,0);
            cvLine (img, cvPoint(cvRound(p[0]),cvRound(p[1]+40)), cvPoint(cvRound(p[0]),cvRound(p[1])), CV_RGB(0,255,0), 1, CV_AA,0);
            cvLine (img, cvPoint(cvRound(p[0]-40),cvRound(p[1])), cvPoint(cvRound(p[0]),cvRound(p[1])), CV_RGB(0,255,0), 1, CV_AA,0);
            cvLine (img, cvPoint(cvRound(p[0]),cvRound(p[1]-40)), cvPoint(cvRound(p[0]),cvRound(p[1])), CV_RGB(0,255,0), 1, CV_AA,0);
            cvPutText(img, "Meow",cvPoint(cvRound(p[0]+45),cvRound(p[1]+45)), &font, CV_RGB(0,0,255)); 

        }

        cvShowImage( "result", img ); 
        key = cvWaitKey( 1 );

    }
    cvReleaseMemStorage(storage);
    cvReleaseImage(gray);
    cvDestroyWindow( "result" );
    cvReleaseCapture( &capture );    

    return 0;
}