topsurf.cpp
7.36 KB
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
#include <topsurf/descriptor.h>
#include <topsurf/topsurf.h>
#include <mm_plugin.h>
#include "common/opencvutils.h"
#include "common/qtutils.h"
#include "common/resource.h"
using namespace cv;
using namespace mm;
using namespace std;
class TopSurfInitializer : public Initializer
{
void initialize() const
{
Globals.Abbreviations.insert("TopSurf", "Open!TopSurfExtract(40000):TopSurfCompare");
Globals.Abbreviations.insert("TopSurfM", "Open!TopSurfExtract(1000000):TopSurfCompare");
Globals.Abbreviations.insert("TopSurfKNN", "Open!TopSurfExtract+TopSurfKNN");
Globals.Abbreviations.insert("DocumentClassification", "TopSurfKNN");
}
void finalize() const {}
};
MM_REGISTER(Initializer, TopSurfInitializer, false)
class TopSurfResourceMaker : public ResourceMaker<TopSurf>
{
QString file;
public:
TopSurfResourceMaker(const QString &dictionary)
{
file = Globals.SDKPath + "/models/topsurf/dictionary_" + dictionary;
}
private:
TopSurf *make() const
{
TopSurf *topSurf = new TopSurf(256, 100);
if (!topSurf->LoadDictionary(qPrintable(file)))
qFatal("TopSurfResourceMaker::make failed to load dictionary.");
return topSurf;
}
};
/****
TopSurfExtract
Wrapper to TopSurf::ExtractDescriptor()
B. Thomee, E.M. Bakker, and M.S. Lew, "TOP-SURF: a visual words toolkit",
in Proceedings of the 18th ACM International Conference on Multimedia, pp. 1473-1476, Firenze, Italy, 2010.
****/
class TopSurfExtract : public UntrainableFeature
{
Q_OBJECT
Q_PROPERTY(QString dictionary READ get_dictionary WRITE set_dictionary)
MM_MEMBER(QString, dictionary)
Resource<TopSurf> topSurfResource;
public:
TopSurfExtract() : topSurfResource(new TopSurfResourceMaker("10000")) {}
private:
void init()
{
topSurfResource.setResourceMaker(new TopSurfResourceMaker(dictionary));
}
void project(const Template &src, Template &dst) const
{
// Compute descriptor (not thread safe)
TopSurf *topSurf = topSurfResource.acquire();
TOPSURF_DESCRIPTOR descriptor;
IplImage iplSrc = src.m();
if (!topSurf->ExtractDescriptor(iplSrc, descriptor))
qFatal("TopSurfExtract::project ExtractDescriptor failure.");
topSurfResource.release(topSurf);
// Copy descriptor and clean up
unsigned char *data;
int length;
Descriptor2Array(descriptor, data, length);
Mat m(1, length, CV_8UC1);
memcpy(m.data, data, length);
delete data;
TopSurf::ReleaseDescriptor(descriptor);
dst = m;
}
public:
static QString args()
{
return "10000|20000|40000 dictionary = 10000";
}
static TopSurfExtract *make(const QStringList &args)
{
(void) args;
return new TopSurfExtract();
}
};
MM_REGISTER(Feature, TopSurfExtract, true)
class TopSurfHist : public UntrainableFeature
{
Q_OBJECT
Q_PROPERTY(int size READ get_size WRITE set_size)
MM_MEMBER(int, size)
void project(const Template &src, Template &dst) const
{
TOPSURF_DESCRIPTOR td;
Array2Descriptor(src.m().data, td);
Mat m(1, size, CV_32FC1);
m.setTo(0);
for (int i=0; i<td.count; i++)
m.at<float>(0, td.visualword[i].identifier % size)++;
TopSurf::ReleaseDescriptor(td);
dst = m;
}
public:
static QString args()
{
return "int size = 10000";
}
static TopSurfHist *make(const QStringList &args)
{
(void) args;
return new TopSurfHist();
}
};
MM_REGISTER(Feature, TopSurfHist, true)
// Wrapper around TopSurf CompareDescriptors
float TopSurfSimilarity(const Mat &a, const Mat &b, bool cosine)
{
TOPSURF_DESCRIPTOR tda, tdb;
Array2Descriptor(a.data, tda);
Array2Descriptor(b.data, tdb);
float result;
if (cosine) result = TopSurf::CompareDescriptorsCosine(tda, tdb);
else result = TopSurf::CompareDescriptorsAbsolute(tda, tdb);
TopSurf::ReleaseDescriptor(tda);
TopSurf::ReleaseDescriptor(tdb);
return result;
}
/****
TopSurfCompare
Wrapper to TopSurf_CompareDescriptors()
B. Thomee, E.M. Bakker, and M.S. Lew, "TOP-SURF: a visual words toolkit",
in Proceedings of the 18th ACM International Conference on Multimedia, pp. 1473-1476, Firenze, Italy, 2010.
****/
class TopSurfCompare : public ComparerBase
{
Q_OBJECT
Q_PROPERTY(bool cosine READ get_cosine WRITE set_cosine)
MM_MEMBER(bool, cosine)
float compare(const Mat &a, const Mat &b) const
{
return TopSurfSimilarity(a, b, cosine);
}
public:
static QString args()
{
return "bool cosine = 1";
}
static TopSurfCompare *make(const QStringList &args)
{
(void) args;
return new TopSurfCompare();
}
};
MM_REGISTER(Comparer, TopSurfCompare, true)
/****
TopSurfKNN
KNN classifier for TopSurf features.
****/
class TopSurfKNN : public Feature
{
Q_OBJECT
Q_PROPERTY(int k READ get_k WRITE set_k)
Q_PROPERTY(bool cosine READ get_cosine WRITE set_cosine)
MM_MEMBER(int, k)
MM_MEMBER(bool, cosine)
TemplateList data;
private:
void train(const TemplateList &data)
{
this->data = data;
}
void project(const Template &src, Template &dst) const
{
// Compute distance to each descriptor
QList< QPair<float, int> > distances; // <distance, label>
distances.reserve(data.size());
foreach (const Template &t, data)
distances.append(QPair<float, int>(TopSurfSimilarity(src, t, cosine), t.file.label()));
// Find nearest neighbors
qSort(distances);
QHash<int, QPair<int, float> > counts; // <label, <count, cumulative distance>>
for (int i=0; i<k; i++) {
QPair<float,int> &distance = distances[i];
QPair<int,float> &count = counts[distance.second];
count.first++;
count.second += distance.first;
}
// Find most occuring label
int best_label = -1;
int best_count = 0;
float best_distance = numeric_limits<float>::max();
foreach (int label, counts.keys()) {
const QPair<int, float> &count = counts[label];
if ((count.first > best_count) || ((count.first == best_count) && (count.second < best_distance))) {
best_label = label;
best_count = count.first;
best_distance = count.second;
}
}
assert(best_label != -1);
// Measure confidence
int rest_count = 0;
float rest_distance = 0;
foreach (int label, counts.keys()) {
if (label != best_label) {
const QPair<int, float> &count = counts[label];
rest_count = count.first;
rest_distance = count.second;
}
}
dst = src;
dst.file["Label"] = best_label;
dst.file["Confidence"] = (float)best_count/(float)k;
}
void store(QDataStream &stream) const
{
stream << data;
}
void load(QDataStream &stream)
{
stream >> data;
}
public:
static QString args()
{
return "int k, int cosine = 1";
}
static TopSurfKNN *make(const QStringList &args)
{
(void) args;
return new TopSurfKNN();
}
};
MM_REGISTER(Feature, TopSurfKNN, true)
#include "topsurf.moc"