Path: blob/master/Background-Subtraction/background_subtr_opencv.cpp
3118 views
#include <iostream>1#include <sstream>2#include <opencv2/bgsegm.hpp>3#include <opencv2/imgcodecs.hpp>4#include <opencv2/imgproc.hpp>5#include <opencv2/videoio.hpp>6#include <opencv2/highgui.hpp>7#include <opencv2/video.hpp>89using namespace cv;10using namespace std;11using namespace cv::bgsegm;1213const char* input_params = "{ input | space_traffic.mp4 | Define the full input video path }";1415void get_opencv_result(String video_to_process) {16// create VideoCapture object for further video processing17VideoCapture capture(samples::findFile(video_to_process));18if (!capture.isOpened()) {19//error in opening the video input20cerr << "Unable to open: " << video_to_process << endl;21return;22}2324// instantiate background subtraction model25Ptr<BackgroundSubtractorGSOC> background_subtr_method = createBackgroundSubtractorGSOC();2627Mat frame, fgMask, background;28while (true) {29capture >> frame;3031// check whether the frames have been grabbed32if (frame.empty())33break;3435// resize video frames36resize(frame, frame, Size(640, 360));3738// pass the frame to the background subtractor39background_subtr_method->apply(frame, fgMask);40// obtain the background without foreground mask41background_subtr_method->getBackgroundImage(background);4243// show the current frame, foreground mask, subtracted result44imshow("Initial Frames", frame);45imshow("Foreground Masks", fgMask);46imshow("Subtraction Result", background);4748int keyboard = waitKey(10);49if (keyboard == 27)50break;51}52}535455int main(int argc, char* argv[])56{57CommandLineParser parser(argc, argv, input_params);58// start BS-pipeline59get_opencv_result(parser.get<String>("input"));6061return 0;62}6364