-
Notifications
You must be signed in to change notification settings - Fork 13
/
emvisi2_video.cpp
154 lines (127 loc) · 3.3 KB
/
emvisi2_video.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include "emvisi2.h"
#include <opencv2/highgui/highgui.hpp>
#include <sys/time.h>
#include <list>
#include <string>
#include <boost/filesystem.hpp>
using namespace boost::filesystem;
using namespace cv;
using namespace std;
namespace {
class Timer {
public:
Timer() { start(); }
void start() { gettimeofday(&s,0); }
double duration();
private:
struct timeval s;
};
double Timer::duration() {
struct timeval end, dt;
gettimeofday(&end,0);
timersub(&end,&s, &dt);
return dt.tv_sec * 1000.0 + dt.tv_usec/1000.0;
}
void usage(char *str)
{
cerr << "usage: " << str << " [-v] [-d <destination folder>] -b <background> <input frames>\n";
cerr << " use -v for more verbosity and more intermediate images saved.\n";
exit(-1);
}
string destinationFilename(const string &a, const path &destinationFolder) {
return (destinationFolder / path(path(a).stem().string() + "_result.png")).string();
}
Mat loadImage(string path, int flag) {
Mat im = imread(path, flag);
if (im.empty()) {
cerr << path << ": can't load image.\n";
}
Mat smaller;
pyrDown(im, smaller);
pyrDown(smaller, smaller);
return smaller;
}
} // namespace
int main(int argc, char *argv[])
{
EMVisi2 emv;
int nim=0;
float smooth = 0.0f;
cv::Mat background;
list<string> images;
path destinationFolder("out");
bool window = false;
// parse command line
for (int narg=1; narg<argc; ++narg) {
if (narg + 1 < argc) {
if (strcmp(argv[narg], "-b") == 0) {
background = loadImage(argv[narg+1], -1);
if (background.empty()) {
return 1;
}
narg++;
continue;
} else if (strcmp(argv[narg], "-d") == 0) {
destinationFolder = path(argv[narg+1]);
narg++;
continue;
}
}
try {
create_directory(destinationFolder);
} catch (filesystem_error e) { }
if (strcmp(argv[narg],"-v")==0) {
emv.save_images=true;
} else if (strcmp(argv[narg],"-w")==0) {
window = true;
} else if (strcmp(argv[narg],"-s")==0) {
smooth = 0.001f;
} else if (argv[narg][0] == '-') {
usage(argv[0]);
} else {
images.push_back(argv[narg]);
}
}
if (background.empty()) {
usage(argv[0]);
return 2;
}
Timer timer;
cout << "Initialization.. ";
// EMVisi2 setup
if (!emv.init()) {
cerr << "EMVisi2::init() failed.\n";
return -1;
}
emv.setModel(background);
cout << "done in " << timer.duration() << " ms.\n";
int playing = 0;
for (list<string>::const_iterator it = images.begin();
it != images.end(); ++it) {
Mat frame = loadImage(*it, -1);
if (frame.empty()) {
cerr << *it << ": can't load frame, skipping.\n";
continue;
}
cout << *it << ": ";
timer.start();
emv.setTarget(frame);
emv.run(8, smooth);
cout << "computed in " << timer.duration() << " ms.\n";
save_proba(destinationFilename(*it, destinationFolder).c_str(),emv.proba);
if (window) {
std::vector<cv::Mat> channels;
cv::split(frame, channels);
emv.proba.convertTo(channels[1], CV_8UC1, 255, 0);
cv::Mat result;
cv::merge(channels, result);
imshow("Segmentation", result);
int k = cv::waitKey(playing);
switch (k) {
case ' ': playing = (playing + 1) & 1; break;
case 'p': --it; --it; break;
}
}
}
return 0;
}