2012-06-25 14:48:54 +02:00
|
|
|
#include <iostream>
|
|
|
|
#include <string>
|
|
|
|
|
2013-03-14 17:10:54 +01:00
|
|
|
#include "opencv2/core.hpp"
|
|
|
|
#include "opencv2/core/utility.hpp"
|
|
|
|
#include "opencv2/gpu.hpp"
|
|
|
|
#include "opencv2/highgui.hpp"
|
2012-06-25 14:48:54 +02:00
|
|
|
|
2013-06-04 11:32:35 +02:00
|
|
|
#include "opencv2/opencv_modules.hpp"
|
|
|
|
|
|
|
|
#ifdef HAVE_OPENCV_NONFREE
|
|
|
|
# include "opencv2/nonfree/gpu.hpp"
|
|
|
|
#endif
|
|
|
|
|
2012-06-25 14:48:54 +02:00
|
|
|
using namespace std;
|
|
|
|
using namespace cv;
|
|
|
|
using namespace cv::gpu;
|
|
|
|
|
|
|
|
enum Method
|
|
|
|
{
|
|
|
|
FGD_STAT,
|
|
|
|
MOG,
|
2012-06-26 12:38:15 +02:00
|
|
|
MOG2,
|
2012-08-09 10:59:18 +02:00
|
|
|
GMG
|
2012-06-25 14:48:54 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
int main(int argc, const char** argv)
|
|
|
|
{
|
|
|
|
cv::CommandLineParser cmd(argc, argv,
|
2012-09-07 11:24:48 +02:00
|
|
|
"{ c camera | | use camera }"
|
|
|
|
"{ f file | 768x576.avi | input video file }"
|
2013-05-16 11:47:24 +02:00
|
|
|
"{ m method | mog | method (fgd, mog, mog2, gmg) }"
|
2012-09-07 11:24:48 +02:00
|
|
|
"{ h help | | print help message }");
|
2012-06-25 14:48:54 +02:00
|
|
|
|
2012-09-07 11:24:48 +02:00
|
|
|
if (cmd.has("help") || !cmd.check())
|
2012-06-25 14:48:54 +02:00
|
|
|
{
|
2012-09-07 11:24:48 +02:00
|
|
|
cmd.printMessage();
|
|
|
|
cmd.printErrors();
|
2012-06-25 14:48:54 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-09-07 11:24:48 +02:00
|
|
|
bool useCamera = cmd.has("camera");
|
2012-06-25 14:48:54 +02:00
|
|
|
string file = cmd.get<string>("file");
|
|
|
|
string method = cmd.get<string>("method");
|
|
|
|
|
2013-03-15 11:09:39 +01:00
|
|
|
if (method != "fgd"
|
|
|
|
&& method != "mog"
|
|
|
|
&& method != "mog2"
|
|
|
|
&& method != "gmg")
|
2012-06-25 14:48:54 +02:00
|
|
|
{
|
|
|
|
cerr << "Incorrect method" << endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-03-15 11:09:39 +01:00
|
|
|
Method m = method == "fgd" ? FGD_STAT :
|
|
|
|
method == "mog" ? MOG :
|
|
|
|
method == "mog2" ? MOG2 :
|
|
|
|
GMG;
|
2012-06-25 14:48:54 +02:00
|
|
|
|
|
|
|
VideoCapture cap;
|
|
|
|
|
|
|
|
if (useCamera)
|
|
|
|
cap.open(0);
|
|
|
|
else
|
|
|
|
cap.open(file);
|
|
|
|
|
|
|
|
if (!cap.isOpened())
|
|
|
|
{
|
|
|
|
cerr << "can not open camera or video file" << endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
Mat frame;
|
|
|
|
cap >> frame;
|
|
|
|
|
|
|
|
GpuMat d_frame(frame);
|
|
|
|
|
|
|
|
FGDStatModel fgd_stat;
|
2013-05-06 14:52:06 +02:00
|
|
|
cv::Ptr<cv::BackgroundSubtractorMOG> mog = cv::gpu::createBackgroundSubtractorMOG();
|
2012-06-25 14:48:54 +02:00
|
|
|
MOG2_GPU mog2;
|
2012-08-09 10:59:18 +02:00
|
|
|
GMG_GPU gmg;
|
|
|
|
gmg.numInitializationFrames = 40;
|
2012-06-25 14:48:54 +02:00
|
|
|
|
|
|
|
GpuMat d_fgmask;
|
|
|
|
GpuMat d_fgimg;
|
|
|
|
GpuMat d_bgimg;
|
|
|
|
|
|
|
|
Mat fgmask;
|
|
|
|
Mat fgimg;
|
|
|
|
Mat bgimg;
|
|
|
|
|
|
|
|
switch (m)
|
|
|
|
{
|
|
|
|
case FGD_STAT:
|
|
|
|
fgd_stat.create(d_frame);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MOG:
|
2013-05-06 14:52:06 +02:00
|
|
|
mog->apply(d_frame, d_fgmask, 0.01);
|
2012-06-25 14:48:54 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MOG2:
|
|
|
|
mog2(d_frame, d_fgmask);
|
|
|
|
break;
|
2012-06-26 12:38:15 +02:00
|
|
|
|
2012-08-09 10:59:18 +02:00
|
|
|
case GMG:
|
|
|
|
gmg.initialize(d_frame.size());
|
|
|
|
break;
|
2012-06-25 14:48:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
namedWindow("image", WINDOW_NORMAL);
|
|
|
|
namedWindow("foreground mask", WINDOW_NORMAL);
|
|
|
|
namedWindow("foreground image", WINDOW_NORMAL);
|
2013-05-16 11:47:24 +02:00
|
|
|
if (m != GMG)
|
2013-03-15 11:09:39 +01:00
|
|
|
{
|
2012-06-26 12:38:15 +02:00
|
|
|
namedWindow("mean background image", WINDOW_NORMAL);
|
2013-03-15 11:09:39 +01:00
|
|
|
}
|
2012-06-25 14:48:54 +02:00
|
|
|
|
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
cap >> frame;
|
|
|
|
if (frame.empty())
|
|
|
|
break;
|
|
|
|
d_frame.upload(frame);
|
|
|
|
|
2012-08-09 10:59:18 +02:00
|
|
|
int64 start = cv::getTickCount();
|
|
|
|
|
2012-06-25 14:48:54 +02:00
|
|
|
//update the model
|
|
|
|
switch (m)
|
|
|
|
{
|
|
|
|
case FGD_STAT:
|
|
|
|
fgd_stat.update(d_frame);
|
|
|
|
d_fgmask = fgd_stat.foreground;
|
|
|
|
d_bgimg = fgd_stat.background;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MOG:
|
2013-05-06 14:52:06 +02:00
|
|
|
mog->apply(d_frame, d_fgmask, 0.01);
|
|
|
|
mog->getBackgroundImage(d_bgimg);
|
2012-06-25 14:48:54 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MOG2:
|
|
|
|
mog2(d_frame, d_fgmask);
|
|
|
|
mog2.getBackgroundImage(d_bgimg);
|
|
|
|
break;
|
2012-06-26 12:38:15 +02:00
|
|
|
|
2012-08-09 10:59:18 +02:00
|
|
|
case GMG:
|
|
|
|
gmg(d_frame, d_fgmask);
|
|
|
|
break;
|
2012-06-25 14:48:54 +02:00
|
|
|
}
|
|
|
|
|
2012-08-09 10:59:18 +02:00
|
|
|
double fps = cv::getTickFrequency() / (cv::getTickCount() - start);
|
|
|
|
std::cout << "FPS : " << fps << std::endl;
|
|
|
|
|
2012-06-25 14:48:54 +02:00
|
|
|
d_fgimg.setTo(Scalar::all(0));
|
|
|
|
d_frame.copyTo(d_fgimg, d_fgmask);
|
|
|
|
|
|
|
|
d_fgmask.download(fgmask);
|
|
|
|
d_fgimg.download(fgimg);
|
|
|
|
if (!d_bgimg.empty())
|
|
|
|
d_bgimg.download(bgimg);
|
|
|
|
|
|
|
|
imshow("image", frame);
|
|
|
|
imshow("foreground mask", fgmask);
|
|
|
|
imshow("foreground image", fgimg);
|
|
|
|
if (!bgimg.empty())
|
|
|
|
imshow("mean background image", bgimg);
|
|
|
|
|
2012-08-22 01:59:05 +02:00
|
|
|
int key = waitKey(30);
|
2012-06-25 14:48:54 +02:00
|
|
|
if (key == 27)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|