-
Notifications
You must be signed in to change notification settings - Fork 3
/
backgroud_subtraction.cpp
50 lines (42 loc) · 1.31 KB
/
backgroud_subtraction.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
#include <iostream>
#include <filesystem>
#include <opencv2/videoio.hpp>
#include <opencv2/video.hpp>
#include <opencv2/opencv.hpp>
#include "utils.h"
int main(int argc, char* argv[])
{
if(argc < 2) {
std::cout<< "Usage: ./" << argv[0] << " [Camera Num or File Path with format imgX.png]\n";
return 1;
}
cv::Ptr<cv::BackgroundSubtractor> pBackSub = cv::createBackgroundSubtractorMOG2();
if(std::filesystem::exists(argv[1])) {
std::cout << "Interpreting input as File Path.\n";
std::vector<std::string> images;
cv::glob(std::string(argv[1]) + "img*.png", images);
unsigned x = 0;
for (const auto &file : images) {
cv::Mat img = cv::imread(file);
cv::Mat fgMask = subtractBackground(img, pBackSub);
cv::imwrite(std::string(argv[1]) + "mask_" + std::to_string(x) + ".png", fgMask);
x++;
}
} else {
std::cout << "Interpreting input as camera number.\n";
int camNum = std::atoi(argv[1]);
// Read the web cam
cv::VideoCapture cap;
cv::Mat frame, fgMask;
if(!cap.open(camNum, cv::CAP_ANY))
std::cout << "Could not open camera!\n";
while (cap.isOpened() && cap.read(frame)) {
fgMask = subtractBackground(frame, pBackSub);
//show the current frame and the fg masks
cv::imshow("Frame", frame);
cv::imshow("FG Mask", fgMask);
if (cv::waitKey(10) == 27) break;
}
}
return 0;
}