-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
96 lines (85 loc) · 2.66 KB
/
main.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <memory>
#include <opencv2/opencv.hpp>
#include "stopwatch.h"
#include "backgroundhider.h"
#include "colorfilter.h"
bool pressedESC() {
static const int EscapeKey = 27;
return cv::waitKey(1) == EscapeKey;
}
int processFrames(std::function<void(cv::Mat)> body) {
cv::VideoCapture cap(0);
if (!cap.isOpened()) {
std::cerr << "Couldn't open videocam" << std::endl;
return -1;
}
while (!pressedESC()) {
cv::Mat frame;
cap >> frame;
body(frame);
}
return 0;
}
const char*const BGName = "Background Estimate";
const char*const TrackerName = "FCS Tracker";
int main(int argc, char** argv) {
if (argc>1 && argv[1] == std::string("--record")) {
std::string filename = "input.avi";
if (argc>2) filename = argv[2];
cv::VideoCapture cap(0);
if (!cap.isOpened()) {
std::cerr << "Couldn't open videocam" << std::endl;
return -1;
}
cv::Mat frame;
cap >> frame;
cv::VideoWriter writer;
writer.open(filename, CV_FOURCC('D','I','V','X'),30, frame.size(), true);
if (!writer.isOpened()) {
std::cerr << "Couldn't open writer with file: " << filename << "\n";
return -2;
}
cv::namedWindow("input");
while (cv::waitKey(1)<1) {
cap >> frame;
cv::imshow("input", frame);
writer << frame;
}
return 0;
}
cv::namedWindow(BGName);
cv::namedWindow(TrackerName);
int threshold = 30;
int satMin = 100;
int valMin = 100;
int showStage = 0;
cv::createTrackbar("threshold", TrackerName, &threshold, 255);
cv::createTrackbar("showStage", TrackerName, &showStage, 5);
BackgroundHider bgHider(threshold);
ColorFilter redFilter(0, 10, satMin, valMin);
StopWatch stopWatch;
return processFrames([&](cv::Mat frame) {
cv::Mat withoutBackground = bgHider.process(frame);
cv::Mat eroded;
cv::erode(withoutBackground, eroded, cv::Mat(), cv::Point(-1,-1), 3);
cv::Mat dilated;
cv::dilate(eroded, dilated, cv::Mat(), cv::Point(-1,-1), 5);
//cv::Mat processed = redFilter.filter(withoutBackground);
cv::Mat* showImage = 0;
switch (showStage) {
case 0: showImage = &frame; break;
case 1: showImage = &withoutBackground; break;
case 2: showImage = &eroded; break;
case 3: showImage = &dilated; break;
default: showImage = 0;
}
if (showImage) {
std::ostringstream oss;
auto millis = stopWatch.getMillisAndReset();
oss << 1000/millis;
cv::putText(*showImage, oss.str(),cv::Point(30,frame.rows-30), cv::FONT_HERSHEY_COMPLEX_SMALL, 0.8, cv::Scalar(200,200,250),1,CV_AA);
cv::imshow(BGName, bgHider.backgroundEstimate());
cv::imshow(TrackerName, *showImage);
}
});
}