From e26ac53589d35f06d313ce4c336063674e4ae0a3 Mon Sep 17 00:00:00 2001 From: Vadim Pisarevsky Date: Tue, 28 Dec 2010 21:28:34 +0000 Subject: [PATCH] some more fixes in background/foreground subtraction; converted bgfg_segm.cpp sample to C++ --- .../include/opencv2/video/background_segm.hpp | 4 +- modules/video/src/bgfg_gaussmix2.cpp | 38 +++++++-------- samples/cpp/bgfg_segm.cpp | 56 ++++++++-------------- 3 files changed, 42 insertions(+), 56 deletions(-) diff --git a/modules/video/include/opencv2/video/background_segm.hpp b/modules/video/include/opencv2/video/background_segm.hpp index fca2098..c362bd1 100644 --- a/modules/video/include/opencv2/video/background_segm.hpp +++ b/modules/video/include/opencv2/video/background_segm.hpp @@ -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; diff --git a/modules/video/src/bgfg_gaussmix2.cpp b/modules/video/src/bgfg_gaussmix2.cpp index 9f92eae..e216a89 100644 --- a/modules/video/src/bgfg_gaussmix2.cpp +++ b/modules/video/src/bgfg_gaussmix2.cpp @@ -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 > 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(), 1); + //foreground filtering: filter out small regions + morphologyEx(fgmask, fgmask, CV_MOP_OPEN, Mat()); + morphologyEx(fgmask, fgmask, CV_MOP_CLOSE, Mat()); + + vector > 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(), 1); + } } fgmask.copyTo(fgmask0); diff --git a/samples/cpp/bgfg_segm.cpp b/samples/cpp/bgfg_segm.cpp index e04e390..8eadfcf 100644 --- a/samples/cpp/bgfg_segm.cpp +++ b/samples/cpp/bgfg_segm.cpp @@ -1,7 +1,9 @@ #include #include - #include + +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; - } - - cvNamedWindow("BG", 1); - cvNamedWindow("FG", 1); + namedWindow("BG", 1); + namedWindow("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; + + if( img.empty() ) + break; - 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); + 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; } -- 2.7.4