-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathAppFeature.cpp
352 lines (292 loc) · 9.72 KB
/
AppFeature.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#include "AppFeature.h"
static bool TestOutlier(const cv::Rect &bound);
AppFeature::AppFeature():valid(false)
{
std::fill(colorHist,colorHist + BIN_SIZE*BIN_SIZE*BIN_SIZE,0);
}
AppFeature::~AppFeature(void)
{
Clear();
}
void AppFeature::SuperviseTraining(ImageRepresentation *image, const cv::Rect &innerbound)
{
if (!TestOutlier(innerbound))
{
return;
}
cv::Rect new_bound = CropDetection(innerbound);
Size validROI(image_height,image_width);
if (!valid)
{
int iteration = tracker.SupervisedTraining(image,new_bound,validROI,true);
if (tracker.available)//tracker become available
{
InitParticleFilter(innerbound); //Init particle filter
accumulateToHist(image,innerbound); //Generate histogram description
valid = true;
}
}else
{
int iteration = tracker.SupervisedTraining(image,new_bound,validROI,false);
accumulateToHist(image,innerbound);
}
}
void AppFeature::SemiSupervisedTraining(ImageRepresentation *image, const cv::Rect &innerbound)
{
if (!TestOutlier(innerbound))
{
return;
}
cv::Rect new_bound = CropDetection(innerbound);
tracker.SemiSupervisedTraining(image,new_bound);
}
/*Clear all instances*/
void AppFeature::Clear()
{
valid = false;
std::fill(colorHist,colorHist+BIN_SIZE*BIN_SIZE*BIN_SIZE,0);
for (int i = 0;i<BIN_SIZE*BIN_SIZE*BIN_SIZE;++i)
{
m_pos[i].setDefault();//set to default value
}
tracker.Clear();
}
void AppFeature::accumulateToHist(ImageRepresentation *image, const cv::Rect &bound)
{
cv::Mat ROI = image->frame(bound);
float hist[BIN_SIZE*BIN_SIZE*BIN_SIZE];
extractHistFeature(hist,ROI);
if (!valid)
{
for (int i =0;i<BIN_SIZE*BIN_SIZE*BIN_SIZE;++i)
{
m_pos[i].setValues(hist[i],1);//set estimated kalman filter
m_pos[i].update(hist[i]);
colorHist[i] = m_pos[i].getMean();
}
}else
{
for (int i = 0; i<BIN_SIZE*BIN_SIZE*BIN_SIZE; ++i)
{
m_pos[i].update(hist[i]);
colorHist[i] = m_pos[i].getMean();
}
}
}
float AppFeature::TestHistSimi(ImageRepresentation *image, const cv::Rect &detection) const
{
if(!TestOutlier(detection))
{
return -1;
}
cv::Rect new_bound = CropDetection(detection);
cv::Mat ROI = image->frame(new_bound);
float hist[BIN_SIZE*BIN_SIZE*BIN_SIZE];
extractHistFeature(hist,ROI);
float classify = evalHist(hist);
return classify;
}
float AppFeature::evalHist(const float hist[BIN_SIZE*BIN_SIZE*BIN_SIZE]) const
{
float distance_pos = WeakClassifierRGBHist::Bhattacharyya_distance(colorHist, hist, BIN_SIZE*BIN_SIZE*BIN_SIZE);
float simi_pos = 1-distance_pos;
return simi_pos;
}
void AppFeature::extractHistFeature(float hist[BIN_SIZE*BIN_SIZE*BIN_SIZE], const cv::Mat &ROI)
{
std::fill(hist,hist+BIN_SIZE*BIN_SIZE*BIN_SIZE,0);
cv::Mat Lab_ROI;
cv::Rect crop(0.15*ROI.cols, 0.15*ROI.rows, 0.7*ROI.cols, 0.7*ROI.rows);
cv::Mat shrinkROI = ROI(crop);
cv::cvtColor(shrinkROI,Lab_ROI,CV_BGR2Lab);
uchar gap = 256/BIN_SIZE;
float h_w = Lab_ROI.cols/2.0f;
float h_h = Lab_ROI.rows/2.0f;
float sum_location = 0;
for (int i = 0; i<Lab_ROI.cols; ++i)
{
for (int j = 0; j < Lab_ROI.rows; ++j)
{
cv::Vec3b val = Lab_ROI.at<cv::Vec3b>(j,i);
uchar L = val.val[0];
uchar a = val.val[1];
uchar b = val.val[2];
int indexB = L / gap;
int indexG = a / gap;
int indexR = b / gap;
//weight of this location
float location = stdfunc::pow2((i-h_w)/h_w) + stdfunc::pow2((j-h_h)/h_h);
if (location<=1)
{
float weight = 0.75f*(1-location);
hist[(indexG*BIN_SIZE+indexR)*BIN_SIZE+indexB] += weight;
// hist[indexB] += weight;
// hist[indexG + BIN_SIZE] += weight;
// hist[indexR + BIN_SIZE*2] += weight;
sum_location += weight;
}
}
}
for (int i = 0; i < BIN_SIZE*BIN_SIZE*BIN_SIZE; ++i)
{
hist[i]/=sum_location;
}
}
/************************************************************************/
/* Crop body detection to a refined region */
/************************************************************************/
cv::Rect AppFeature::CropDetection(const cv::Rect &original)
{
//crop accord to the inner setting of tracker (mainly upper)
cv::Rect new_bound;
new_bound.width = int(original.width/2.0f + 0.5f);
new_bound.height = int(original.height + 0.5f);
new_bound.y = int(original.y + 0.5f);
new_bound.x = int(original.x + original.width/4.0f + 0.5f);
return original;
}
/*
cv::Rect AppFeature::CropDetection(const cv::Rect &original)
{
//crop accord to the inner setting of tracker (mainly upper)
cv::Rect new_bound;
new_bound.width = int(original.width/2.0f + 0.5f);
new_bound.height = int(original.height*0.35f +0.5f);
new_bound.y = int(original.y + original.height * 0.19f + 0.5f);
new_bound.x = int(original.x + original.width/4.0f + 0.5f);
return new_bound;
}
*/
sir_filter::State AppFeature::InitParticleFilter(const sir_filter::State &state)
{
cv::Rect new_bound(state.pos_x-state.scale_x/2.0f,state.pos_y-state.scale_y/2.0f,state.scale_x,state.scale_y);
return InitParticleFilter(new_bound);
}
/*Init particle filter*/
sir_filter::State AppFeature::InitParticleFilter(const cv::Rect &new_bound)
{
float aspect = MIN_WIDTH / float(MIN_HEIGHT);
// Draw samples from initial detection
float scale_det = new_bound.width / float(MIN_WIDTH);
float sigma = 14 * scale_det; // initial pos sigma for drawing samples
float sigma_scale = 0; // scale sigma
float center[] = {new_bound.x + new_bound.width/2.0f, new_bound.y + new_bound.height/2.0f};
std::vector<sir_filter::State> state;
std::vector<float> weight;
state.reserve(sir_filter::SIRFilter::N_PARTICLE);
weight.reserve(sir_filter::SIRFilter::N_PARTICLE);
float sum_w = 0;
float max_weight = 0;
sir_filter::State max_state;
//draw std randn noise centered at bounding box center
for(int i = 0; i < sir_filter::SIRFilter::N_PARTICLE; ++i)
{
float noise_x = stdfunc::randn(0,sigma);
float noise_y = stdfunc::randn(0,sigma);
sir_filter::State sample_state;
sample_state.pos_x = center[0] + noise_x;
sample_state.pos_y = center[1] + noise_y;
float noise_scale_w = stdfunc::randn(0,sigma_scale);
float noise_scale_h = noise_scale_w / aspect;
sample_state.scale_x = new_bound.width + noise_scale_w;
sample_state.scale_y = new_bound.height + noise_scale_h;
cv::Rect sample_bound(int(sample_state.pos_x-sample_state.scale_x/2+0.5f), int(sample_state.pos_y - sample_state.scale_y/2+0.5f) ,
int(sample_state.scale_x + 0.5f), int(sample_state.scale_y + 0.5f));
float w = GetOverLapPercentage(sample_bound,new_bound);
state.push_back(sample_state);
weight.push_back(w);
sum_w += w;
if (w > max_weight)
{
max_weight = w;
max_state = sample_state;
}
}
//normalize weight
for(std::vector<float>::iterator iter = weight.begin(); iter != weight.end(); ++iter)
{
(*iter) /= sum_w;
}
particle_filter.SetInitState(state,weight);
return max_state;
}
float AppFeature::TestSimi(ImageRepresentation *image, const cv::Rect &detection, bool use_negative)
{
if(!TestOutlier(detection))
{
return 0;
}
cv::Rect new_bound = CropDetection(detection);
float classify = tracker.Evaluate(image,new_bound,use_negative);
return classify;
}
/*Particle filtering when detection responses are associated*/
void AppFeature::DetectionCheck(const float vec[2], const cv::Rect &detection, const std::list<ImageRepresentation*> &frame_buffer, int frame_no, const cv::Rect &predict_bound, sir_filter::State &max_state)
{
std::vector<sir_filter::State> state_org = particle_filter.GetParticleStates();
float scale_det = predict_bound.width / float(MIN_WIDTH);
float sig_pos = 6 * scale_det;
float sig_scale = 0;
int frame_gap = frame_buffer.size();
particle_filter.DrawSamplesFromStateTransition(vec, frame_gap, sig_pos, sig_scale);
std::vector<sir_filter::State> proposed_state = particle_filter.GetParticleStates();
//Calculate weight for each transited particle
std::vector<float> weight;
float max_weight = CalWeightForParticle(detection,NULL,proposed_state,weight,max_state);
//Whether reinit
max_state = InitParticleFilter(detection);
}
/*Calculate weight for each particle*/
float AppFeature::CalWeightForParticle(const cv::Rect &detection, ImageRepresentation *image, const std::vector<sir_filter::State> &particles, std::vector<float> &weight, sir_filter::State &max_state)
{
weight.reserve(sir_filter::SIRFilter::N_PARTICLE);
float max_weight = 0;
if(detection.area() == 0) //no detection
{
assert(image!=NULL);
for(std::vector<sir_filter::State>::const_iterator citer = particles.cbegin(); citer != particles.cend(); ++citer)
{
const sir_filter::State &s = (*citer);
cv::Rect candidate(int(s.pos_x - s.scale_x/2 + 0.5f), int(s.pos_y - s.scale_y/2 + 0.5f), int(s.scale_x + 0.5f), int(s.scale_y + 0.5f));
float w = TestSimi(image,candidate,false); // w ranges from [0, 1]
if(max_weight < w)
{
max_weight = w;
max_state = s;
}
weight.push_back(w);
}
}else //have a detection, weight is determined by associated detection result
{
for(std::vector<sir_filter::State>::const_iterator citer = particles.cbegin(); citer != particles.cend(); ++citer)
{
const sir_filter::State &s = (*citer);
cv::Rect candidate(int(s.pos_x - s.scale_x/2 + 0.5f), int(s.pos_y - s.scale_y/2 + 0.5f), int(s.scale_x + 0.5f), int(s.scale_y + 0.5f));
float w = GetOverLapPercentage(detection, candidate);
if(max_weight < w)
{
max_weight = w;
max_state = s;
}
weight.push_back(w);
}
}
return max_weight;
}
/*Get object's current hidden state*/
sir_filter::State AppFeature::GetObjState()
{
sir_filter::State avg_state = particle_filter.GetAvgState();
return avg_state;
}
/*Test whether bound is inside the image*/
static bool TestOutlier(const cv::Rect &bound)
{
if(bound.x <0 || bound.y<0 || bound.br().x >= image_width || bound.br().y >= image_height)
{
return false;//out
}
if(bound.width<=0 || bound.height<=0)
return false;
return true;//inside image frame
}