视频前景提取 (II)【IplImage版本】

来源:互联网 发布:linux io 编辑:程序博客网 时间:2024/06/04 18:41

以下代码可以求出视频的前景,用的方法就是拿第一帧做差分。。。

但是以下代码有个致命的问题,那就是在视频帧处理的过程中,不断的create却不释放内存,导致了内存溢出。。

#include "cv.h"#include "highgui.h" #include <stdio.h>#include <stdlib.h>#include <string.h>#include <assert.h>#include <math.h>#include <float.h>#include <limits.h>#include <time.h>#include <ctype.h> #ifdef _EiC#define WIN32#endif static CvMemStorage* storage = 0; int main(){    CvCapture* capture = 0;    IplImage *frame, *frame_copy = 0,*img1=NULL;    char* input_name; input_name = "002.avi";      storage = cvCreateMemStorage(0);     if( !input_name || (isdigit(input_name[0]) && input_name[1] == '\0') )//直接从摄像头读入        capture = cvCaptureFromCAM( !input_name ? 0 : input_name[0] - '0' );    else//从AVI视频读入        capture = cvCaptureFromAVI( input_name );      cvNamedWindow( "result", 1 ); int flag=0;    if( capture )    {//如果捕捉到帧了//对每一帧做处理        for(;;)        {if( !cvGrabFrame( capture ))break;            frame = cvRetrieveFrame( capture );            if( !frame )                break;            if(flag==0){//捕捉第一帧frame_copy = cvCreateImage( cvSize(frame->width,frame->height), IPL_DEPTH_8U, frame->nChannels );cvCopy(frame,frame_copy,0);flag=1;}img1 = cvCreateImage( cvSize(frame->width,frame->height), IPL_DEPTH_8U, frame->nChannels );cvAbsDiff(frame,frame_copy,img1);cvShowImage( "src", frame );cvShowImage( "result", img1 );  //10ms中按任意键进入此if块            if( cvWaitKey( 10 ) >= 0 )                break;        }         cvReleaseImage( &frame_copy );cvReleaseImage( &img1 );        cvReleaseCapture( &capture );    }    cvDestroyWindow("src");    cvDestroyWindow("result");     return 0;}


为了解决这个问题,可以在开始的时候设置标志位,只在第一次的时候create图片:


#include "cv.h"#include "highgui.h"#include <stdio.h>#include <stdlib.h>#include <string.h>#include <assert.h>#include <math.h>#include <float.h>#include <limits.h>#include <time.h>#include <ctype.h>#ifdef _EiC#define WIN32#endifint main(){CvCapture* capture = 0;IplImage *frame, *frame_copy = 0,*img1=NULL;char* input_name;input_name = "001.avi";if( !input_name || (isdigit(input_name[0]) && input_name[1] == '\0') )//直接从摄像头读入capture = cvCaptureFromCAM( !input_name ? 0 : input_name[0] - '0' );else//从AVI视频读入capture = cvCaptureFromAVI( input_name ); cvNamedWindow( "result", 1 );int flag=0;if( capture ){//如果捕捉到帧了//对每一帧做处理for(;;){if( !cvGrabFrame( capture ))break;frame = cvRetrieveFrame( capture );if( !frame )break;if(flag==0){//捕捉第一帧if( !frame_copy )frame_copy = cvCreateImage( cvSize(frame->width,frame->height), IPL_DEPTH_8U, frame->nChannels );if( frame->origin == IPL_ORIGIN_TL )  cvCopy( frame, frame_copy, 0 );  else  cvFlip( frame, frame_copy, 0 );flag=1;}//为NULL就create一次,不然内存会溢出if( !img1 )img1 = cvCreateImage( cvSize(frame->width,frame->height), IPL_DEPTH_8U, frame->nChannels );cvAbsDiff(frame,frame_copy,img1);cvShowImage( "src", frame );cvShowImage( "result", img1 );//10ms中按任意键进入此if块if( cvWaitKey( 10 ) >= 0 )break;}cvReleaseImage( &frame_copy );cvReleaseCapture( &capture );//不用释放,因为开始的时候没有显式分配内存//cvReleaseImage( &frame );//cvReleaseImage( &img1 );}cvDestroyWindow("src");cvDestroyWindow("result");return 0;}


0 0