Commit e26ac535 authored by Vadim Pisarevsky's avatar Vadim Pisarevsky

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

parent 2dd0e852
...@@ -419,7 +419,7 @@ public: ...@@ -419,7 +419,7 @@ public:
double TB=0.9, double TB=0.9,
double CT=0.05, double CT=0.05,
uchar shadowOutputValue=127, int shadowOutputValue=127,
double tau=0.5); double tau=0.5);
//! the destructor //! the destructor
...@@ -442,7 +442,7 @@ public: ...@@ -442,7 +442,7 @@ public:
double TB=0.9, double TB=0.9,
double CT=0.05, double CT=0.05,
uchar nShadowDetection=127, int nShadowDetection=127,
double tau=0.5); double tau=0.5);
void* model; void* model;
......
...@@ -539,7 +539,7 @@ BackgroundSubtractorMOG2::BackgroundSubtractorMOG2() ...@@ -539,7 +539,7 @@ BackgroundSubtractorMOG2::BackgroundSubtractorMOG2()
BackgroundSubtractorMOG2::BackgroundSubtractorMOG2(double alphaT, BackgroundSubtractorMOG2::BackgroundSubtractorMOG2(double alphaT,
double sigma, int nmixtures, bool postFiltering, double minArea, double sigma, int nmixtures, bool postFiltering, double minArea,
bool detectShadows, bool removeForeground, double Tb, double Tg, 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; model = 0;
initialize(Size(), alphaT, sigma, nmixtures, postFiltering, minArea, initialize(Size(), alphaT, sigma, nmixtures, postFiltering, minArea,
...@@ -550,7 +550,7 @@ BackgroundSubtractorMOG2::BackgroundSubtractorMOG2(double alphaT, ...@@ -550,7 +550,7 @@ BackgroundSubtractorMOG2::BackgroundSubtractorMOG2(double alphaT,
void BackgroundSubtractorMOG2::initialize(Size frameSize, double alphaT, void BackgroundSubtractorMOG2::initialize(Size frameSize, double alphaT,
double sigma, int nmixtures, bool postFiltering, double minArea, double sigma, int nmixtures, bool postFiltering, double minArea,
bool detectShadows, bool removeForeground, double Tb, double Tg, 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) if(!model)
model = new CvGaussBGModel2; model = new CvGaussBGModel2;
...@@ -605,10 +605,10 @@ void BackgroundSubtractorMOG2::operator()(const Mat& image0, Mat& fgmask0, doubl ...@@ -605,10 +605,10 @@ void BackgroundSubtractorMOG2::operator()(const Mat& image0, Mat& fgmask0, doubl
CvGaussBGModel2* bg_model = (CvGaussBGModel2*)model; CvGaussBGModel2* bg_model = (CvGaussBGModel2*)model;
CV_Assert(bg_model != 0); 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 ); CV_Assert( image.type() == CV_8UC1 || image.type() == CV_8UC3 );
if( learningRate <= 0 ) if( learningRate < 0 )
learningRate = bg_model->params.fAlphaT; learningRate = bg_model->params.fAlphaT;
if( learningRate >= 1 ) if( learningRate >= 1 )
{ {
...@@ -653,9 +653,8 @@ void BackgroundSubtractorMOG2::operator()(const Mat& image0, Mat& fgmask0, doubl ...@@ -653,9 +653,8 @@ void BackgroundSubtractorMOG2::operator()(const Mat& image0, Mat& fgmask0, doubl
icvUpdatePixelBackgroundGMM(&bg_model->data,&bg_model->params,alpha,image.data,fgmask.data); icvUpdatePixelBackgroundGMM(&bg_model->data,&bg_model->params,alpha,image.data,fgmask.data);
if (!bg_model->params.bPostFiltering) if( bg_model->params.bPostFiltering )
return; {
//foreground filtering: filter out small regions //foreground filtering: filter out small regions
morphologyEx(fgmask, fgmask, CV_MOP_OPEN, Mat()); morphologyEx(fgmask, fgmask, CV_MOP_OPEN, Mat());
morphologyEx(fgmask, fgmask, CV_MOP_CLOSE, Mat()); morphologyEx(fgmask, fgmask, CV_MOP_CLOSE, Mat());
...@@ -670,6 +669,7 @@ void BackgroundSubtractorMOG2::operator()(const Mat& image0, Mat& fgmask0, doubl ...@@ -670,6 +669,7 @@ void BackgroundSubtractorMOG2::operator()(const Mat& image0, Mat& fgmask0, doubl
continue; continue;
drawContours(fgmask, contours, (int)i, Scalar::all(255), -1, 8, vector<Vec4i>(), 1); drawContours(fgmask, contours, (int)i, Scalar::all(255), -1, 8, vector<Vec4i>(), 1);
} }
}
fgmask.copyTo(fgmask0); fgmask.copyTo(fgmask0);
} }
......
#include <opencv2/video/background_segm.hpp> #include <opencv2/video/background_segm.hpp>
#include <opencv2/highgui/highgui.hpp> #include <opencv2/highgui/highgui.hpp>
#include <stdio.h> #include <stdio.h>
using namespace cv;
void help() void help()
{ {
printf("\nDo background segmentation, especially demonstrating the use of cvUpdateBGStatModel().\n" printf("\nDo background segmentation, especially demonstrating the use of cvUpdateBGStatModel().\n"
...@@ -14,51 +16,39 @@ void help() ...@@ -14,51 +16,39 @@ void help()
//this is a sample for foreground detection functions //this is a sample for foreground detection functions
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
IplImage* tmp_frame = NULL; VideoCapture cap;
CvCapture* cap = NULL;
bool update_bg_model = true; bool update_bg_model = true;
if( argc < 2 ) if( argc < 2 )
cap = cvCaptureFromCAM(0); cap.open(0);
else else
cap = cvCaptureFromFile(argv[1]); cap.open(argv[1]);
help(); help();
if( !cap ) if( !cap.isOpened() )
{ {
printf("can not open camera or video file\n"); printf("can not open camera or video file\n");
return -1; return -1;
} }
tmp_frame = cvQueryFrame(cap); namedWindow("BG", 1);
if(!tmp_frame) namedWindow("FG", 1);
{
printf("can not read data from the video source\n");
return -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) cap >> img;
{
//create BG model if( img.empty() )
bg_model = cvCreateGaussianBGModel( tmp_frame ); break;
//bg_model = cvCreateFGDStatModel( temp );
continue;
}
double t = (double)cvGetTickCount(); bg_model(img, fgmask, update_bg_model ? -1 : 0);
cvUpdateBGStatModel( tmp_frame, bg_model, update_bg_model ? -1 : 0 );
t = (double)cvGetTickCount() - t; imshow("image", img);
printf( "%d. %.1f\n", fr, t/(cvGetTickFrequency()*1000.) ); imshow("foreground mask", fgmask);
cvShowImage("BG", bg_model->background); char k = (char)waitKey(30);
cvShowImage("FG", bg_model->foreground);
char k = cvWaitKey(5);
if( k == 27 ) break; if( k == 27 ) break;
if( k == ' ' ) if( k == ' ' )
{ {
...@@ -70,9 +60,5 @@ int main(int argc, char** argv) ...@@ -70,9 +60,5 @@ int main(int argc, char** argv)
} }
} }
cvReleaseBGStatModel( &bg_model );
cvReleaseCapture(&cap);
return 0; return 0;
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment