forked from tsaith/pyct
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compressive_tracker.cpp
323 lines (260 loc) · 10.4 KB
/
compressive_tracker.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
#include "compressive_tracker.h"
#include <math.h>
#include <iostream>
#include <fstream>
using namespace cv;
using namespace std;
//------------------------------------------------
CompressiveTracker::CompressiveTracker(void)
{
featureMinNumRect = 2;
featureMaxNumRect = 4; // number of rectangle from 2 to 4
featureNum = 50; // number of all weaker classifiers, i.e,feature pool
rOuterPositive = 4; // radical scope of positive samples
rSearchWindow = 25; // size of search window
muPositive = vector<float>(featureNum, 0.0f);
muNegative = vector<float>(featureNum, 0.0f);
sigmaPositive = vector<float>(featureNum, 1.0f);
sigmaNegative = vector<float>(featureNum, 1.0f);
learnRate = 0.85f; // Learning rate parameter
}
CompressiveTracker::~CompressiveTracker(void)
{
}
void CompressiveTracker::init_wrap(vector<vector<uint8> > &_frame, vector<int> &_object_box)
{
/*
A wrapper to the init method.
*/
int rows, cols;
rows = _frame.size();
cols = _frame[0].size();
Mat frame_mat = Mat::zeros(rows, cols, CV_8UC1);
int i, j;
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
frame_mat.at<uint8>(i, j) = _frame[i][j];
}
}
Rect box;
box.x = _object_box[0];
box.y = _object_box[1];
box.width = _object_box[2];
box.height = _object_box[3];
init(frame_mat, box);
}
void CompressiveTracker::process_frame_wrap(vector<vector<uint8> > &_frame, vector<int> &_object_box)
{
/*
A wrapper to the processFrame method.
*/
int rows, cols;
rows = _frame.size();
cols = _frame[0].size();
Mat frame_mat = Mat::zeros(rows, cols, CV_8UC1);
int i, j;
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
frame_mat.at<uint8>(i, j) = _frame[i][j];
}
}
Rect box;
box.x = _object_box[0];
box.y = _object_box[1];
box.width = _object_box[2];
box.height = _object_box[3];
// Process the frame
processFrame(frame_mat, box);
_object_box[0] = box.x;
_object_box[1] = box.y;
_object_box[2] = box.width;
_object_box[3] = box.height;
}
void CompressiveTracker::HaarFeature(Rect& _objectBox, int _numFeature)
/*Description: compute Haar features
Arguments:
-_objectBox: [x y width height] object rectangle
-_numFeature: total number of features.The default is 50.
*/
{
features = vector<vector<Rect> >(_numFeature, vector<Rect>());
featuresWeight = vector<vector<float> >(_numFeature, vector<float>());
int numRect;
Rect rectTemp;
float weightTemp;
for (int i=0; i<_numFeature; i++) {
numRect = cvFloor(rng.uniform((double)featureMinNumRect, (double)featureMaxNumRect));
for (int j=0; j<numRect; j++) {
rectTemp.x = cvFloor(rng.uniform(0.0, (double)(_objectBox.width - 3)));
rectTemp.y = cvFloor(rng.uniform(0.0, (double)(_objectBox.height - 3)));
rectTemp.width = cvCeil(rng.uniform(0.0, (double)(_objectBox.width - rectTemp.x - 2)));
rectTemp.height = cvCeil(rng.uniform(0.0, (double)(_objectBox.height - rectTemp.y - 2)));
features[i].push_back(rectTemp);
weightTemp = (float)pow(-1.0, cvFloor(rng.uniform(0.0, 2.0))) / sqrt(float(numRect));
featuresWeight[i].push_back(weightTemp);
}
}
}
void CompressiveTracker::sampleRect(Mat& _image, Rect& _objectBox, float _rInner, float _rOuter, int _maxSampleNum, vector<Rect>& _sampleBox)
/* Description: compute the coordinate of positive and negative sample image templates
Arguments:
-_image: processing frame
-_objectBox: recent object position
-_rInner: inner sampling radius
-_rOuter: Outer sampling radius
-_maxSampleNum: maximal number of sampled images
-_sampleBox: Storing the rectangle coordinates of the sampled images.
*/
{
int rowsz = _image.rows - _objectBox.height - 1;
int colsz = _image.cols - _objectBox.width - 1;
float inradsq = _rInner*_rInner;
float outradsq = _rOuter*_rOuter;
int dist;
int minrow = max(0,(int)_objectBox.y-(int)_rInner);
int maxrow = min((int)rowsz-1,(int)_objectBox.y+(int)_rInner);
int mincol = max(0,(int)_objectBox.x-(int)_rInner);
int maxcol = min((int)colsz-1,(int)_objectBox.x+(int)_rInner);
int i = 0;
float prob = ((float)(_maxSampleNum))/(maxrow-minrow+1)/(maxcol-mincol+1);
int r;
int c;
_sampleBox.clear();//important
Rect rec(0,0,0,0);
for( r=minrow; r<=(int)maxrow; r++ )
for( c=mincol; c<=(int)maxcol; c++ ){
dist = (_objectBox.y-r)*(_objectBox.y-r) + (_objectBox.x-c)*(_objectBox.x-c);
if( rng.uniform(0.,1.)<prob && dist < inradsq && dist >= outradsq ){
rec.x = c;
rec.y = r;
rec.width = _objectBox.width;
rec.height= _objectBox.height;
_sampleBox.push_back(rec);
i++;
}
}
_sampleBox.resize(i);
}
void CompressiveTracker::sampleRect(Mat& _image, Rect& _objectBox, float _srw, vector<Rect>& _sampleBox)
/* Description: Compute the coordinate of samples when detecting the object.*/
{
int rowsz = _image.rows - _objectBox.height - 1;
int colsz = _image.cols - _objectBox.width - 1;
float inradsq = _srw*_srw;
int dist;
int minrow = max(0,(int)_objectBox.y-(int)_srw);
int maxrow = min((int)rowsz-1,(int)_objectBox.y+(int)_srw);
int mincol = max(0,(int)_objectBox.x-(int)_srw);
int maxcol = min((int)colsz-1,(int)_objectBox.x+(int)_srw);
int i = 0;
int r;
int c;
Rect rec(0,0,0,0);
_sampleBox.clear();//important
for( r=minrow; r<=(int)maxrow; r++ )
for( c=mincol; c<=(int)maxcol; c++ ){
dist = (_objectBox.y-r)*(_objectBox.y-r) + (_objectBox.x-c)*(_objectBox.x-c);
if( dist < inradsq ){
rec.x = c;
rec.y = r;
rec.width = _objectBox.width;
rec.height= _objectBox.height;
_sampleBox.push_back(rec);
i++;
}
}
_sampleBox.resize(i);
}
// Compute the features of samples
void CompressiveTracker::getFeatureValue(Mat& _imageIntegral, vector<Rect>& _sampleBox, Mat& _sampleFeatureValue) {
int sampleBoxSize = _sampleBox.size();
_sampleFeatureValue.create(featureNum, sampleBoxSize, CV_32F);
float tempValue;
int xMin;
int xMax;
int yMin;
int yMax;
for (int i=0; i<featureNum; i++) {
for (int j=0; j<sampleBoxSize; j++)
{
tempValue = 0.0f;
for (size_t k=0; k<features[i].size(); k++)
{
xMin = _sampleBox[j].x + features[i][k].x;
xMax = _sampleBox[j].x + features[i][k].x + features[i][k].width;
yMin = _sampleBox[j].y + features[i][k].y;
yMax = _sampleBox[j].y + features[i][k].y + features[i][k].height;
tempValue += featuresWeight[i][k] *
(_imageIntegral.at<float>(yMin, xMin) +
_imageIntegral.at<float>(yMax, xMax) -
_imageIntegral.at<float>(yMin, xMax) -
_imageIntegral.at<float>(yMax, xMin));
}
_sampleFeatureValue.at<float>(i,j) = tempValue;
}
}
}
// Update the mean and variance of the gaussian classifier
void CompressiveTracker::classifierUpdate(Mat& _sampleFeatureValue, vector<float>& _mu, vector<float>& _sigma, float _learnRate) {
Scalar muTemp;
Scalar sigmaTemp;
for (int i=0; i<featureNum; i++) {
meanStdDev(_sampleFeatureValue.row(i), muTemp, sigmaTemp);
_sigma[i] = (float)sqrt( _learnRate*_sigma[i]*_sigma[i]
+ (1.0f-_learnRate)*sigmaTemp.val[0]*sigmaTemp.val[0]
+ _learnRate*(1.0f-_learnRate)*(_mu[i]-muTemp.val[0])*(_mu[i]-muTemp.val[0])); // equation 6 in paper
_mu[i] = _mu[i]*_learnRate + (1.0f-_learnRate)*muTemp.val[0]; // equation 6 in paper
}
}
// Compute the ratio classifier
void CompressiveTracker::ratioClassifier(vector<float>& _muPos, vector<float>& _sigmaPos, vector<float>& _muNeg, vector<float>& _sigmaNeg, Mat& _sampleFeatureValue, float& _ratioMax, int& _ratioMaxIndex) {
float sumRatio;
_ratioMax = -FLT_MAX;
_ratioMaxIndex = 0;
float pPos;
float pNeg;
int sampleBoxNum = _sampleFeatureValue.cols;
for (int j=0; j<sampleBoxNum; j++) {
sumRatio = 0.0f;
for (int i=0; i<featureNum; i++) {
pPos = exp( (_sampleFeatureValue.at<float>(i,j)-_muPos[i])*(_sampleFeatureValue.at<float>(i,j)-_muPos[i]) / -(2.0f*_sigmaPos[i]*_sigmaPos[i]+1e-30) ) / (_sigmaPos[i]+1e-30);
pNeg = exp( (_sampleFeatureValue.at<float>(i,j)-_muNeg[i])*(_sampleFeatureValue.at<float>(i,j)-_muNeg[i]) / -(2.0f*_sigmaNeg[i]*_sigmaNeg[i]+1e-30) ) / (_sigmaNeg[i]+1e-30);
sumRatio += log(pPos+1e-30) - log(pNeg+1e-30); // equation 4
}
if (_ratioMax < sumRatio) {
_ratioMax = sumRatio;
_ratioMaxIndex = j;
}
}
}
void CompressiveTracker::init(Mat& _frame, Rect& _objectBox)
{
// compute feature template
HaarFeature(_objectBox, featureNum);
// compute sample templates
sampleRect(_frame, _objectBox, rOuterPositive, 0, 1000000, samplePositiveBox);
sampleRect(_frame, _objectBox, rSearchWindow*1.5, rOuterPositive+4.0, 100, sampleNegativeBox);
integral(_frame, imageIntegral, CV_32F);
getFeatureValue(imageIntegral, samplePositiveBox, samplePositiveFeatureValue);
getFeatureValue(imageIntegral, sampleNegativeBox, sampleNegativeFeatureValue);
classifierUpdate(samplePositiveFeatureValue, muPositive, sigmaPositive, learnRate);
classifierUpdate(sampleNegativeFeatureValue, muNegative, sigmaNegative, learnRate);
}
void CompressiveTracker::processFrame(Mat& _frame, Rect& _objectBox)
{
// predict
sampleRect(_frame, _objectBox, rSearchWindow,detectBox);
integral(_frame, imageIntegral, CV_32F);
getFeatureValue(imageIntegral, detectBox, detectFeatureValue);
int ratioMaxIndex;
float ratioMax;
ratioClassifier(muPositive, sigmaPositive, muNegative, sigmaNegative, detectFeatureValue, ratioMax, ratioMaxIndex);
_objectBox = detectBox[ratioMaxIndex];
// update
sampleRect(_frame, _objectBox, rOuterPositive, 0.0, 1000000, samplePositiveBox);
sampleRect(_frame, _objectBox, rSearchWindow*1.5, rOuterPositive+4.0, 100, sampleNegativeBox);
getFeatureValue(imageIntegral, samplePositiveBox, samplePositiveFeatureValue);
getFeatureValue(imageIntegral, sampleNegativeBox, sampleNegativeFeatureValue);
classifierUpdate(samplePositiveFeatureValue, muPositive, sigmaPositive, learnRate);
classifierUpdate(sampleNegativeFeatureValue, muNegative, sigmaNegative, learnRate);
}