Minor refactoring in several C++ samples:
[platform/upstream/opencv.git] / samples / cpp / bgfg_segm.cpp
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
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         cap.open(file.c_str());
42     if (!cap.isOpened())
43     {
44         cout << "Can not open video stream: '" << (file.empty() ? "<camera>" : file) << "'" << endl;
45         return 2;
46     }
47
48     Ptr<BackgroundSubtractor> model;
49     if (method == "knn")
50         model = createBackgroundSubtractorKNN();
51     else if (method == "mog2")
52         model = createBackgroundSubtractorMOG2();
53     if (!model)
54     {
55         cout << "Can not create background model using provided method: '" << method << "'" << endl;
56         return 3;
57     }
58
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;
64
65     Mat inputFrame, frame, foregroundMask, foreground, background;
66     for (;;)
67     {
68         // prepare input frame
69         cap >> inputFrame;
70         if (inputFrame.empty())
71         {
72             cout << "Finished reading: empty frame" << endl;
73             break;
74         }
75         const Size scaledSize(640, 640 * inputFrame.rows / inputFrame.cols);
76         resize(inputFrame, frame, scaledSize, 0, 0, INTER_LINEAR);
77
78         // pass the frame to background model
79         model->apply(frame, foregroundMask, doUpdateModel ? -1 : 0);
80
81         // show processed frame
82         imshow("image", frame);
83
84         // show foreground image and mask (with optional smoothing)
85         if (doSmoothMask)
86         {
87             GaussianBlur(foregroundMask, foregroundMask, Size(11, 11), 3.5, 3.5);
88             threshold(foregroundMask, foregroundMask, 10, 255, THRESH_BINARY);
89         }
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);
96
97         // show background image
98         model->getBackgroundImage(background);
99         if (!background.empty())
100             imshow("mean background image", background );
101
102         // interact with user
103         const char key = (char)waitKey(30);
104         if (key == 27 || key == 'q') // ESC
105         {
106             cout << "Exit requested" << endl;
107             break;
108         }
109         else if (key == ' ')
110         {
111             doUpdateModel = !doUpdateModel;
112             cout << "Toggle background update: " << (doUpdateModel ? "ON" : "OFF") << endl;
113         }
114         else if (key == 's')
115         {
116             doSmoothMask = !doSmoothMask;
117             cout << "Toggle foreground mask smoothing: " << (doSmoothMask ? "ON" : "OFF") << endl;
118         }
119     }
120     return 0;
121 }