2013-03-15 00:10:54 +08:00
|
|
|
#include "opencv2/core.hpp"
|
|
|
|
#include <opencv2/core/utility.hpp>
|
|
|
|
#include "opencv2/imgproc.hpp"
|
2011-05-22 02:32:34 +08:00
|
|
|
#include "opencv2/video/background_segm.hpp"
|
2013-03-15 00:10:54 +08:00
|
|
|
#include "opencv2/highgui.hpp"
|
2010-11-29 22:00:49 +08:00
|
|
|
#include <stdio.h>
|
2010-12-29 05:28:34 +08:00
|
|
|
|
2011-08-10 16:56:27 +08:00
|
|
|
using namespace std;
|
2010-12-29 05:28:34 +08:00
|
|
|
using namespace cv;
|
|
|
|
|
2012-06-08 01:21:29 +08:00
|
|
|
static void help()
|
2010-11-30 09:26:29 +08:00
|
|
|
{
|
2010-12-04 16:30:10 +08:00
|
|
|
printf("\nDo background segmentation, especially demonstrating the use of cvUpdateBGStatModel().\n"
|
2011-06-09 20:01:47 +08:00
|
|
|
"Learns the background at the start and then segments.\n"
|
|
|
|
"Learning is togged by the space key. Will read from file or camera\n"
|
2011-08-10 16:56:27 +08:00
|
|
|
"Usage: \n"
|
|
|
|
" ./bgfg_segm [--camera]=<use camera, if this key is present>, [--file_name]=<path to movie file> \n\n");
|
2010-11-30 09:26:29 +08:00
|
|
|
}
|
2010-11-29 22:00:49 +08:00
|
|
|
|
2012-10-17 15:12:04 +08:00
|
|
|
const char* keys =
|
2011-08-10 16:56:27 +08:00
|
|
|
{
|
2012-09-07 17:24:48 +08:00
|
|
|
"{c camera | | use camera or not}"
|
|
|
|
"{fn file_name|tree.avi | movie file }"
|
2011-08-10 16:56:27 +08:00
|
|
|
};
|
|
|
|
|
2010-11-29 22:00:49 +08:00
|
|
|
//this is a sample for foreground detection functions
|
2011-08-10 16:56:27 +08:00
|
|
|
int main(int argc, const char** argv)
|
2010-11-29 22:00:49 +08:00
|
|
|
{
|
2012-10-17 15:12:04 +08:00
|
|
|
help();
|
2011-08-10 16:56:27 +08:00
|
|
|
|
2012-10-17 15:12:04 +08:00
|
|
|
CommandLineParser parser(argc, argv, keys);
|
2012-09-07 17:24:48 +08:00
|
|
|
bool useCamera = parser.has("camera");
|
2012-10-17 15:12:04 +08:00
|
|
|
string file = parser.get<string>("file_name");
|
2010-12-29 05:28:34 +08:00
|
|
|
VideoCapture cap;
|
2010-11-29 22:00:49 +08:00
|
|
|
bool update_bg_model = true;
|
|
|
|
|
2011-08-10 16:56:27 +08:00
|
|
|
if( useCamera )
|
2010-12-29 05:28:34 +08:00
|
|
|
cap.open(0);
|
2010-11-29 22:00:49 +08:00
|
|
|
else
|
2012-10-17 15:12:04 +08:00
|
|
|
cap.open(file.c_str());
|
2012-09-07 17:24:48 +08:00
|
|
|
|
|
|
|
parser.printMessage();
|
2011-08-10 16:56:27 +08:00
|
|
|
|
2010-12-29 05:28:34 +08:00
|
|
|
if( !cap.isOpened() )
|
2010-11-29 22:00:49 +08:00
|
|
|
{
|
|
|
|
printf("can not open camera or video file\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2013-04-08 02:45:38 +08:00
|
|
|
namedWindow("image", WINDOW_NORMAL);
|
|
|
|
namedWindow("foreground mask", WINDOW_NORMAL);
|
|
|
|
namedWindow("foreground image", WINDOW_NORMAL);
|
|
|
|
namedWindow("mean background image", WINDOW_NORMAL);
|
2010-11-29 22:00:49 +08:00
|
|
|
|
2013-03-20 23:51:49 +08:00
|
|
|
Ptr<BackgroundSubtractor> bg_model = createBackgroundSubtractorMOG2();
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2011-06-03 22:13:03 +08:00
|
|
|
Mat img, fgmask, fgimg;
|
|
|
|
|
2010-12-29 05:28:34 +08:00
|
|
|
for(;;)
|
2010-11-29 22:00:49 +08:00
|
|
|
{
|
2010-12-29 05:28:34 +08:00
|
|
|
cap >> img;
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2010-12-29 05:28:34 +08:00
|
|
|
if( img.empty() )
|
|
|
|
break;
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2012-04-30 22:33:52 +08:00
|
|
|
//cvtColor(_img, img, COLOR_BGR2GRAY);
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2011-06-03 22:13:03 +08:00
|
|
|
if( fgimg.empty() )
|
|
|
|
fgimg.create(img.size(), img.type());
|
|
|
|
|
|
|
|
//update the model
|
2013-03-20 23:51:49 +08:00
|
|
|
bg_model->apply(img, fgmask, update_bg_model ? -1 : 0);
|
2011-06-03 22:13:03 +08:00
|
|
|
|
|
|
|
fgimg = Scalar::all(0);
|
|
|
|
img.copyTo(fgimg, fgmask);
|
|
|
|
|
|
|
|
Mat bgimg;
|
2013-03-20 23:51:49 +08:00
|
|
|
bg_model->getBackgroundImage(bgimg);
|
2011-06-03 22:13:03 +08:00
|
|
|
|
2010-12-29 05:28:34 +08:00
|
|
|
imshow("image", img);
|
|
|
|
imshow("foreground mask", fgmask);
|
2011-06-03 22:13:03 +08:00
|
|
|
imshow("foreground image", fgimg);
|
|
|
|
if(!bgimg.empty())
|
|
|
|
imshow("mean background image", bgimg );
|
|
|
|
|
2010-12-29 05:28:34 +08:00
|
|
|
char k = (char)waitKey(30);
|
2010-11-29 22:00:49 +08:00
|
|
|
if( k == 27 ) break;
|
|
|
|
if( k == ' ' )
|
2010-11-30 09:26:29 +08:00
|
|
|
{
|
2010-11-29 22:00:49 +08:00
|
|
|
update_bg_model = !update_bg_model;
|
2010-11-30 09:26:29 +08:00
|
|
|
if(update_bg_model)
|
2012-10-17 15:12:04 +08:00
|
|
|
printf("Background update is on\n");
|
2010-11-30 09:26:29 +08:00
|
|
|
else
|
2012-10-17 15:12:04 +08:00
|
|
|
printf("Background update is off\n");
|
2010-11-30 09:26:29 +08:00
|
|
|
}
|
2010-11-29 22:00:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|