2011-05-21 20:32:34 +02:00
|
|
|
#include "opencv2/video/background_segm.hpp"
|
|
|
|
#include "opencv2/highgui/highgui.hpp"
|
2010-11-29 15:00:49 +01:00
|
|
|
#include <stdio.h>
|
2010-12-28 22:28:34 +01:00
|
|
|
|
|
|
|
using namespace cv;
|
|
|
|
|
2010-11-30 02:26:29 +01:00
|
|
|
void help()
|
|
|
|
{
|
2010-12-04 09:30:10 +01:00
|
|
|
printf("\nDo background segmentation, especially demonstrating the use of cvUpdateBGStatModel().\n"
|
2011-06-09 14:01:47 +02: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"
|
|
|
|
"Call:\n"
|
|
|
|
"./ bgfg_segm [file name -- if no name, read from camera]\n\n");
|
2010-11-30 02:26:29 +01:00
|
|
|
}
|
2010-11-29 15:00:49 +01:00
|
|
|
|
|
|
|
//this is a sample for foreground detection functions
|
2011-06-09 14:01:47 +02:00
|
|
|
int main(int argc, char** argv)
|
2010-11-29 15:00:49 +01:00
|
|
|
{
|
2010-12-28 22:28:34 +01:00
|
|
|
VideoCapture cap;
|
2010-11-29 15:00:49 +01:00
|
|
|
bool update_bg_model = true;
|
|
|
|
|
2011-06-09 14:01:47 +02:00
|
|
|
if( argc < 2 )
|
2010-12-28 22:28:34 +01:00
|
|
|
cap.open(0);
|
2010-11-29 15:00:49 +01:00
|
|
|
else
|
2011-06-09 14:01:47 +02:00
|
|
|
cap.open(argv[1]);
|
|
|
|
help();
|
|
|
|
|
2010-12-28 22:28:34 +01:00
|
|
|
if( !cap.isOpened() )
|
2010-11-29 15:00:49 +01:00
|
|
|
{
|
|
|
|
printf("can not open camera or video file\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-06-03 16:13:03 +02:00
|
|
|
namedWindow("image", CV_WINDOW_NORMAL);
|
|
|
|
namedWindow("foreground mask", CV_WINDOW_NORMAL);
|
2011-06-03 16:13:43 +02:00
|
|
|
namedWindow("foreground image", CV_WINDOW_NORMAL);
|
2011-06-03 16:13:03 +02:00
|
|
|
namedWindow("mean background image", CV_WINDOW_NORMAL);
|
2010-11-29 15:00:49 +01:00
|
|
|
|
2010-12-28 22:28:34 +01:00
|
|
|
BackgroundSubtractorMOG2 bg_model;
|
2011-06-03 16:13:03 +02:00
|
|
|
Mat img, fgmask, fgimg;
|
|
|
|
|
2010-12-28 22:28:34 +01:00
|
|
|
for(;;)
|
2010-11-29 15:00:49 +01:00
|
|
|
{
|
2010-12-28 22:28:34 +01:00
|
|
|
cap >> img;
|
|
|
|
|
|
|
|
if( img.empty() )
|
|
|
|
break;
|
2010-11-29 15:00:49 +01:00
|
|
|
|
2011-06-03 16:13:03 +02:00
|
|
|
if( fgimg.empty() )
|
|
|
|
fgimg.create(img.size(), img.type());
|
|
|
|
|
|
|
|
//update the model
|
2010-12-28 22:28:34 +01:00
|
|
|
bg_model(img, fgmask, update_bg_model ? -1 : 0);
|
2011-06-03 16:13:03 +02:00
|
|
|
|
|
|
|
fgimg = Scalar::all(0);
|
|
|
|
img.copyTo(fgimg, fgmask);
|
|
|
|
|
|
|
|
Mat bgimg;
|
|
|
|
bg_model.getBackgroundImage(bgimg);
|
|
|
|
|
2010-12-28 22:28:34 +01:00
|
|
|
imshow("image", img);
|
|
|
|
imshow("foreground mask", fgmask);
|
2011-06-03 16:13:03 +02:00
|
|
|
imshow("foreground image", fgimg);
|
|
|
|
if(!bgimg.empty())
|
|
|
|
imshow("mean background image", bgimg );
|
|
|
|
|
2010-12-28 22:28:34 +01:00
|
|
|
char k = (char)waitKey(30);
|
2010-11-29 15:00:49 +01:00
|
|
|
if( k == 27 ) break;
|
|
|
|
if( k == ' ' )
|
2010-11-30 02:26:29 +01:00
|
|
|
{
|
2010-11-29 15:00:49 +01:00
|
|
|
update_bg_model = !update_bg_model;
|
2010-11-30 02:26:29 +01:00
|
|
|
if(update_bg_model)
|
|
|
|
printf("Background update is on\n");
|
|
|
|
else
|
|
|
|
printf("Background update is off\n");
|
|
|
|
}
|
2010-11-29 15:00:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|