-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathTracker.cpp
236 lines (202 loc) · 5.99 KB
/
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
/*
* Struck: Structured Output Tracking with Kernels
*
* Code to accompany the paper:
* Struck: Structured Output Tracking with Kernels
* Sam Hare, Amir Saffari, Philip H. S. Torr
* International Conference on Computer Vision (ICCV), 2011
*
* Copyright (C) 2011 Sam Hare, Oxford Brookes University, Oxford, UK
*
* This file is part of Struck.
*
* Struck is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Struck is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Struck. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "Tracker.h"
#include "Config.h"
#include "ImageRep.h"
#include "Sampler.h"
#include "Sample.h"
#include "GraphUtils/GraphUtils.h"
#include "HaarFeatures.h"
#include "RawFeatures.h"
#include "HistogramFeatures.h"
#include "MultiFeatures.h"
#include "Kernels.h"
#include "LaRank.h"
#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <Eigen/Core>
#include <vector>
#include <algorithm>
using namespace cv;
using namespace std;
using namespace Eigen;
Tracker::Tracker(const Config& conf) :
m_config(conf),
m_initialised(false),
m_pLearner(0),
m_debugImage(2*conf.searchRadius+1, 2*conf.searchRadius+1, CV_32FC1),
m_needsIntegralImage(false)
{
Reset();
}
Tracker::~Tracker()
{
delete m_pLearner;
for (int i = 0; i < (int)m_features.size(); ++i)
{
delete m_features[i];
delete m_kernels[i];
}
}
void Tracker::Reset()
{
m_initialised = false;
m_debugImage.setTo(0);
if (m_pLearner) delete m_pLearner;
for (int i = 0; i < (int)m_features.size(); ++i)
{
delete m_features[i];
delete m_kernels[i];
}
m_features.clear();
m_kernels.clear();
m_needsIntegralImage = false;
m_needsIntegralHist = false;
int numFeatures = m_config.features.size();
vector<int> featureCounts;
for (int i = 0; i < numFeatures; ++i)
{
switch (m_config.features[i].feature)
{
case Config::kFeatureTypeHaar:
m_features.push_back(new HaarFeatures(m_config));
m_needsIntegralImage = true;
break;
case Config::kFeatureTypeRaw:
m_features.push_back(new RawFeatures(m_config));
break;
case Config::kFeatureTypeHistogram:
m_features.push_back(new HistogramFeatures(m_config));
m_needsIntegralHist = true;
break;
}
featureCounts.push_back(m_features.back()->GetCount());
switch (m_config.features[i].kernel)
{
case Config::kKernelTypeLinear:
m_kernels.push_back(new LinearKernel());
break;
case Config::kKernelTypeGaussian:
m_kernels.push_back(new GaussianKernel(m_config.features[i].params[0]));
break;
case Config::kKernelTypeIntersection:
m_kernels.push_back(new IntersectionKernel());
break;
case Config::kKernelTypeChi2:
m_kernels.push_back(new Chi2Kernel());
break;
}
}
if (numFeatures > 1)
{
MultiFeatures* f = new MultiFeatures(m_features);
m_features.push_back(f);
MultiKernel* k = new MultiKernel(m_kernels, featureCounts);
m_kernels.push_back(k);
}
m_pLearner = new LaRank(m_config, *m_features.back(), *m_kernels.back());
}
void Tracker::Initialise(const cv::Mat& frame, FloatRect bb)
{
m_bb = IntRect(bb);
ImageRep image(frame, m_needsIntegralImage, m_needsIntegralHist);
for (int i = 0; i < 1; ++i)
{
UpdateLearner(image);
}
m_initialised = true;
}
void Tracker::Track(const cv::Mat& frame)
{
assert(m_initialised);
ImageRep image(frame, m_needsIntegralImage, m_needsIntegralHist);
vector<FloatRect> rects = Sampler::PixelSamples(m_bb, m_config.searchRadius);
vector<FloatRect> keptRects;
keptRects.reserve(rects.size());
for (int i = 0; i < (int)rects.size(); ++i)
{
if (!rects[i].IsInside(image.GetRect())) continue;
keptRects.push_back(rects[i]);
}
MultiSample sample(image, keptRects);
vector<double> scores;
m_pLearner->Eval(sample, scores);
double bestScore = -DBL_MAX;
int bestInd = -1;
for (int i = 0; i < (int)keptRects.size(); ++i)
{
if (scores[i] > bestScore)
{
bestScore = scores[i];
bestInd = i;
}
}
UpdateDebugImage(keptRects, m_bb, scores);
if (bestInd != -1)
{
m_bb = keptRects[bestInd];
UpdateLearner(image);
#if VERBOSE
cout << "track score: " << bestScore << endl;
#endif
}
}
void Tracker::UpdateDebugImage(const vector<FloatRect>& samples, const FloatRect& centre, const vector<double>& scores)
{
double mn = VectorXd::Map(&scores[0], scores.size()).minCoeff();
double mx = VectorXd::Map(&scores[0], scores.size()).maxCoeff();
m_debugImage.setTo(0);
for (int i = 0; i < (int)samples.size(); ++i)
{
int x = (int)(samples[i].XMin() - centre.XMin());
int y = (int)(samples[i].YMin() - centre.YMin());
m_debugImage.at<float>(m_config.searchRadius+y, m_config.searchRadius+x) = (float)((scores[i]-mn)/(mx-mn));
}
}
void Tracker::Debug()
{
imshow("tracker", m_debugImage);
m_pLearner->Debug();
}
void Tracker::UpdateLearner(const ImageRep& image)
{
// note these return the centre sample at index 0
vector<FloatRect> rects = Sampler::RadialSamples(m_bb, 2*m_config.searchRadius, 5, 16);
//vector<FloatRect> rects = Sampler::PixelSamples(m_bb, 2*m_config.searchRadius, true);
vector<FloatRect> keptRects;
keptRects.push_back(rects[0]); // the true sample
for (int i = 1; i < (int)rects.size(); ++i)
{
if (!rects[i].IsInside(image.GetRect())) continue;
keptRects.push_back(rects[i]);
}
#if VERBOSE
cout << keptRects.size() << " samples" << endl;
#endif
MultiSample sample(image, keptRects);
m_pLearner->Update(sample, 0);
}