1 #include "opencv2/core.hpp"
2 #include "opencv2/imgproc.hpp"
3 #include "opencv2/video.hpp"
4 #include "opencv2/videoio.hpp"
5 #include "opencv2/highgui.hpp"
11 int main(int argc, const char** argv)
13 const String keys = "{c camera||use video stream from camera (default is NO)}"
14 "{fn file_name|../data/tree.avi|video file}"
15 "{m method|mog2|method: background subtraction algorithm ('knn', 'mog2')}"
16 "{h help||show help message}";
17 CommandLineParser parser(argc, argv, keys);
18 parser.about("This sample demonstrates background segmentation.");
19 if (parser.has("help"))
21 parser.printMessage();
24 bool useCamera = parser.has("camera");
25 String file = parser.get<String>("file_name");
26 String method = parser.get<String>("method");
37 cap.open(file.c_str());
40 cout << "Can not open video stream: '" << (useCamera ? "<camera 0>" : file) << "'" << endl;
44 Ptr<BackgroundSubtractor> model;
46 model = createBackgroundSubtractorKNN();
47 else if (method == "mog2")
48 model = createBackgroundSubtractorMOG2();
51 cout << "Can not create background model using provided method: '" << method << "'" << endl;
55 cout << "Press <space> to toggle background model update" << endl;
56 cout << "Press 's' to toggle foreground mask smoothing" << endl;
57 cout << "Press ESC or 'q' to exit" << endl;
58 bool doUpdateModel = true;
59 bool doSmoothMask = false;
61 Mat inputFrame, frame, foregroundMask, foreground, background;
64 // prepare input frame
66 if (inputFrame.empty())
68 cout << "Finished reading: empty frame" << endl;
71 const Size scaledSize(640, 640 * inputFrame.rows / inputFrame.cols);
72 resize(inputFrame, frame, scaledSize, 0, 0, INTER_LINEAR);
74 // pass the frame to background model
75 model->apply(frame, foregroundMask, doUpdateModel ? -1 : 0);
77 // show processed frame
78 imshow("image", frame);
80 // show foreground image and mask (with optional smoothing)
83 GaussianBlur(foregroundMask, foregroundMask, Size(11, 11), 3.5, 3.5);
84 threshold(foregroundMask, foregroundMask, 10, 255, THRESH_BINARY);
86 if (foreground.empty())
87 foreground.create(scaledSize, frame.type());
88 foreground = Scalar::all(0);
89 frame.copyTo(foreground, foregroundMask);
90 imshow("foreground mask", foregroundMask);
91 imshow("foreground image", foreground);
93 // show background image
94 model->getBackgroundImage(background);
95 if (!background.empty())
96 imshow("mean background image", background );
99 const char key = (char)waitKey(30);
100 if (key == 27 || key == 'q') // ESC
102 cout << "Exit requested" << endl;
107 doUpdateModel = !doUpdateModel;
108 cout << "Toggle background update: " << (doUpdateModel ? "ON" : "OFF") << endl;
112 doSmoothMask = !doSmoothMask;
113 cout << "Toggle foreground mask smoothing: " << (doSmoothMask ? "ON" : "OFF") << endl;