1 #include <opencv2/video/background_segm.hpp>
2 #include <opencv2/highgui/highgui.hpp>
6 //this is a sample for foreground detection functions
7 int main(int argc, char** argv)
9 IplImage* tmp_frame = NULL;
10 CvCapture* cap = NULL;
11 bool update_bg_model = true;
14 cap = cvCaptureFromCAM(0);
16 cap = cvCaptureFromFile(argv[1]);
20 printf("can not open camera or video file\n");
24 tmp_frame = cvQueryFrame(cap);
27 printf("can not read data from the video source\n");
31 cvNamedWindow("BG", 1);
32 cvNamedWindow("FG", 1);
34 CvBGStatModel* bg_model = 0;
36 for( int fr = 1;tmp_frame; tmp_frame = cvQueryFrame(cap), fr++ )
41 bg_model = cvCreateGaussianBGModel( tmp_frame );
42 //bg_model = cvCreateFGDStatModel( temp );
46 double t = (double)cvGetTickCount();
47 cvUpdateBGStatModel( tmp_frame, bg_model, update_bg_model ? -1 : 0 );
48 t = (double)cvGetTickCount() - t;
49 printf( "%d. %.1f\n", fr, t/(cvGetTickFrequency()*1000.) );
50 cvShowImage("BG", bg_model->background);
51 cvShowImage("FG", bg_model->foreground);
52 char k = cvWaitKey(5);
55 update_bg_model = !update_bg_model;
59 cvReleaseBGStatModel( &bg_model );
60 cvReleaseCapture(&cap);