converted some more samples to C++
[profile/ivi/opencv.git] / samples / c / bgfg_segm.cpp
1 #include <opencv2/video/background_segm.hpp>
2 #include <opencv2/highgui/highgui.hpp>
3
4 #include <stdio.h>
5
6 //this is a sample for foreground detection functions
7 int main(int argc, char** argv)
8 {
9     IplImage*       tmp_frame = NULL;
10     CvCapture*      cap = NULL;
11     bool update_bg_model = true;
12
13     if( argc < 2 )
14         cap = cvCaptureFromCAM(0);
15     else
16         cap = cvCaptureFromFile(argv[1]);
17     
18     if( !cap )
19     {
20         printf("can not open camera or video file\n");
21         return -1;
22     }
23     
24     tmp_frame = cvQueryFrame(cap);
25     if(!tmp_frame)
26     {
27         printf("can not read data from the video source\n");
28         return -1;
29     }
30
31     cvNamedWindow("BG", 1);
32     cvNamedWindow("FG", 1);
33
34     CvBGStatModel* bg_model = 0;
35     
36     for( int fr = 1;tmp_frame; tmp_frame = cvQueryFrame(cap), fr++ )
37     {
38         if(!bg_model)
39         {
40             //create BG model
41             bg_model = cvCreateGaussianBGModel( tmp_frame );
42             //bg_model = cvCreateFGDStatModel( temp );
43             continue;
44         }
45         
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);
53         if( k == 27 ) break;
54         if( k == ' ' )
55             update_bg_model = !update_bg_model;
56     }
57
58
59     cvReleaseBGStatModel( &bg_model );
60     cvReleaseCapture(&cap);
61
62     return 0;
63 }