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