作者:gnuhpc
出处:http://www.cnblogs.com/gnuhpc/
- /* Demo of the background/foreground detection algorithme */
- #include "cv.h"
- #include "cvaux.h"
- #include "highgui.h"
- #include <ctype.h>
- #include <stdio.h>
- int main(int argc, char** argv)
- {
- /* Start capturing */
- CvCapture* capture = 0;
- if( argc == 1 || (argc == 2 && strlen(argv[1]) == 1 && isdigit(argv[1][0])))
- capture = cvCaptureFromCAM( argc == 2 ? argv[1][0] - '0' : 0 ); //从摄像头获取
- else if( argc == 2 )
- capture = cvCaptureFromAVI( argv[1] ); //从磁盘中的avi视频获取
- if( !capture )
- {
- fprintf(stderr,"Could not initialize.../n");
- return -1;
- }
- /* print a welcome message, and the OpenCV version */
- printf ("Demo of the background classification using CvGaussBGModel %s (%d.%d.%d)/n",
- CV_VERSION,
- CV_MAJOR_VERSION, CV_MINOR_VERSION, CV_SUBMINOR_VERSION);
- /* Capture 1 video frame for initialization */
- IplImage* videoFrame = NULL;
- videoFrame = cvQueryFrame(capture);
- if(!videoFrame)
- {
- printf("Bad frame /n");
- exit(0);
- }
- // Create windows
- cvNamedWindow("BG", 1);
- cvNamedWindow("FG", 1);
- // Select parameters for Gaussian model.
- CvGaussBGStatModelParams* params = new CvGaussBGStatModelParams;
- params->win_size=50; //修改为:50
- params->n_gauss=3; //修改为:3
- params->bg_threshold=0.7;
- params->std_threshold=3.5;
- params->minArea=15;
- params->weight_init=0.333; //修改为:1/3
- params->variance_init=30;
- // Creat CvBGStatModel
- // cvCreateGaussianBGModel( IplImage* first_frame, CvGaussBGStatModelParams* parameters )
- // or
- // cvCreateGaussianBGModel( IplImage* first_frame )
- CvBGStatModel* bgModel = cvCreateGaussianBGModel(videoFrame ,params);
- int key=-1;
- while(key != 'q')
- {
- // Grab a fram
- videoFrame = cvQueryFrame(capture);
- if( !videoFrame )
- break;
- // Update model
- cvUpdateBGStatModel(videoFrame,bgModel);
- // Display results
- cvShowImage("BG", bgModel->background);
- cvShowImage("FG", bgModel->foreground);
- key = cvWaitKey(10);
- }
- cvDestroyWindow("BG");
- cvDestroyWindow("FG");
- cvReleaseBGStatModel( &bgModel );
- cvReleaseCapture(&capture);
- return 0;
- }