2018-02-19 23:53:17 +08:00
|
|
|
// This file is part of OpenCV project.
|
|
|
|
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
|
|
|
// of this distribution and at http://opencv.org/license.html
|
|
|
|
|
2013-03-15 00:10:54 +08:00
|
|
|
#include "opencv2/core.hpp"
|
|
|
|
#include "opencv2/imgproc.hpp"
|
2018-02-17 20:04:14 +08:00
|
|
|
#include "opencv2/video.hpp"
|
2014-07-10 22:27:32 +08:00
|
|
|
#include "opencv2/videoio.hpp"
|
2013-03-15 00:10:54 +08:00
|
|
|
#include "opencv2/highgui.hpp"
|
2018-02-17 20:04:14 +08:00
|
|
|
#include <iostream>
|
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;
|
|
|
|
|
2011-08-10 16:56:27 +08:00
|
|
|
int main(int argc, const char** argv)
|
2010-11-29 22:00:49 +08:00
|
|
|
{
|
2018-02-19 23:53:17 +08:00
|
|
|
const String keys = "{c camera | 0 | use video stream from camera (device index starting from 0) }"
|
|
|
|
"{fn file_name | | use video file as input }"
|
|
|
|
"{m method | mog2 | method: background subtraction algorithm ('knn', 'mog2')}"
|
|
|
|
"{h help | | show help message}";
|
2012-10-17 15:12:04 +08:00
|
|
|
CommandLineParser parser(argc, argv, keys);
|
2018-02-17 20:04:14 +08:00
|
|
|
parser.about("This sample demonstrates background segmentation.");
|
|
|
|
if (parser.has("help"))
|
|
|
|
{
|
|
|
|
parser.printMessage();
|
|
|
|
return 0;
|
|
|
|
}
|
2018-02-19 23:53:17 +08:00
|
|
|
int camera = parser.get<int>("camera");
|
2018-02-17 20:04:14 +08:00
|
|
|
String file = parser.get<String>("file_name");
|
|
|
|
String method = parser.get<String>("method");
|
|
|
|
if (!parser.check())
|
|
|
|
{
|
|
|
|
parser.printErrors();
|
|
|
|
return 1;
|
|
|
|
}
|
2010-11-29 22:00:49 +08:00
|
|
|
|
2018-02-17 20:04:14 +08:00
|
|
|
VideoCapture cap;
|
2018-02-19 23:53:17 +08:00
|
|
|
if (file.empty())
|
|
|
|
cap.open(camera);
|
2010-11-29 22:00:49 +08:00
|
|
|
else
|
2018-10-31 20:48:56 +08:00
|
|
|
{
|
|
|
|
file = samples::findFileOrKeep(file); // ignore gstreamer pipelines
|
2012-10-17 15:12:04 +08:00
|
|
|
cap.open(file.c_str());
|
2018-10-31 20:48:56 +08:00
|
|
|
}
|
2018-02-17 20:04:14 +08:00
|
|
|
if (!cap.isOpened())
|
2010-11-29 22:00:49 +08:00
|
|
|
{
|
2018-02-19 23:53:17 +08:00
|
|
|
cout << "Can not open video stream: '" << (file.empty() ? "<camera>" : file) << "'" << endl;
|
2018-02-17 20:04:14 +08:00
|
|
|
return 2;
|
2010-11-29 22:00:49 +08:00
|
|
|
}
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2018-02-17 20:04:14 +08:00
|
|
|
Ptr<BackgroundSubtractor> model;
|
|
|
|
if (method == "knn")
|
|
|
|
model = createBackgroundSubtractorKNN();
|
|
|
|
else if (method == "mog2")
|
|
|
|
model = createBackgroundSubtractorMOG2();
|
|
|
|
if (!model)
|
|
|
|
{
|
|
|
|
cout << "Can not create background model using provided method: '" << method << "'" << endl;
|
|
|
|
return 3;
|
|
|
|
}
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2018-02-17 20:04:14 +08:00
|
|
|
cout << "Press <space> to toggle background model update" << endl;
|
|
|
|
cout << "Press 's' to toggle foreground mask smoothing" << endl;
|
|
|
|
cout << "Press ESC or 'q' to exit" << endl;
|
|
|
|
bool doUpdateModel = true;
|
|
|
|
bool doSmoothMask = false;
|
2011-06-03 22:13:03 +08:00
|
|
|
|
2018-02-17 20:04:14 +08:00
|
|
|
Mat inputFrame, frame, foregroundMask, foreground, background;
|
|
|
|
for (;;)
|
2010-11-29 22:00:49 +08:00
|
|
|
{
|
2018-02-17 20:04:14 +08:00
|
|
|
// prepare input frame
|
|
|
|
cap >> inputFrame;
|
|
|
|
if (inputFrame.empty())
|
|
|
|
{
|
|
|
|
cout << "Finished reading: empty frame" << endl;
|
2010-12-29 05:28:34 +08:00
|
|
|
break;
|
2018-02-17 20:04:14 +08:00
|
|
|
}
|
|
|
|
const Size scaledSize(640, 640 * inputFrame.rows / inputFrame.cols);
|
|
|
|
resize(inputFrame, frame, scaledSize, 0, 0, INTER_LINEAR);
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2018-02-17 20:04:14 +08:00
|
|
|
// pass the frame to background model
|
|
|
|
model->apply(frame, foregroundMask, doUpdateModel ? -1 : 0);
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2018-02-17 20:04:14 +08:00
|
|
|
// show processed frame
|
|
|
|
imshow("image", frame);
|
2011-06-03 22:13:03 +08:00
|
|
|
|
2018-02-17 20:04:14 +08:00
|
|
|
// show foreground image and mask (with optional smoothing)
|
|
|
|
if (doSmoothMask)
|
2014-08-11 03:24:16 +08:00
|
|
|
{
|
2018-02-17 20:04:14 +08:00
|
|
|
GaussianBlur(foregroundMask, foregroundMask, Size(11, 11), 3.5, 3.5);
|
|
|
|
threshold(foregroundMask, foregroundMask, 10, 255, THRESH_BINARY);
|
2014-08-11 03:24:16 +08:00
|
|
|
}
|
2018-02-17 20:04:14 +08:00
|
|
|
if (foreground.empty())
|
|
|
|
foreground.create(scaledSize, frame.type());
|
|
|
|
foreground = Scalar::all(0);
|
|
|
|
frame.copyTo(foreground, foregroundMask);
|
|
|
|
imshow("foreground mask", foregroundMask);
|
|
|
|
imshow("foreground image", foreground);
|
|
|
|
|
|
|
|
// show background image
|
|
|
|
model->getBackgroundImage(background);
|
|
|
|
if (!background.empty())
|
|
|
|
imshow("mean background image", background );
|
|
|
|
|
|
|
|
// interact with user
|
|
|
|
const char key = (char)waitKey(30);
|
|
|
|
if (key == 27 || key == 'q') // ESC
|
|
|
|
{
|
|
|
|
cout << "Exit requested" << endl;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (key == ' ')
|
2010-11-30 09:26:29 +08:00
|
|
|
{
|
2018-02-17 20:04:14 +08:00
|
|
|
doUpdateModel = !doUpdateModel;
|
|
|
|
cout << "Toggle background update: " << (doUpdateModel ? "ON" : "OFF") << endl;
|
|
|
|
}
|
|
|
|
else if (key == 's')
|
|
|
|
{
|
|
|
|
doSmoothMask = !doSmoothMask;
|
|
|
|
cout << "Toggle foreground mask smoothing: " << (doSmoothMask ? "ON" : "OFF") << endl;
|
2010-11-30 09:26:29 +08:00
|
|
|
}
|
2010-11-29 22:00:49 +08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|