-
Notifications
You must be signed in to change notification settings - Fork 7
/
cpuFeatureMatcher.h
369 lines (322 loc) · 11.9 KB
/
cpuFeatureMatcher.h
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
#include <iostream>
#include <cassert>
#include "Matrix.h"
#include <vector>
#include <set>
#include <cfloat>
typedef std::pair<int, int> match;
/*
*Pre-conditions:
* `descriptor1` and `descriptor2` are each arrays of length `length`
*Post-conditions:
* Returns the L2 distance of the two descriptors interpreted as vectors.
*/
float computeL2Distance(float* descriptor1, float* descriptor2, int length) {
float distance = 0;
for (int i = 0; i < length; ++i) {
distance += pow(descriptor1[i] - descriptor2[i], 2);
}
return pow(distance, 0.5);
}
/*
*Pre-conditions:
* `descriptors` is a n x k matrix
* `distanceMat` is a n x n matrix
*Post-conditions:
* distanceMat[j][i] is the Euclidean distance from descriptors[j][:]
* to descriptors[i][:]. Note that Euclidean distance is symmetric.
*Known bugs:
* binary produces no stdout with n = 10000 and k = 32.
*/
void cpuComputeDistanceMat(const Matrix<float> descriptors, Matrix<float> distanceMat) {
assert(descriptors.height == distanceMat.height);
assert(distanceMat.height == distanceMat.width);
int n = descriptors.height;
int k = descriptors.width;
for (int j = 0; j < n; ++j) {
for (int i = 0; i < n; ++i) {
float* descriptor1 = descriptors.elements + j * k;
float* descriptor2 = descriptors.elements + i * k;
float* dst = distanceMat.elements + j * n + i;
*dst = computeL2Distance(descriptor1, descriptor2, k);
}
}
}
/*
*Pre-conditions:
* `A` and `B` are of the same size and are on host memory.
*Post-conditions:
* Returns the root mean squared error between A and B
* if number of elements is more than 0, else returns 0
*/
template <typename T>
float getRMSE(const Matrix<T> A, const Matrix<T> B) {
assert(A.height == B.height);
assert(A.width == B.width);
int numel = A.height * A.width; // number of elements
if (numel == 0) { return 0; }
float sse = 0; // sum of squared error
for (int i = 0; i < numel; ++i) {
sse = pow(A.elements[i] - B.elements[i], 2);
}
return sqrt(sse / numel);
}
/*
*Pre-conditions:
* start < stop
* 0 <= start < mat.height
* 0 < start <= mat.height
*Post-conditions:
* Returns submat, which is a view of mat[start:stop][:]
* submat contains rows start, start+1, ..., stop-1 of mat
*/
template<typename T>
Matrix<T> getSubmatrix(const Matrix<T> mat, const int start, const int stop) {
assert(start < stop);
assert(0 <= start && start < mat.height);
assert(0 < stop && stop <= mat.height);
Matrix<T> submat;
submat.width = mat.width;
submat.height = stop - start;
submat.elements = mat.elements + start * submat.width;
return submat;
}
/*
*Pre-conditions:
* mat.height >= 2
* 0 <= col < mat.width
*Post-conditions:
* mat[idx1][col] and mat[idx2][col] are the smallest and second smallest
* elements in mat[:][col]
*/
template<typename T>
void getIndexOfTwoSmallestInColumn(const Matrix<T> mat, int col, int& idx1, int& idx2) {
assert(mat.height >= 2);
assert(0 <= col && col < mat.width);
float val1 = FLT_MAX, val2 = FLT_MAX;
idx1 = -1, idx2 = -1;
for (int j = 0; j < mat.height; ++j) {
float currVal = mat.elements[j * mat.width + col];
// Push current value to first place
if (currVal < val1) {
val2 = val1;
idx2 = idx1;
val1 = currVal;
idx1 = j;
}
// Push current value to second place
else if (currVal < val2) {
val2 = currVal;
idx2 = j;
}
}
}
/*
*Computes the correspondence of one feature given its distance from other
*features
*Pre-conditions:
* `distances` is an array of length `len`
* distances[i] is the distance of a fixed feature to feature i
*Post-conditions:
* Returns index i, such that feature i corresponds to the fixed feature, if
* there is correspondence, else returns -1
*/
int computeCorrespondenceScalar(float* distances, int len, float matchConfidence) {
Matrix<float> M;
M.height = len;
M.width = 1;
M.elements = distances;
int idx1, idx2;
getIndexOfTwoSmallestInColumn(M, 0, idx1, idx2);
float dist1 = distances[idx1];
float dist2 = distances[idx2];
if (dist1 / dist2 < 1 - matchConfidence) {
return idx1;
}
else {
return -1;
}
}
/*
*Computes the correspondence of descriptor[idxFrom] against descriptors
*descriptor[idxToStart:idxToStop] (excludes idxToStop)
*Pre-conditions:
* descriptors is a n x k matrx
* 0 <= idxFrom, idxToStart < n
* 0 < idxToStop <= n
* idxToStart < idxToStop
*Post-conditions:
* Returns i s.t. descriptor[idxFrom] corresponds to descriptor idxToStart+i,
* if there is correspondence, else returns -1
* 0 <= i < idxToStop-idxToStart
*/
int computeCorrespondenceScalar(const Matrix<float> descriptors,
int idxFrom, int idxToStart, int idxToStop, float matchConfidence) {
assert(0 <= idxFrom && idxFrom < descriptors.height);
assert(0 <= idxToStart && idxToStart < descriptors.height);
assert(0 < idxToStop && idxToStop <= descriptors.height);
int n_i = idxToStop - idxToStart;
int k = descriptors.width;
float distances[n_i];
float* descriptorFrom = descriptors.elements + k * idxFrom;
for (int j = 0; j < n_i; ++j) {
float* descriptorTo = descriptors.elements + k * (j + idxToStart);
distances[j] = computeL2Distance(descriptorFrom, descriptorTo, k);
}
return computeCorrespondenceScalar(distances, n_i, matchConfidence);
}
/*
*Compute correspondence matrix given descriptors. This has better space
*complexity than first computing the full distance matrix
*Pre-conditions:
* `descriptors` is a n x k matrix
* cumNumDescriptors is an array of length `numImages`
* cumNumDescriptors[numImages-1] = n
* correspondenceMat is a numImages x n matrix
*Post-conditions:
* correspondenceMat[j][i] is the i-th feature's corresponding feature in
* image j. It is -1 if there's no correspondence.
* -1 <= correspondenceMat[j][i] < n_j, where n_j is the number of features
* in image j.
*/
void computeCorrespondenceMatFromDescriptors(
const Matrix<float> descriptors,
int* cumNumDescriptors,
int numImages, Matrix<int> correspondenceMat, float matchConfidence) {
assert(correspondenceMat.height == numImages);
assert(correspondenceMat.width == descriptors.height);
assert(cumNumDescriptors[numImages-1] == descriptors.height);
int n = descriptors.height;
for (int imgIdx = 0; imgIdx < numImages; ++imgIdx) {
int idxToStart = imgIdx > 0 ? cumNumDescriptors[imgIdx-1] : 0;
int idxToStop = cumNumDescriptors[imgIdx];
for (int idxFrom = 0; idxFrom < n; ++idxFrom) {
int* dst = correspondenceMat.elements + imgIdx * n + idxFrom;
*dst = computeCorrespondenceScalar(descriptors, idxFrom,
idxToStart, idxToStop, matchConfidence);
}
}
}
/*
*Pre-conditions:
* `distanceSubmat` is a n_i x n matrix
* `correspondenceMat` is a 1 x n matrix
*Post-conditions:
* See `computeCorrespondenceMat`
*/
void computeCorrespondenceVec(const Matrix<float> distanceSubmat,
Matrix<int> correspondenceMat, float matchConfidence) {
assert(correspondenceMat.height == 1);
assert(correspondenceMat.width == distanceSubmat.width);
int n = distanceSubmat.width;
for (int i = 0; i < n; ++i) {
int idx1, idx2;
getIndexOfTwoSmallestInColumn(distanceSubmat, i, idx1, idx2);
float dist1 = distanceSubmat.elements[idx1 * distanceSubmat.width + i];
float dist2 = distanceSubmat.elements[idx2 * distanceSubmat.width + i];
if (dist1 / dist2 < 1 - matchConfidence) {
correspondenceMat.elements[i] = idx1;
}
else {
correspondenceMat.elements[i] = -1;
}
}
}
/*
*Pre-conditions:
* `distanceMat` is a n x n matrix
* `distanceMat` is the result of computeDistanceMat()
* cumNumDescriptors is an array of length `numImages`
* cumNumDescriptors[numImages-1] = distanceMat.height
* correspondenceMat is a numImages * n matrix
*
* Let start = cumNumDescriptors[i-1], stop = cumNumDescriptors[i].
* Then distanceMat[start:stop][:] refers to descriptors of image i.
*Post-conditions:
* correspondenceMat[j][i] is the i-th feature's corresponding feature in
* image j. It is -1 if there's no correspondence.
* -1 <= correspondenceMat[j][i] < n_j, where n_j is the number of features
* in image j.
*/
void computeCorrespondenceMat(const Matrix<float> distanceMat,
int* cumNumDescriptors,
int numImages, Matrix<int> correspondenceMat, float matchConfidence) {
assert(distanceMat.height == distanceMat.width);
assert(correspondenceMat.height == numImages);
assert(correspondenceMat.width == distanceMat.width);
assert(cumNumDescriptors[numImages-1] == distanceMat.height);
for (int imgIdx = 0; imgIdx < numImages; ++imgIdx) {
int start = imgIdx > 0 ? cumNumDescriptors[imgIdx-1] : 0;
int stop = cumNumDescriptors[imgIdx];
Matrix<float> src = getSubmatrix<float>(distanceMat, start, stop);
Matrix<int> dst = getSubmatrix<int>(correspondenceMat, imgIdx, imgIdx + 1);
computeCorrespondenceVec(src, dst, matchConfidence);
}
}
/*
*Pre-conditions:
* src and dst are arrays of length `length`.
* length > 0
*Post-conditions:
* dst is the cumulative sum of src.
*/
template<typename T>
void cumsum(T* src, T* dst, int length) {
assert(length > 0);
dst[0] = src[0];
for (int i = 1; i < length; ++i) {
dst[i] = dst[i-1] + src[i];
}
}
/*
*Pre-conditions:
* `allDescriptors` is a n x k matrix
* numDescriptors = [n1, n2, ..., n_m] where n_i is the number of descriptors
* of image i and m is numImages
* n = sum(n1, n2, ..., n_m)
*Post-conditions:
* Prints the feature correspondence between each image pair, based on
* OpenCV's Best2NearestMatcher
*/
void printCorrespondence(const Matrix<float>& allDescriptors,
int* numDescriptors, int numImages, float matchConf) {
std::vector<std::set<match> > correspondenceSets;
// cumsum of n_i = [n_0, n_0 + n_1, ..., n]
int* cumNumDesc = (int*)malloc(sizeof(int) * (numImages));
cumsum<int>(numDescriptors, cumNumDesc, numImages);
// Compute distance matrix
int n = allDescriptors.height;
Matrix<float> distanceMat = AllocateMatrix<float>(n, n, 0);
cpuComputeDistanceMat(allDescriptors, distanceMat);
// Compute correspondence matrix
Matrix<int> corrMat = AllocateMatrix<int>(numImages, n, 0);
computeCorrespondenceMat(distanceMat, cumNumDesc, numImages,
corrMat, matchConf);
std::cout << "distanceMat\n";
printMatrix(distanceMat);
std::cout << "corrMat\n";
printMatrix(corrMat);
for (int imgIdx1 = 0; imgIdx1 < numImages - 1; ++imgIdx1) {
for (int imgIdx2 = imgIdx1 + 1; imgIdx2 < numImages; ++imgIdx2) {
int numDescriptors1 = numDescriptors[imgIdx1];
int numDescriptors2 = numDescriptors[imgIdx2];
int* src = NULL;
// Correspondence from imgIdx2 to imgIdx1
src = corrMat.elements + imgIdx1 * n + cumNumDesc[imgIdx2-1];
for (int i = 0; i < numDescriptors2; ++i) {
if (src[i] > -0.5) {
printf("im%i-%i corr %i-%i\n", imgIdx1, imgIdx2, src[i], i);
}
}
// Correspondence from imgIdx1 to imgIdx2
src = corrMat.elements + imgIdx2 * n +
(imgIdx1 > 0 ? cumNumDesc[imgIdx1-1] : 0);
for (int i = 0; i < numDescriptors1; ++i) {
if (src[i] > -0.5) {
printf("im%i-%i corr %i-%i\n", imgIdx1, imgIdx2, i, src[i]);
}
}
}
}
free(cumNumDesc);
}