Update the trunk to the OpenCV's CVS (2008-07-14)
[opencv] / samples / c / bgfg_segm.cpp
1 #include "cvaux.h"
2 #include "highgui.h"
3 #include <stdio.h>
4
5 //this is a sample for foreground detection functions
6 int main(int argc, char** argv)
7 {
8     IplImage*       tmp_frame = NULL;
9     CvCapture*      cap = NULL;
10
11     if( argc < 2 )
12     {
13         printf("please specify video file name \n");
14         exit(0);
15     }
16
17     cap = cvCaptureFromFile(argv[1]);
18     tmp_frame = cvQueryFrame(cap);
19     if(!tmp_frame)
20     {
21         printf("bad video \n");
22         exit(0);
23     }
24
25     cvNamedWindow("BG", 1);
26     cvNamedWindow("FG", 1);
27
28     //create BG model
29     CvBGStatModel* bg_model = cvCreateFGDStatModel( tmp_frame );
30     
31     for( int fr = 1;tmp_frame; tmp_frame = cvQueryFrame(cap), fr++ )
32     {
33         double t = (double)cvGetTickCount();
34         cvUpdateBGStatModel( tmp_frame, bg_model );
35         t = (double)cvGetTickCount() - t;
36         printf( "%.1f\n", t/(cvGetTickFrequency()*1000.) );
37         cvShowImage("BG", bg_model->background);
38         cvShowImage("FG", bg_model->foreground);
39         char k = cvWaitKey(5);
40         if( k == 27 ) break;
41         //printf("frame# %d \r", fr);
42     }
43
44
45     cvReleaseBGStatModel( &bg_model );
46     cvReleaseCapture(&cap);
47
48     return 0;
49 }