OpenCV:cvCreateGaussianBGModel
Example
/* Demo of the background/foreground detection algorithme */
/* Author: Francois Cauwe */
#include <ctype.h>
#include <stdio.h>
#include <iostream>
#include <string>
#include <sstream>
#include <opencv/cv.h>
#include <opencv/cvaux.h>
#include <opencv/highgui.h>
#define DEBUG false
using namespace std;
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]);
if (DEBUG) cout << "Now capturing from AVI, capture = " << capture << endl;
}
if (DEBUG) {
cout << "argc = " << argc << ", argv[1] = " << argv[1] << endl;
}
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);
cvNamedWindow("Original", 1);
cvMoveWindow("BG", 670, 0);
cvMoveWindow("FG", 335, 0);
cvMoveWindow("Original", 0, 0);
// Select parameters for Gaussian model.
CvGaussBGStatModelParams* params = new CvGaussBGStatModelParams;
params->win_size = 200;
params->n_gauss = 5; // cars:5, trees: 2/10
params->bg_threshold = 0.6; // cars: 0.7, trees: 0.9, car thief: 0.6
params->std_threshold = 3.0; // cars: 3.5, car thief: 3.0
params->minArea = 5; // cars: 25, trees: 1
params->weight_init = 0.05;
params->variance_init = 50;
// Creat CvBGStatModel
// cvCreateGaussianBGModel( IplImage* first_frame, CvGaussBGStatModelParams* parameters )
// or
// cvCreateGaussianBGModel( IplImage* first_frame )
CvBGStatModel* bgModel = cvCreateGaussianBGModel(videoFrame, params);
int key = -1;
int frameNr = 0;
while (key != 'q') {
// Grab a frame
videoFrame = cvQueryFrame(capture);
if (!videoFrame) break;
// Update model
cvUpdateBGStatModel(videoFrame, bgModel);
// Display results
cvShowImage("FG", bgModel->foreground);
cvShowImage("BG", bgModel->background);
cvShowImage("Original", videoFrame);
// Print frame number:
if (key == 'f') {
cout << "Frame: " << frameNr << endl;
key = ' ';
}
// Save images:
if (key == 's') {
// string name = string("FG_")+frameNr+".jpg";
stringstream name;
name << "output/Frame_" << frameNr << "_FG.jpg";
cvSaveImage(name.str().c_str(), bgModel->foreground);
name.str("");
name << "output/Frame_" << frameNr << "_BG.jpg";
cvSaveImage(name.str().c_str(), bgModel->background);
name.str("");
name << "output/Frame_" << frameNr << "_Original.jpg";
cvSaveImage(name.str().c_str(), videoFrame);
cout << "Images saved at frame number " << frameNr << endl;
key = ' ';
}
frameNr++;
key = cvWaitKey(10);
}
cvDestroyWindow("BG");
cvDestroyWindow("FG");
cvReleaseBGStatModel(&bgModel);
cvReleaseCapture(&capture);
return 0;
}