2012-06-25 20:48:54 +08:00
|
|
|
#include <iostream>
|
|
|
|
#include <string>
|
|
|
|
|
2013-03-15 00:10:54 +08:00
|
|
|
#include "opencv2/core.hpp"
|
|
|
|
#include "opencv2/core/utility.hpp"
|
2013-07-24 14:44:39 +08:00
|
|
|
#include "opencv2/cudabgsegm.hpp"
|
2013-07-23 17:19:54 +08:00
|
|
|
#include "opencv2/video.hpp"
|
2013-03-15 00:10:54 +08:00
|
|
|
#include "opencv2/highgui.hpp"
|
2012-06-25 20:48:54 +08:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace cv;
|
2013-08-28 19:45:13 +08:00
|
|
|
using namespace cv::cuda;
|
2012-06-25 20:48:54 +08:00
|
|
|
|
|
|
|
enum Method
|
|
|
|
{
|
|
|
|
MOG,
|
2012-06-26 18:38:15 +08:00
|
|
|
MOG2,
|
2012-06-25 20:48:54 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
int main(int argc, const char** argv)
|
|
|
|
{
|
|
|
|
cv::CommandLineParser cmd(argc, argv,
|
2016-10-25 17:41:03 +08:00
|
|
|
"{ c camera | | use camera }"
|
|
|
|
"{ f file | ../data/vtest.avi | input video file }"
|
2018-11-01 07:41:49 +08:00
|
|
|
"{ m method | mog | method (mog, mog2) }"
|
2016-10-25 17:41:03 +08:00
|
|
|
"{ h help | | print help message }");
|
2012-06-25 20:48:54 +08:00
|
|
|
|
2012-09-07 17:24:48 +08:00
|
|
|
if (cmd.has("help") || !cmd.check())
|
2012-06-25 20:48:54 +08:00
|
|
|
{
|
2012-09-07 17:24:48 +08:00
|
|
|
cmd.printMessage();
|
|
|
|
cmd.printErrors();
|
2012-06-25 20:48:54 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-09-07 17:24:48 +08:00
|
|
|
bool useCamera = cmd.has("camera");
|
2012-06-25 20:48:54 +08:00
|
|
|
string file = cmd.get<string>("file");
|
|
|
|
string method = cmd.get<string>("method");
|
|
|
|
|
2013-05-07 15:02:59 +08:00
|
|
|
if (method != "mog"
|
2018-11-01 07:41:49 +08:00
|
|
|
&& method != "mog2")
|
2012-06-25 20:48:54 +08:00
|
|
|
{
|
|
|
|
cerr << "Incorrect method" << endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-05-07 15:02:59 +08:00
|
|
|
Method m = method == "mog" ? MOG :
|
2013-03-15 18:09:39 +08:00
|
|
|
method == "mog2" ? MOG2 :
|
2018-11-01 07:41:49 +08:00
|
|
|
(Method)-1;
|
|
|
|
CV_Assert(m != (Method)-1);
|
2012-06-25 20:48:54 +08: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);
|
|
|
|
|
2013-08-28 19:45:13 +08:00
|
|
|
Ptr<BackgroundSubtractor> mog = cuda::createBackgroundSubtractorMOG();
|
|
|
|
Ptr<BackgroundSubtractor> mog2 = cuda::createBackgroundSubtractorMOG2();
|
2012-06-25 20:48:54 +08:00
|
|
|
|
|
|
|
GpuMat d_fgmask;
|
|
|
|
GpuMat d_fgimg;
|
|
|
|
GpuMat d_bgimg;
|
|
|
|
|
|
|
|
Mat fgmask;
|
|
|
|
Mat fgimg;
|
|
|
|
Mat bgimg;
|
|
|
|
|
|
|
|
switch (m)
|
|
|
|
{
|
|
|
|
case MOG:
|
2013-05-06 20:52:06 +08:00
|
|
|
mog->apply(d_frame, d_fgmask, 0.01);
|
2012-06-25 20:48:54 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MOG2:
|
2013-05-06 21:41:13 +08:00
|
|
|
mog2->apply(d_frame, d_fgmask);
|
2012-06-25 20:48:54 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
namedWindow("image", WINDOW_NORMAL);
|
|
|
|
namedWindow("foreground mask", WINDOW_NORMAL);
|
|
|
|
namedWindow("foreground image", WINDOW_NORMAL);
|
2018-11-01 07:41:49 +08:00
|
|
|
namedWindow("mean background image", WINDOW_NORMAL);
|
2012-06-25 20:48:54 +08:00
|
|
|
|
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
cap >> frame;
|
|
|
|
if (frame.empty())
|
|
|
|
break;
|
|
|
|
d_frame.upload(frame);
|
|
|
|
|
2012-08-09 16:59:18 +08:00
|
|
|
int64 start = cv::getTickCount();
|
|
|
|
|
2012-06-25 20:48:54 +08:00
|
|
|
//update the model
|
|
|
|
switch (m)
|
|
|
|
{
|
|
|
|
case MOG:
|
2013-05-06 20:52:06 +08:00
|
|
|
mog->apply(d_frame, d_fgmask, 0.01);
|
|
|
|
mog->getBackgroundImage(d_bgimg);
|
2012-06-25 20:48:54 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MOG2:
|
2013-05-06 21:41:13 +08:00
|
|
|
mog2->apply(d_frame, d_fgmask);
|
|
|
|
mog2->getBackgroundImage(d_bgimg);
|
2012-06-25 20:48:54 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-08-09 16:59:18 +08:00
|
|
|
double fps = cv::getTickFrequency() / (cv::getTickCount() - start);
|
|
|
|
std::cout << "FPS : " << fps << std::endl;
|
|
|
|
|
2013-10-19 16:40:10 +08:00
|
|
|
d_fgimg.create(d_frame.size(), d_frame.type());
|
2012-06-25 20:48:54 +08: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);
|
|
|
|
|
2016-08-12 21:11:30 +08:00
|
|
|
char key = (char)waitKey(30);
|
2012-06-25 20:48:54 +08:00
|
|
|
if (key == 27)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|