1 // This file is part of OpenCV project.
2 // It is subject to the license terms in the LICENSE file found in the top-level directory
3 // of this distribution and at http://opencv.org/license.html
5 #include "opencv2/core.hpp"
6 #include "opencv2/imgproc.hpp"
7 #include "opencv2/video.hpp"
8 #include "opencv2/videoio.hpp"
9 #include "opencv2/highgui.hpp"
15 int main(int argc, const char** argv)
17 const String keys = "{c camera | 0 | use video stream from camera (device index starting from 0) }"
18 "{fn file_name | | use video file as input }"
19 "{m method | mog2 | method: background subtraction algorithm ('knn', 'mog2')}"
20 "{h help | | show help message}";
21 CommandLineParser parser(argc, argv, keys);
22 parser.about("This sample demonstrates background segmentation.");
23 if (parser.has("help"))
25 parser.printMessage();
28 int camera = parser.get<int>("camera");
29 String file = parser.get<String>("file_name");
30 String method = parser.get<String>("method");
41 cap.open(file.c_str());
44 cout << "Can not open video stream: '" << (file.empty() ? "<camera>" : file) << "'" << endl;
48 Ptr<BackgroundSubtractor> model;
50 model = createBackgroundSubtractorKNN();
51 else if (method == "mog2")
52 model = createBackgroundSubtractorMOG2();
55 cout << "Can not create background model using provided method: '" << method << "'" << endl;
59 cout << "Press <space> to toggle background model update" << endl;
60 cout << "Press 's' to toggle foreground mask smoothing" << endl;
61 cout << "Press ESC or 'q' to exit" << endl;
62 bool doUpdateModel = true;
63 bool doSmoothMask = false;
65 Mat inputFrame, frame, foregroundMask, foreground, background;
68 // prepare input frame
70 if (inputFrame.empty())
72 cout << "Finished reading: empty frame" << endl;
75 const Size scaledSize(640, 640 * inputFrame.rows / inputFrame.cols);
76 resize(inputFrame, frame, scaledSize, 0, 0, INTER_LINEAR);
78 // pass the frame to background model
79 model->apply(frame, foregroundMask, doUpdateModel ? -1 : 0);
81 // show processed frame
82 imshow("image", frame);
84 // show foreground image and mask (with optional smoothing)
87 GaussianBlur(foregroundMask, foregroundMask, Size(11, 11), 3.5, 3.5);
88 threshold(foregroundMask, foregroundMask, 10, 255, THRESH_BINARY);
90 if (foreground.empty())
91 foreground.create(scaledSize, frame.type());
92 foreground = Scalar::all(0);
93 frame.copyTo(foreground, foregroundMask);
94 imshow("foreground mask", foregroundMask);
95 imshow("foreground image", foreground);
97 // show background image
98 model->getBackgroundImage(background);
99 if (!background.empty())
100 imshow("mean background image", background );
102 // interact with user
103 const char key = (char)waitKey(30);
104 if (key == 27 || key == 'q') // ESC
106 cout << "Exit requested" << endl;
111 doUpdateModel = !doUpdateModel;
112 cout << "Toggle background update: " << (doUpdateModel ? "ON" : "OFF") << endl;
116 doSmoothMask = !doSmoothMask;
117 cout << "Toggle foreground mask smoothing: " << (doSmoothMask ? "ON" : "OFF") << endl;