1 |
|
2 |
|
3 |
|
4 | #include "stdafx.h"
|
5 |
|
6 |
|
7 | #include <opencv.hpp>
|
8 |
|
9 | #ifdef _DEBUG
|
10 | #pragma comment (lib, "opencv_world300d.lib")
|
11 |
|
12 | #else
|
13 | #pragma comment (lib, "opencv_world300.lib")
|
14 | #endif
|
15 |
|
16 | #include <iostream>
|
17 | #include <string>
|
18 |
|
19 | #include "opencv2/core/core.hpp"
|
20 | #include "opencv2/core/ocl.hpp"
|
21 | #include "opencv2/highgui/highgui.hpp"
|
22 |
|
23 | using namespace std;
|
24 | using namespace cv;
|
25 | using namespace cv::ocl;
|
26 |
|
27 | #define M_MOG 1
|
28 | #define M_MOG2 2
|
29 |
|
30 | int main(int argc, const char** argv)
|
31 | {
|
32 | |
33 | "{ c | camera | false | use camera }"
|
34 | "{ f | file | 768x576.avi | input video file }"
|
35 | "{ m | method | mog | method (mog, mog2) }"
|
36 | "{ h | help | false | print help message }");
|
37 |
|
38 | if (cmd.get<bool>("help"))
|
39 | {
|
40 | cout << "Usage : bgfg_segm [options]" << endl;
|
41 | cout << "Available options:" << endl;
|
42 | cmd.printParams();
|
43 | return EXIT_SUCCESS;
|
44 | }*/
|
45 |
|
46 | bool useCamera = false;
|
47 | string file = "..\\768x576.avi";
|
48 | string method = "mog2";
|
49 |
|
50 | if (method != "knn" && method != "mog2")
|
51 | {
|
52 | cerr << "Incorrect method" << endl;
|
53 | return EXIT_FAILURE;
|
54 | }
|
55 |
|
56 | VideoCapture cap;
|
57 | if (useCamera)
|
58 | cap.open(0);
|
59 | else
|
60 | cap.open(file);
|
61 |
|
62 | if (!cap.isOpened())
|
63 | {
|
64 | cout << "can not open camera or video file" << endl;
|
65 | return EXIT_FAILURE;
|
66 | }
|
67 |
|
68 | |
69 | createBackgroundSubtractorKNN().dynamicCast<BackgroundSubtractor>() :
|
70 | createBackgroundSubtractorMOG2().dynamicCast<BackgroundSubtractor>();*/
|
71 |
|
72 | Ptr<BackgroundSubtractorMOG2> bg_model = createBackgroundSubtractorMOG2(100, 16, false);
|
73 |
|
74 |
|
75 | Mat img0, img, fgmask, fgimg;
|
76 | bool update_bg_model = true;
|
77 | bool smoothMask = false;
|
78 | int64 start = cv::getTickCount();
|
79 |
|
80 | for(;;)
|
81 | {
|
82 | cap >> img0;
|
83 |
|
84 | if( img0.empty() )
|
85 | break;
|
86 |
|
87 | img = img0;
|
88 |
|
89 |
|
90 | if( fgimg.empty() )
|
91 | fgimg.create(img.size(), img.type());
|
92 |
|
93 |
|
94 | bg_model->apply(img, fgmask);
|
95 | if (smoothMask)
|
96 | {
|
97 | GaussianBlur(fgmask, fgmask, Size(11, 11), 3.5, 3.5);
|
98 | threshold(fgmask, fgmask, 10, 255, THRESH_BINARY);
|
99 | }
|
100 |
|
101 | fgimg = Scalar::all(0);
|
102 | img.copyTo(fgimg, fgmask);
|
103 |
|
104 | Mat bgimg;
|
105 | bg_model->getBackgroundImage(bgimg);
|
106 |
|
107 | imshow("image", img);
|
108 | imshow("foreground mask", fgmask);
|
109 | imshow("foreground image", fgimg);
|
110 | if (!bgimg.empty())
|
111 | imshow("mean background image", bgimg);
|
112 |
|
113 | char k = (char)waitKey(30);
|
114 | if (k == 27) break;
|
115 | if (k == ' ')
|
116 | {
|
117 | update_bg_model = !update_bg_model;
|
118 | if (update_bg_model)
|
119 | printf("Background update is on\n");
|
120 | else
|
121 | printf("Background update is off\n");
|
122 | }
|
123 | }
|
124 |
|
125 |
|
126 |
|
127 | double t = (cv::getTickCount() - start) / cv::getTickFrequency();
|
128 | std::cout << "t : " << t << std::endl;
|
129 | getchar();
|
130 |
|
131 | return EXIT_SUCCESS;
|
132 | }
|