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
4
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"
10 #include <iostream>
11
12 using namespace std;
13 using namespace cv;
14
main(int argc,const char ** argv)15 int main(int argc, const char** argv)
16 {
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"))
24 {
25 parser.printMessage();
26 return 0;
27 }
28 int camera = parser.get<int>("camera");
29 String file = parser.get<String>("file_name");
30 String method = parser.get<String>("method");
31 if (!parser.check())
32 {
33 parser.printErrors();
34 return 1;
35 }
36
37 VideoCapture cap;
38 if (file.empty())
39 cap.open(camera);
40 else
41 {
42 file = samples::findFileOrKeep(file); // ignore gstreamer pipelines
43 cap.open(file.c_str());
44 }
45 if (!cap.isOpened())
46 {
47 cout << "Can not open video stream: '" << (file.empty() ? "<camera>" : file) << "'" << endl;
48 return 2;
49 }
50
51 Ptr<BackgroundSubtractor> model;
52 if (method == "knn")
53 model = createBackgroundSubtractorKNN();
54 else if (method == "mog2")
55 model = createBackgroundSubtractorMOG2();
56 if (!model)
57 {
58 cout << "Can not create background model using provided method: '" << method << "'" << endl;
59 return 3;
60 }
61
62 cout << "Press <space> to toggle background model update" << endl;
63 cout << "Press 's' to toggle foreground mask smoothing" << endl;
64 cout << "Press ESC or 'q' to exit" << endl;
65 bool doUpdateModel = true;
66 bool doSmoothMask = false;
67
68 Mat inputFrame, frame, foregroundMask, foreground, background;
69 for (;;)
70 {
71 // prepare input frame
72 cap >> inputFrame;
73 if (inputFrame.empty())
74 {
75 cout << "Finished reading: empty frame" << endl;
76 break;
77 }
78 const Size scaledSize(640, 640 * inputFrame.rows / inputFrame.cols);
79 resize(inputFrame, frame, scaledSize, 0, 0, INTER_LINEAR);
80
81 // pass the frame to background model
82 model->apply(frame, foregroundMask, doUpdateModel ? -1 : 0);
83
84 // show processed frame
85 imshow("image", frame);
86
87 // show foreground image and mask (with optional smoothing)
88 if (doSmoothMask)
89 {
90 GaussianBlur(foregroundMask, foregroundMask, Size(11, 11), 3.5, 3.5);
91 threshold(foregroundMask, foregroundMask, 10, 255, THRESH_BINARY);
92 }
93 if (foreground.empty())
94 foreground.create(scaledSize, frame.type());
95 foreground = Scalar::all(0);
96 frame.copyTo(foreground, foregroundMask);
97 imshow("foreground mask", foregroundMask);
98 imshow("foreground image", foreground);
99
100 // show background image
101 model->getBackgroundImage(background);
102 if (!background.empty())
103 imshow("mean background image", background );
104
105 // interact with user
106 const char key = (char)waitKey(30);
107 if (key == 27 || key == 'q') // ESC
108 {
109 cout << "Exit requested" << endl;
110 break;
111 }
112 else if (key == ' ')
113 {
114 doUpdateModel = !doUpdateModel;
115 cout << "Toggle background update: " << (doUpdateModel ? "ON" : "OFF") << endl;
116 }
117 else if (key == 's')
118 {
119 doSmoothMask = !doSmoothMask;
120 cout << "Toggle foreground mask smoothing: " << (doSmoothMask ? "ON" : "OFF") << endl;
121 }
122 }
123 return 0;
124 }
125