forked from spmallick/learnopencv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground_subtr_opencv.cpp
63 lines (50 loc) · 1.8 KB
/
background_subtr_opencv.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <sstream>
#include <opencv2/bgsegm.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/video.hpp>
using namespace cv;
using namespace std;
using namespace cv::bgsegm;
const char* input_params = "{ input | space_traffic.mp4 | Define the full input video path }";
void get_opencv_result(String video_to_process) {
// create VideoCapture object for further video processing
VideoCapture capture(samples::findFile(video_to_process));
if (!capture.isOpened()) {
//error in opening the video input
cerr << "Unable to open: " << video_to_process << endl;
return;
}
// instantiate background subtraction model
Ptr<BackgroundSubtractorGSOC> background_subtr_method = createBackgroundSubtractorGSOC();
Mat frame, fgMask, background;
while (true) {
capture >> frame;
// check whether the frames have been grabbed
if (frame.empty())
break;
// resize video frames
resize(frame, frame, Size(640, 360));
// pass the frame to the background subtractor
background_subtr_method->apply(frame, fgMask);
// obtain the background without foreground mask
background_subtr_method->getBackgroundImage(background);
// show the current frame, foreground mask, subtracted result
imshow("Initial Frames", frame);
imshow("Foreground Masks", fgMask);
imshow("Subtraction Result", background);
int keyboard = waitKey(10);
if (keyboard == 27)
break;
}
}
int main(int argc, char* argv[])
{
CommandLineParser parser(argc, argv, input_params);
// start BS-pipeline
get_opencv_result(parser.get<String>("input"));
return 0;
}