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