#include "opencv2/imgcodecs.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/videoio.hpp"
#include <iostream>
#include <opencv2/highgui.hpp>
#include <opencv2/video.hpp>
#include <sstream>
using namespace cv;
using namespace std;
Mat frame;
Mat fgMaskMOG2;
Ptr<BackgroundSubtractor> pMOG2;
int keyboard;
void processVideo(string videoFilename) {
VideoCapture capture(videoFilename);
if (!capture.isOpened()) {
cerr << "Unable to open video file: " << videoFilename << endl;
exit(EXIT_FAILURE);
}
while ((char)keyboard != 'q' && (char)keyboard != 27) {
if (!capture.read(frame)) {
cerr << "Unable to read next frame." << endl;
cerr << "Exiting..." << endl;
exit(EXIT_FAILURE);
}
cv::resize(frame, frame, cv::Size(), 0.25, 0.25);
pMOG2->apply(frame, fgMaskMOG2);
stringstream ss;
rectangle(frame, cv::Point(10, 2), cv::Point(100, 20), cv::Scalar(255, 255, 255), -1);
ss << capture.get(CAP_PROP_POS_FRAMES);
string frameNumberString = ss.str();
putText(frame, frameNumberString.c_str(), cv::Point(15, 15), FONT_HERSHEY_SIMPLEX, 0.5,
cv::Scalar(0, 0, 0));
imshow("Frame", frame);
imshow("FG Mask MOG 2", fgMaskMOG2);
keyboard = waitKey(30);
}
capture.release();
}
int main(int argc, char *argv[]) {
pMOG2 = createBackgroundSubtractorMOG2();
string inputPath = "..\\images\\car.avi";
processVideo(inputPath);
return 0;
}