-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopensurf.cpp
More file actions
369 lines (345 loc) · 11 KB
/
opensurf.cpp
File metadata and controls
369 lines (345 loc) · 11 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
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/* TOP-SURF: a visual words toolkit
Copyright (C) 2010 LIACS Media Lab, Leiden University,
Bart Thomee (bthomee@liacs.nl),
Erwin M. Bakker (erwin@liacs.nl) and
Michael S. Lew (mlew@liacs.nl).
This program 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.
See http://www.gnu.org/licenses/gpl.html for the full license.
This program 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.
In addition, this work is covered under the Creative Commons
Attribution license version 3.
See http://creativecommons.org/licenses/by/3.0/ for the full license.
*/
#include "opensurf.h"
#include "integral.h"
#include "fasthessian.h"
#include "surf.h"
OpenSurf::OpenSurf(int imagedim)
{
m_imagedim = imagedim;
}
OpenSurf::~OpenSurf()
{
}
bool OpenSurf::ExtractDescriptor(IplImage &image, float *&descriptor, int &ipoints)
{
// resize image
IplImage *small = cvCreateImage(cvSize(m_imagedim, m_imagedim), IPL_DEPTH_8U, 3);
cvResize(&image, small, CV_INTER_CUBIC);
// create the integral image
IplImage *integral = Integral(small);
cvReleaseImage(&small);
// start finding the SURF points
vector<OpenSurfInterestPoint> ivector;
ivector.reserve(2000);
// extract interest points
FastHessian fh(integral, ivector, OCTAVES, INTERVALS, INIT_SAMPLE, THRES);
fh.getIpoints();
if (ivector.empty())
{
ipoints = 0;
descriptor = NULL;
return true;
}
// extract descriptors
Surf des(integral, ivector);
des.getDescriptors(false);
cvReleaseImage(&integral);
// save the points
ipoints = (int)ivector.size();
descriptor = NEW float[ipoints * OPENSURF_FEATURECOUNT];
float *f = descriptor;
for (vector<OpenSurfInterestPoint>::const_iterator it = ivector.begin(); it != ivector.end(); ++it, f += OPENSURF_FEATURECOUNT)
memcpy(f, (*it).descriptor, OPENSURF_FEATURECOUNT * sizeof(float));
return true;
}
bool OpenSurf::ExtractDescriptor(IplImage &image, OpenSurfInterestPoint *&points, int &ipoints)
{
// resize image
IplImage *small = cvCreateImage(cvSize(m_imagedim, m_imagedim), IPL_DEPTH_8U, 3);
cvResize(&image, small, CV_INTER_CUBIC);
// create the integral image
IplImage *integral = Integral(small);
cvReleaseImage(&small);
// start finding the SURF points
vector<OpenSurfInterestPoint> ivector;
ivector.reserve(2000);
// extract interest points
FastHessian fh(integral, ivector, OCTAVES, INTERVALS, INIT_SAMPLE, THRES);
fh.getIpoints();
if (ivector.empty())
{
ipoints = 0;
points = NULL;
return true;
}
// extract descriptors
Surf des(integral, ivector);
des.getDescriptors(false);
cvReleaseImage(&integral);
// save the points
ipoints = (int)ivector.size();
points = NEW OpenSurfInterestPoint[ipoints];
OpenSurfInterestPoint *p = points;
for (vector<OpenSurfInterestPoint>::const_iterator it = ivector.begin(); it != ivector.end(); ++it, p++)
memcpy(p, &(*it), sizeof(OpenSurfInterestPoint));
return true;
}
float OpenSurf::CompareDescriptors(const float *descriptor1, int ipoints1, const float *descriptor2, int ipoints2)
{
// modified copy from getMatches function in ipoint.cpp
int matches = 0;
float d1, d2;
const float *temp1 = descriptor1;
const float *temp2;
for (int i = 0; i < ipoints1; i++, temp1 += OPENSURF_FEATURECOUNT)
{
d1 = d2 = FLT_MAX;
temp2 = descriptor2;
for (int j = 0; j < ipoints2; j++, temp2 += OPENSURF_FEATURECOUNT)
{
// calculate the distance between the descriptors of two interest points
float dist = 0.0f;
for (int k = 0; k < OPENSURF_FEATURECOUNT; k++)
dist += (temp1[k] - temp2[k]) * (temp1[k] - temp2[k]);
// check if this feature matches better than current best or second best
if (dist < d1)
{
d2 = d1;
d1 = dist;
}
else if (dist < d2)
d2 = dist;
}
// if match has a d1:d2 ratio < 0.65 ipoints are a match
// Note: it should be impossible for d2 to be zero. even when the best interest
// point match is a perfect fit, another interest point will have a different
// descriptor and consequently have a distance larger than zero
if (d1/d2 < 0.65f)
matches++;
}
// return 1 minus percentage of interest points that were matched
return 1.0f - ((float)matches / ipoints1);
}
float OpenSurf::CompareDescriptors(const OpenSurfInterestPoint *points1, int ipoints1, const OpenSurfInterestPoint *points2, int ipoints2)
{
// modified copy from getMatches function in ipoint.cpp
int matches = 0;
float d1, d2;
const OpenSurfInterestPoint *temp1 = points1;
const OpenSurfInterestPoint *temp2;
for (int i = 0; i < ipoints1; i++, temp1++)
{
d1 = d2 = FLT_MAX;
temp2 = points2;
for (int j = 0; j < ipoints2; j++, temp2++)
{
// only match if the laplacian corresponds
if (temp1->laplacian != temp2->laplacian)
continue;
// calculate the distance between the descriptors of two interest points
float dist = 0.0f;
for (unsigned int k = 0; k < OPENSURF_FEATURECOUNT; k++)
dist += (temp1->descriptor[k] - temp2->descriptor[k]) * (temp1->descriptor[k] - temp2->descriptor[k]);
// check if this feature matches better than current best or second best
if (dist < d1)
{
d2 = d1;
d1 = dist;
}
else if (dist < d2)
d2 = dist;
}
// if match has a d1:d2 ratio < 0.65 ipoints are a match
// Note: it should be impossible for d2 to be zero. even when the best interest
// point match is a perfect fit, another interest point will have a different
// descriptor and consequently have a distance larger than zero
if (d1/d2 < 0.65f)
matches++;
}
// return 1 minus percentage of interest points that were matched
return 1.0f - ((float)matches / ipoints1);
}
bool OpenSurf::LoadDescriptor(const char *fname, float *&descriptor, int &ipoints)
{
if (fname == NULL)
{
SAFE_FLUSHPRINT(stderr, "an empty filename was provided\n");
return false;
}
// open the file
FILE *file = fopen(fname, "rb");
if (file == NULL)
{
SAFE_FLUSHPRINT(stderr, "could not open %s\n", fname);
return false;
}
// load the descriptor
bool success = LoadDescriptor(file, descriptor, ipoints);
// close the file
fclose(file);
return success;
}
bool OpenSurf::LoadDescriptor(FILE *file, float *&descriptor, int &ipoints)
{
if (file == NULL)
goto failure;
// read the number of points
if (fread(&ipoints, sizeof(int), 1, file) != 1)
goto failure;
// read the point data
descriptor = NULL;
if (ipoints > 0)
{
descriptor = NEW float[ipoints * OPENSURF_FEATURECOUNT];
float *temp = descriptor;
for (int i = 0; i < ipoints; i++, temp+=OPENSURF_FEATURECOUNT)
{
if (fread(temp, sizeof(float), OPENSURF_FEATURECOUNT, file) != OPENSURF_FEATURECOUNT)
goto failure;
}
}
return true;
failure:
SAFE_FLUSHPRINT(stderr, "could not read from file\n");
SAFE_DELETE_ARRAY(descriptor);
return false;
}
bool OpenSurf::LoadDescriptor(const char *fname, OpenSurfInterestPoint *&points, int &ipoints)
{
if (fname == NULL)
{
SAFE_FLUSHPRINT(stderr, "an empty filename was provided\n");
return false;
}
// open the file
FILE *file = fopen(fname, "rb");
if (file == NULL)
{
SAFE_FLUSHPRINT(stderr, "could not open %s\n", fname);
return false;
}
// load the descriptor
bool success = LoadDescriptor(file, points, ipoints);
// close the file
fclose(file);
return success;
}
bool OpenSurf::LoadDescriptor(FILE *file, OpenSurfInterestPoint *&points, int &ipoints)
{
if (file == NULL)
goto failure;
// read the number of points
if (fread(&ipoints, sizeof(int), 1, file) != 1)
goto failure;
// read the point data
points = NULL;
if (ipoints > 0)
{
points = NEW OpenSurfInterestPoint[ipoints];
for (int i = 0; i < ipoints; i++)
{
if (fread(&points[i].x, sizeof(float), 1, file) != 1 ||
fread(&points[i].y, sizeof(float), 1, file) != 1 ||
fread(&points[i].scale, sizeof(float), 1, file) != 1 ||
fread(&points[i].orientation, sizeof(float), 1, file) != 1 ||
fread(&points[i].laplacian, sizeof(int), 1, file) != 1 ||
fread(points[i].descriptor, sizeof(float), OPENSURF_FEATURECOUNT, file) != OPENSURF_FEATURECOUNT)
goto failure;
}
}
return true;
failure:
SAFE_FLUSHPRINT(stderr, "could not read from file\n");
SAFE_DELETE_ARRAY(points);
return false;
}
bool OpenSurf::SaveDescriptor(const char *fname, const float *descriptor, int ipoints)
{
if (fname == NULL)
{
SAFE_FLUSHPRINT(stderr, "an empty filename was provided\n");
return false;
}
// open the file
FILE *file = fopen(fname, "wb");
if (file == NULL)
{
SAFE_FLUSHPRINT(stderr, "could not open %s\n", fname);
return false;
}
// save the descriptor
bool success = SaveDescriptor(file, descriptor, ipoints);
// close the file
fclose(file);
return success;
}
bool OpenSurf::SaveDescriptor(FILE *file, const float *descriptor, int ipoints)
{
const float *temp;
if (file == NULL)
goto failure;
// save the number of points
if (fwrite(&ipoints, sizeof(int), 1, file) != 1)
goto failure;
// save the point data
temp = descriptor;
for (int i = 0; i < ipoints; i++, temp+=OPENSURF_FEATURECOUNT)
{
if (fwrite(temp, sizeof(float), OPENSURF_FEATURECOUNT, file) != OPENSURF_FEATURECOUNT)
goto failure;
}
return true;
failure:
SAFE_FLUSHPRINT(stderr, "could not write to file\n");
return false;
}
bool OpenSurf::SaveDescriptor(const char *fname, const OpenSurfInterestPoint *points, int ipoints)
{
if (fname == NULL)
{
SAFE_FLUSHPRINT(stderr, "an empty filename was provided\n");
return false;
}
// open the file
FILE *file = fopen(fname, "wb");
if (file == NULL)
{
SAFE_FLUSHPRINT(stderr, "could not open %s\n", fname);
return false;
}
// save the descriptor
bool success = SaveDescriptor(file, points, ipoints);
// close the file
fclose(file);
return success;
}
bool OpenSurf::SaveDescriptor(FILE *file, const OpenSurfInterestPoint *points, int ipoints)
{
if (file == NULL)
goto failure;
// save the number of points
if (fwrite(&ipoints, sizeof(int), 1, file) != 1)
goto failure;
// save the point data
for (int i = 0; i < ipoints; i++)
{
if (fwrite(&points[i].x, sizeof(float), 1, file) != 1 ||
fwrite(&points[i].y, sizeof(float), 1, file) != 1 ||
fwrite(&points[i].scale, sizeof(float), 1, file) != 1 ||
fwrite(&points[i].orientation, sizeof(float), 1, file) != 1 ||
fwrite(&points[i].laplacian, sizeof(int), 1, file) != 1 ||
fwrite(points[i].descriptor, sizeof(float), OPENSURF_FEATURECOUNT, file) != OPENSURF_FEATURECOUNT)
goto failure;
}
return true;
failure:
SAFE_FLUSHPRINT(stderr, "could not write to file\n");
return false;
}