71473d124a5b33d14f12c0167a8cff6d57f51b39
[platform/upstream/opencv.git] / samples / cpp / bgfg_segm.cpp
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"
6 #include <iostream>
7
8 using namespace std;
9 using namespace cv;
10
11 int main(int argc, const char** argv)
12 {
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"))
20     {
21         parser.printMessage();
22         return 0;
23     }
24     bool useCamera = parser.has("camera");
25     String file = parser.get<String>("file_name");
26     String method = parser.get<String>("method");
27     if (!parser.check())
28     {
29         parser.printErrors();
30         return 1;
31     }
32
33     VideoCapture cap;
34     if (useCamera)
35         cap.open(0);
36     else
37         cap.open(file.c_str());
38     if (!cap.isOpened())
39     {
40         cout << "Can not open video stream: '" << (useCamera ? "<camera 0>" : file) << "'" << endl;
41         return 2;
42     }
43
44     Ptr<BackgroundSubtractor> model;
45     if (method == "knn")
46         model = createBackgroundSubtractorKNN();
47     else if (method == "mog2")
48         model = createBackgroundSubtractorMOG2();
49     if (!model)
50     {
51         cout << "Can not create background model using provided method: '" << method << "'" << endl;
52         return 3;
53     }
54
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;
60
61     Mat inputFrame, frame, foregroundMask, foreground, background;
62     for (;;)
63     {
64         // prepare input frame
65         cap >> inputFrame;
66         if (inputFrame.empty())
67         {
68             cout << "Finished reading: empty frame" << endl;
69             break;
70         }
71         const Size scaledSize(640, 640 * inputFrame.rows / inputFrame.cols);
72         resize(inputFrame, frame, scaledSize, 0, 0, INTER_LINEAR);
73
74         // pass the frame to background model
75         model->apply(frame, foregroundMask, doUpdateModel ? -1 : 0);
76
77         // show processed frame
78         imshow("image", frame);
79
80         // show foreground image and mask (with optional smoothing)
81         if (doSmoothMask)
82         {
83             GaussianBlur(foregroundMask, foregroundMask, Size(11, 11), 3.5, 3.5);
84             threshold(foregroundMask, foregroundMask, 10, 255, THRESH_BINARY);
85         }
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);
92
93         // show background image
94         model->getBackgroundImage(background);
95         if (!background.empty())
96             imshow("mean background image", background );
97
98         // interact with user
99         const char key = (char)waitKey(30);
100         if (key == 27 || key == 'q') // ESC
101         {
102             cout << "Exit requested" << endl;
103             break;
104         }
105         else if (key == ' ')
106         {
107             doUpdateModel = !doUpdateModel;
108             cout << "Toggle background update: " << (doUpdateModel ? "ON" : "OFF") << endl;
109         }
110         else if (key == 's')
111         {
112             doSmoothMask = !doSmoothMask;
113             cout << "Toggle foreground mask smoothing: " << (doSmoothMask ? "ON" : "OFF") << endl;
114         }
115     }
116     return 0;
117 }