-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.cpp
270 lines (221 loc) · 5.76 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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#include <opencv2/opencv.hpp>
#include <iostream>
#include <cmath>
#include <sys/time.h>
#include <cstdlib>
#include <string>
#include "getopt.h"
#include "filter.h"
#include "selector.h"
#include "state.h"
using namespace cv;
using namespace std;
typedef unsigned int uint;
const char* WINDOW = "Particle Tracker";
const uint NUM_PARTICLES = 200;
inline void update_target_histogram(Mat& image, Rect& selection, Mat& histogram, Mat& target) {
Mat roi(image, selection);
roi.copyTo(target);
Mat new_hist;
float alpha = 0.2;
calc_hist(roi, new_hist);
normalize(new_hist, new_hist);
if(histogram.empty()) {
histogram = new_hist;
}
else {
histogram = ((1.f - alpha) * histogram) + (alpha * new_hist);
normalize(histogram, histogram);
}
cout << "Target updated" << endl;
}
struct StateData {
StateData(int num_particles):
image(),
target(),
target_histogram(),
selector(WINDOW),
selection(),
paused(false),
draw_particles(false),
filter(num_particles)
{};
Mat image;
Mat target;
Mat target_histogram;
Selector selector;
Rect selection;
bool paused;
bool draw_particles;
ParticleFilter filter;
};
State_ state_start(StateData& d) {
if( d.selector.selecting() ) {
cout << "state_selecting" << endl;
return state_selecting;
}
else {
return state_start;
}
}
State_ state_selecting(StateData& d) {
if( d.selector.valid() ) {
cout << "state_initializing: (" << d.selection.x << ", " << d.selection.y << ", " << d.selection.width << ", " << d.selection.height << ")" << endl;
d.selection = d.selector.selection();
cout << "selection: (" << d.selection.x << ", " << d.selection.y << ", " << d.selection.width << ", " << d.selection.height << ")" << endl;
return state_initializing(d); // Call with current frame
}
else {
Mat roi(d.image, d.selector.selection());
bitwise_not(roi, roi);
return state_selecting;
}
}
State_ state_initializing(StateData& d) {
if( d.selector.selecting() ) {
cout << "state_selecting" << endl;
return state_selecting;
}
// Generate initial target histogram
update_target_histogram(d.image, d.selection, d.target_histogram, d.target);
// Initialize condensation filter with center of selection
d.filter.init(d.selection);
// Start video running if paused
d.paused = false;
cout << "state_tracking" << endl;
return state_tracking(d); // Call with current frame
}
State_ state_tracking(StateData& d) {
if( d.selector.selecting() ) {
cout << "state_selecting" << endl;
return state_selecting;
}
// Update particle filter
d.filter.update(d.image, d.selection.size(), d.target_histogram);
Size target_size(d.target.cols, d.target.rows);
// Draw particles
if( d.draw_particles )
d.filter.draw_particles(d.image, target_size, Scalar(255, 255, 255));
// Draw estimated state with color based on confidence
float confidence = d.filter.confidence();
// TODO - Make these values not arbitrary
d.filter.draw_estimated_state(d.image, target_size, Scalar(0, 255, 0));
return state_tracking;
}
struct Options {
Options()
:num_particles(NUM_PARTICLES),
infile(),
outfile()
{}
int num_particles;
string infile;
string outfile;
};
void parse_command_line(int argc, char** argv, Options& o) {
int c = -1;
while( (c = getopt(argc, argv, "lo:p:")) != -1 ) {
switch(c) {
case 'o':
o.outfile = optarg;
break;
case 'p':
o.num_particles = atoi(optarg);
break;
default:
cerr << "Usage: " << argv[0] << " [-o output_file] [-p num_particles] [input_file]" << endl << endl;
cerr << "\t-o output_file : Optional MPEG output file" << endl;
cerr << "\t-p num_particles: Number of particles (samples) to use, default is 200" << endl;
cerr << "\tinput_file : Optional file to read, otherwise use camera" << endl;
exit(1);
}
}
if( optind < argc ) {
o.infile = argv[optind];
}
cout << "Num particles: " << o.num_particles << endl;
cout << "Input file: " << o.infile << endl;
cout << "Output file: " << o.outfile << endl;
}
int main(int argc, char** argv) {
Options o;
parse_command_line(argc, argv, o);
bool use_camera;
VideoCapture cap;
VideoWriter writer;
// Use filename if given, else use default camera
if( !o.infile.empty() ) {
cap.open(o.infile);
use_camera = false;
}
else {
cap.open(0);
use_camera = true;
}
if( !cap.isOpened() ) {
cerr << "Failed to open capture device" << endl;
exit(2);
}
if( !o.outfile.empty() ) {
int fps = cap.get(CV_CAP_PROP_FPS);
int width = cap.get(CV_CAP_PROP_FRAME_WIDTH);
int height = cap.get(CV_CAP_PROP_FRAME_HEIGHT);
writer.open(o.outfile, CV_FOURCC('j', 'p', 'e', 'g'), fps, Size(width, height));
if( !writer.isOpened() ) {
cerr << "Could not open '" << o.outfile << "'" << endl;
exit(1);
}
use_camera = false;
}
// Open window and start capture
namedWindow(WINDOW, CV_WINDOW_FREERATIO | CV_GUI_NORMAL);
StateData d(o.num_particles);
State state = state_start;
Mat frame, gray;
// Main loop
int count = 0;
for(;;) {
// Start timing the loop
timeval start_time;
gettimeofday(&start_time, 0);
// Capture frame
if( !d.paused) {
cap >> frame;
if(frame.empty()) {
cerr << "Error reading frame" << endl;
break;
}
}
if( use_camera ) {
flip(frame, d.image, 1);
}
else {
frame.copyTo(d.image);
}
if (!count) {d.paused = true;}
// Handle keyboard input
char c = (char)waitKey(10);
if( c == 27 )
break;
switch(c) {
case 'p':
d.paused = !d.paused;
break;
case 'c':
cout << "Tracking cancelled." << endl;
state = state_start;
break;
case 'd':
d.draw_particles = !d.draw_particles;
cout << "Draw particles: " << d.draw_particles << endl;
break;
}
// Process frame in current state
state = state(d);
imshow(WINDOW, d.image);
if( writer.isOpened() and !d.paused ) {
writer << d.image;
}
count++;
}
}