some more fixes in background/foreground subtraction; converted bgfg_segm.cpp sample to C++

This commit is contained in:
Vadim Pisarevsky 2010-12-28 21:28:34 +00:00
parent 2dd0e85264
commit e26ac53589
3 changed files with 42 additions and 56 deletions

View File

@ -419,7 +419,7 @@ public:
double TB=0.9,
double CT=0.05,
uchar shadowOutputValue=127,
int shadowOutputValue=127,
double tau=0.5);
//! the destructor
@ -442,7 +442,7 @@ public:
double TB=0.9,
double CT=0.05,
uchar nShadowDetection=127,
int nShadowDetection=127,
double tau=0.5);
void* model;

View File

@ -539,7 +539,7 @@ BackgroundSubtractorMOG2::BackgroundSubtractorMOG2()
BackgroundSubtractorMOG2::BackgroundSubtractorMOG2(double alphaT,
double sigma, int nmixtures, bool postFiltering, double minArea,
bool detectShadows, bool removeForeground, double Tb, double Tg,
double TB, double CT, uchar shadowValue, double tau)
double TB, double CT, int shadowValue, double tau)
{
model = 0;
initialize(Size(), alphaT, sigma, nmixtures, postFiltering, minArea,
@ -550,7 +550,7 @@ BackgroundSubtractorMOG2::BackgroundSubtractorMOG2(double alphaT,
void BackgroundSubtractorMOG2::initialize(Size frameSize, double alphaT,
double sigma, int nmixtures, bool postFiltering, double minArea,
bool detectShadows, bool removeForeground, double Tb, double Tg,
double TB, double CT, uchar shadowValue, double tau)
double TB, double CT, int shadowValue, double tau)
{
if(!model)
model = new CvGaussBGModel2;
@ -605,10 +605,10 @@ void BackgroundSubtractorMOG2::operator()(const Mat& image0, Mat& fgmask0, doubl
CvGaussBGModel2* bg_model = (CvGaussBGModel2*)model;
CV_Assert(bg_model != 0);
Mat fgmask = fgmask0, image = image0;
Mat image = image0, fgmask = fgmask0;
CV_Assert( image.type() == CV_8UC1 || image.type() == CV_8UC3 );
if( learningRate <= 0 )
if( learningRate < 0 )
learningRate = bg_model->params.fAlphaT;
if( learningRate >= 1 )
{
@ -653,22 +653,22 @@ void BackgroundSubtractorMOG2::operator()(const Mat& image0, Mat& fgmask0, doubl
icvUpdatePixelBackgroundGMM(&bg_model->data,&bg_model->params,alpha,image.data,fgmask.data);
if (!bg_model->params.bPostFiltering)
return;
//foreground filtering: filter out small regions
morphologyEx(fgmask, fgmask, CV_MOP_OPEN, Mat());
morphologyEx(fgmask, fgmask, CV_MOP_CLOSE, Mat());
vector<vector<Point> > contours;
findContours(fgmask, contours, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);
fgmask = Scalar::all(0);
for( size_t i = 0; i < contours.size(); i++ )
if( bg_model->params.bPostFiltering )
{
if( boundingRect(Mat(contours[i])).area() < bg_model->params.minArea )
continue;
drawContours(fgmask, contours, (int)i, Scalar::all(255), -1, 8, vector<Vec4i>(), 1);
//foreground filtering: filter out small regions
morphologyEx(fgmask, fgmask, CV_MOP_OPEN, Mat());
morphologyEx(fgmask, fgmask, CV_MOP_CLOSE, Mat());
vector<vector<Point> > contours;
findContours(fgmask, contours, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);
fgmask = Scalar::all(0);
for( size_t i = 0; i < contours.size(); i++ )
{
if( boundingRect(Mat(contours[i])).area() < bg_model->params.minArea )
continue;
drawContours(fgmask, contours, (int)i, Scalar::all(255), -1, 8, vector<Vec4i>(), 1);
}
}
fgmask.copyTo(fgmask0);

View File

@ -1,7 +1,9 @@
#include <opencv2/video/background_segm.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <stdio.h>
using namespace cv;
void help()
{
printf("\nDo background segmentation, especially demonstrating the use of cvUpdateBGStatModel().\n"
@ -14,51 +16,39 @@ void help()
//this is a sample for foreground detection functions
int main(int argc, char** argv)
{
IplImage* tmp_frame = NULL;
CvCapture* cap = NULL;
VideoCapture cap;
bool update_bg_model = true;
if( argc < 2 )
cap = cvCaptureFromCAM(0);
cap.open(0);
else
cap = cvCaptureFromFile(argv[1]);
cap.open(argv[1]);
help();
if( !cap )
if( !cap.isOpened() )
{
printf("can not open camera or video file\n");
return -1;
}
tmp_frame = cvQueryFrame(cap);
if(!tmp_frame)
{
printf("can not read data from the video source\n");
return -1;
}
namedWindow("BG", 1);
namedWindow("FG", 1);
cvNamedWindow("BG", 1);
cvNamedWindow("FG", 1);
CvBGStatModel* bg_model = 0;
BackgroundSubtractorMOG2 bg_model;
Mat img, fgmask;
for( int fr = 1;tmp_frame; tmp_frame = cvQueryFrame(cap), fr++ )
for(;;)
{
if(!bg_model)
{
//create BG model
bg_model = cvCreateGaussianBGModel( tmp_frame );
//bg_model = cvCreateFGDStatModel( temp );
continue;
}
cap >> img;
double t = (double)cvGetTickCount();
cvUpdateBGStatModel( tmp_frame, bg_model, update_bg_model ? -1 : 0 );
t = (double)cvGetTickCount() - t;
printf( "%d. %.1f\n", fr, t/(cvGetTickFrequency()*1000.) );
cvShowImage("BG", bg_model->background);
cvShowImage("FG", bg_model->foreground);
char k = cvWaitKey(5);
if( img.empty() )
break;
bg_model(img, fgmask, update_bg_model ? -1 : 0);
imshow("image", img);
imshow("foreground mask", fgmask);
char k = (char)waitKey(30);
if( k == 27 ) break;
if( k == ' ' )
{
@ -70,9 +60,5 @@ int main(int argc, char** argv)
}
}
cvReleaseBGStatModel( &bg_model );
cvReleaseCapture(&cap);
return 0;
}