-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.cpp
452 lines (386 loc) · 14.4 KB
/
main.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
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
#include <iostream>
#include <cmath>
#include <iomanip>
#include <string>
#include <sstream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include "cuImage.h"
#include "cuSIFT.h"
#include "extras/matching.h"
#include "extras/homography.h"
#include "extras/debug.h"
using namespace std;
int ImproveHomography(SiftData &data, float *homography, int numLoops, float minScore, float maxAmbiguity, float thresh);
void PrintMatchData(SiftData &siftData1, SiftData &siftData2, cuImage &img);
void MatchAll(SiftData &siftData1, SiftData &siftData2, float *homography);
void demo(int argc, char **argv);
void testMatchingDotProduct(int argc, char **argv);
void testMatchingL2(int argc, char **argv);
void compareMatchingWithMATLAB(int argc, char **argv);
///////////////////////////////////////////////////////////////////////////////
// Main program
///////////////////////////////////////////////////////////////////////////////
// TODO: Move most functions into test
// TODO: refactor demo
int main(int argc, char **argv)
{
demo(argc, argv);
// testMatchingDotProduct(argc, argv);
// testMatchingL2(argc, argv);
// compareMatchingWithMATLAB(argc, argv);
}
void compareMatchingWithMATLAB(int argc, char **argv) {
int numFrames = 284;
vector<SiftData> data(numFrames);
for (int i = 0; i < numFrames; i++) {
cerr << "Reading sift data for frame " << i + 1 << endl;
std::ostringstream path;
path << "../../../../../result/sift/sift";
path << i + 1;
ReadVLFeatSiftData(data[i], path.str().c_str());
}
for (int i = 0; i < numFrames - 1; i++) {
vector<SiftMatch *> ours = MatchSiftData(data[i], data[i + 1], MatchSiftDistanceL2, 999, 0.6);
std::ostringstream path;
path << "../../../../../result/match/orig_match";
path << i + 1;
path << "_";
path << i + 2;
vector<SiftMatch *> theirs = ReadMATLABMatchDataBeforeRANSAC(path.str().c_str());
cerr << ours.size() - theirs.size() << endl;
}
}
void testMatchingL2(int argc, char **argv) {
char *limgPath = argv[1];
char *rimgPath = argv[2];
// Read images using OpenCV
cv::Mat limg, rimg;
cv::imread(limgPath, 0).convertTo(limg, CV_32FC1);
cv::imread(rimgPath, 0).convertTo(rimg, CV_32FC1);
unsigned int w = limg.cols;
unsigned int h = limg.rows;
cout << "Image size = (" << w << "," << h << ")" << endl;
// Perform some initial blurring (if needed)
cv::GaussianBlur(limg, limg, cv::Size(3, 3), 0.5);
cv::GaussianBlur(rimg, rimg, cv::Size(3, 3), 0.5);
// Initial Cuda images and download images to device
cout << "Initializing data..." << endl;
InitCuda(0);
cuImage img1, img2;
img1.Allocate(w, h, iAlignUp(w, 128), false, NULL, (float*)limg.data);
img2.Allocate(w, h, iAlignUp(w, 128), false, NULL, (float*)rimg.data);
img1.HostToDevice();
img2.HostToDevice();
// Extract Sift features from images
SiftData siftData1, siftData2;
float initBlur = 0.0f;
float thresh = 0.1f;
int numSift = 4096;
InitSiftData(siftData1, numSift, true, true);
InitSiftData(siftData2, numSift, true, true);
ExtractSift(siftData1, img1, 6, initBlur, thresh, 0.0f);
ExtractSift(siftData2, img2, 6, initBlur, thresh, 0.0f);
vector<SiftMatch *> matches = MatchSiftData(siftData1, siftData2, MatchSiftDistanceL2);
// float total = 0.0f;
// for (int i = 0; i < matches.size(); i++) {
// float sum = 0.0f;
// for (int j = 0; j < 128; j++) {
// sum += matches[i]->pt1->data[j] * matches[i]->pt2->data[j];
// }
// float diff = matches[i]->score - sum;
// // cerr << "Match " << i << " score " <<
// // matches[i]->score << ", " << sum << " " << diff << endl;
// total += abs(diff);
// }
// cerr << "Total error " << total << endl;
vector<SiftMatch *> matchesCPU(numSift);
for (int i = 0; i < numSift; i++) {
float minScore = 999.0;
float minScore2 = 999.0;
int minIndex = 999;
for (int j = 0; j < numSift; j++) {
float score = 0.0;
for (int k = 0; k < 128; k++) {
score += (siftData1.h_data[i].data[k] - siftData2.h_data[j].data[k]) * (siftData1.h_data[i].data[k] - siftData2.h_data[j].data[k]);
}
if (score < minScore) {
minScore2 = minScore;
minScore = score;
minIndex = j;
} else if (score < minScore2) {
minScore2 = score;
}
}
SiftMatch *siftMatch = new SiftMatch();
siftMatch->pt1 = &(siftData1.h_data[i]);
siftMatch->pt2 = &(siftData2.h_data[minIndex]);
siftMatch->score = minScore;
siftMatch->ambiguity = minScore / (minScore2 + 1e-6);
matchesCPU[i] = siftMatch;
}
float scoreSum = 0.0;
float ambiguitySum = 0.0;
for (int i = 0; i < numSift; i++) {
scoreSum += abs(matches[i]->score - matchesCPU[i]->score);
ambiguitySum += abs(matches[i]->ambiguity - matchesCPU[i]->ambiguity);
// cerr << matches[i]->score - matchesCPU[i]->score << endl;
// cerr << matches[i]->ambiguity - matchesCPU[i]->ambiguity << endl;
}
cerr << "Average score difference: " << scoreSum / numSift << endl;
cerr << "Average ambiguity difference: " << ambiguitySum / numSift << endl;
cerr << "Total score difference: " << scoreSum << endl;
cerr << "Total ambiguity difference: " << ambiguitySum << endl;
for (int i = 0; i < matches.size(); i++) {
delete matches[i];
}
for (int i = 0; i < matchesCPU.size(); i++) {
delete matchesCPU[i];
}
FreeSiftData(siftData1);
FreeSiftData(siftData2);
}
void testMatchingDotProduct(int argc, char **argv) {
char *limgPath = argv[1];
char *rimgPath = argv[2];
// Read images using OpenCV
cv::Mat limg, rimg;
cv::imread(limgPath, 0).convertTo(limg, CV_32FC1);
cv::imread(rimgPath, 0).convertTo(rimg, CV_32FC1);
unsigned int w = limg.cols;
unsigned int h = limg.rows;
cout << "Image size = (" << w << "," << h << ")" << endl;
// Perform some initial blurring (if needed)
cv::GaussianBlur(limg, limg, cv::Size(3, 3), 0.5);
cv::GaussianBlur(rimg, rimg, cv::Size(3, 3), 0.5);
// Initial Cuda images and download images to device
cout << "Initializing data..." << endl;
InitCuda(0);
cuImage img1, img2;
img1.Allocate(w, h, iAlignUp(w, 128), false, NULL, (float*)limg.data);
img2.Allocate(w, h, iAlignUp(w, 128), false, NULL, (float*)rimg.data);
img1.HostToDevice();
img2.HostToDevice();
// Extract Sift features from images
SiftData siftData1, siftData2;
float initBlur = 0.0f;
float thresh = 0.1f;
int numSift = 4096;
InitSiftData(siftData1, numSift, true, true);
InitSiftData(siftData2, numSift, true, true);
ExtractSift(siftData1, img1, 6, initBlur, thresh, 0.0f);
ExtractSift(siftData2, img2, 6, initBlur, thresh, 0.0f);
vector<SiftMatch *> matches = MatchSiftData(siftData1, siftData2, MatchSiftDistanceDotProduct);
float total = 0.0f;
for (int i = 0; i < matches.size(); i++) {
float sum = 0.0f;
for (int j = 0; j < 128; j++) {
sum += matches[i]->pt1->data[j] * matches[i]->pt2->data[j];
}
float diff = matches[i]->score - sum;
// cerr << "Match " << i << " score " <<
// matches[i]->score << ", " << sum << " " << diff << endl;
total += abs(diff);
}
cerr << "Total error " << total << endl;
vector<SiftMatch *> matchesCPU(numSift);
for (int i = 0; i < numSift; i++) {
float maxScore = -1.0;
float maxScore2 = -1.0;
int maxIndex = -1;
for (int j = 0; j < numSift; j++) {
float score = 0.0;
for (int k = 0; k < 128; k++) {
score += siftData1.h_data[i].data[k] * siftData2.h_data[j].data[k];
}
if (score > maxScore) {
maxScore2 = maxScore;
maxScore = score;
maxIndex = j;
} else if (score > maxScore2) {
maxScore2 = score;
}
}
SiftMatch *siftMatch = new SiftMatch();
siftMatch->pt1 = &(siftData1.h_data[i]);
siftMatch->pt2 = &(siftData2.h_data[maxIndex]);
siftMatch->score = maxScore;
siftMatch->ambiguity = (1 - maxScore) / (1 - maxScore2 + 1e-6);
matchesCPU[i] = siftMatch;
}
float scoreSum = 0.0;
float ambiguitySum = 0.0;
for (int i = 0; i < numSift; i++) {
scoreSum += abs(matches[i]->score - matchesCPU[i]->score);
ambiguitySum += abs(matches[i]->ambiguity - matchesCPU[i]->ambiguity);
// cerr << matches[i]->score - matchesCPU[i]->score << endl;
// cerr << matches[i]->ambiguity - matchesCPU[i]->ambiguity << endl;
}
cerr << "Average score difference: " << scoreSum / numSift << endl;
cerr << "Average ambiguity difference: " << ambiguitySum / numSift << endl;
cerr << "Total score difference: " << scoreSum << endl;
cerr << "Total ambiguity difference: " << ambiguitySum << endl;
for (int i = 0; i < matches.size(); i++) {
delete matches[i];
}
for (int i = 0; i < matchesCPU.size(); i++) {
delete matchesCPU[i];
}
FreeSiftData(siftData1);
FreeSiftData(siftData2);
}
void demo(int argc, char **argv) {
if (argc < 4) {
cout << "Usage: ./cuSIFT leftImagePath rightImagePath outDir numDevices" << endl;
exit(1);
}
char *limgPath = argv[1];
char *rimgPath = argv[2];
char *outDir = argv[3];
int devNum = 0;
if (argc > 4)
devNum = atoi(argv[4]);
// Read images using OpenCV
cv::Mat limg, rimg;
cv::imread(limgPath, 0).convertTo(limg, CV_32FC1);
cv::imread(rimgPath, 0).convertTo(rimg, CV_32FC1);
unsigned int w = limg.cols;
unsigned int h = limg.rows;
cout << "Image size = (" << w << "," << h << ")" << endl;
// Perform some initial blurring (if needed)
cv::GaussianBlur(limg, limg, cv::Size(3, 3), 0.5);
cv::GaussianBlur(rimg, rimg, cv::Size(3, 3), 0.5);
// Initial Cuda images and download images to device
cout << "Initializing data..." << endl;
InitCuda(devNum);
cuImage img1, img2;
img1.Allocate(w, h, iAlignUp(w, 128), false, NULL, (float*)limg.data);
img2.Allocate(w, h, iAlignUp(w, 128), false, NULL, (float*)rimg.data);
img1.HostToDevice();
img2.HostToDevice();
// Extract Sift features from images
SiftData siftData1, siftData2;
float initBlur = 0.0f;
float thresh = 0.1f;
InitSiftData(siftData1, 4096, true, true);
InitSiftData(siftData2, 4096, true, true);
ExtractSift(siftData1, img1, 6, initBlur, thresh, 0.0f);
ExtractSift(siftData2, img2, 6, initBlur, thresh, 0.0f);
// Match Sift features and find a homography
MatchSiftData(siftData1, siftData2);
float homography[9];
int numMatches;
FindHomography(siftData1, homography, &numMatches, 10000, 0.00f, 0.80f, 5.0);
int numFit = ImproveHomography(siftData1, homography, 5, 0.00f, 0.80f, 3.0);
// Print out and store summary data
PrintMatchData(siftData1, siftData2, img1);
#if 0
PrintSiftData(siftData1);
MatchAll(siftData1, siftData2, homography);
#endif
cout << "Number of original features: " << siftData1.numPts << " " << siftData2.numPts << endl;
cout << "Number of matching features: " << numFit << " " << numMatches << " " << 100.0f*numMatches/min(siftData1.numPts, siftData2.numPts) << "%" << endl;
cv::imwrite(outDir, limg);
// Free Sift data from device
FreeSiftData(siftData1);
FreeSiftData(siftData2);
}
void MatchAll(SiftData &siftData1, SiftData &siftData2, float *homography)
{
#ifdef MANAGEDMEM
SiftPoint *sift1 = siftData1.m_data;
SiftPoint *sift2 = siftData2.m_data;
#else
SiftPoint *sift1 = siftData1.h_data;
SiftPoint *sift2 = siftData2.h_data;
#endif
int numPts1 = siftData1.numPts;
int numPts2 = siftData2.numPts;
int numFound = 0;
for (int i=0;i<numPts1;i++) {
float *data1 = sift1[i].data;
cout << i << ":" << sift1[i].scale << ":" << (int)sift1[i].orientation << endl;
bool found = false;
for (int j=0;j<numPts2;j++) {
float *data2 = sift2[j].data;
float sum = 0.0f;
for (int k=0;k<128;k++)
sum += data1[k]*data2[k];
float den = homography[6]*sift1[i].coords2D[0] + homography[7]*sift1[i].coords2D[1] + homography[8];
float dx = (homography[0]*sift1[i].coords2D[0] + homography[1]*sift1[i].coords2D[1] + homography[2]) / den - sift2[j].coords2D[0];
float dy = (homography[3]*sift1[i].coords2D[0] + homography[4]*sift1[i].coords2D[1] + homography[5]) / den - sift2[j].coords2D[1];
float err = dx*dx + dy*dy;
if (err<100.0f)
found = true;
if (err<100.0f || j==sift1[i].match) {
if (j==sift1[i].match && err<100.0f)
cout << " *";
else if (j==sift1[i].match)
cout << " -";
else if (err<100.0f)
cout << " +";
else
cout << " ";
cout << j << ":" << sum << ":" << (int)sqrt(err) << ":" << sift2[j].scale << ":" << (int)sift2[j].orientation << endl;
}
}
cout << endl;
if (found)
numFound++;
}
cout << "Number of founds: " << numFound << endl;
}
void PrintMatchData(SiftData &siftData1, SiftData &siftData2, cuImage &img)
{
int numPts = siftData1.numPts;
#ifdef MANAGEDMEM
SiftPoint *sift1 = siftData1.m_data;
SiftPoint *sift2 = siftData2.m_data;
#else
SiftPoint *sift1 = siftData1.h_data;
SiftPoint *sift2 = siftData2.h_data;
#endif
float *h_img = img.h_data;
int w = img.width;
int h = img.height;
cout << setprecision(3);
for (int j=0;j<numPts;j++) {
int k = sift1[j].match;
if (true || sift1[j].match_error<5) {
float dx = sift2[k].coords2D[0] - sift1[j].coords2D[0];
float dy = sift2[k].coords2D[1] - sift1[j].coords2D[1];
#if 1
if (false && sift1[j].coords2D[0]>550 && sift1[j].coords2D[0]<600) {
cout << "pos1=(" << (int)sift1[j].coords2D[0] << "," << (int)sift1[j].coords2D[1] << ") ";
cout << j << ": " << "score=" << sift1[j].score << " ambiguity=" << sift1[j].ambiguity << " match=" << k << " ";
cout << "scale=" << sift1[j].scale << " ";
cout << "error=" << (int)sift1[j].match_error << " ";
cout << "orient=" << (int)sift1[j].orientation << "," << (int)sift2[k].orientation << " ";
cout << " delta=(" << (int)dx << "," << (int)dy << ")" << endl;
}
#endif
#if 1
int len = (int)(fabs(dx)>fabs(dy) ? fabs(dx) : fabs(dy));
for (int l=0;l<len;l++) {
int x = (int)(sift1[j].coords2D[0] + dx*l/len);
int y = (int)(sift1[j].coords2D[1] + dy*l/len);
h_img[y*w+x] = 255.0f;
}
#endif
}
#if 1
int x = (int)(sift1[j].coords2D[0]+0.5);
int y = (int)(sift1[j].coords2D[1]+0.5);
int s = min(x, min(y, min(w-x-2, min(h-y-2, (int)(1.41*sift1[j].scale)))));
int p = y*w + x;
p += (w+1);
for (int k=0;k<s;k++)
h_img[p-k] = h_img[p+k] = h_img[p-k*w] = h_img[p+k*w] = 0.0f;
p -= (w+1);
for (int k=0;k<s;k++)
h_img[p-k] = h_img[p+k] = h_img[p-k*w] =h_img[p+k*w] = 255.0f;
#endif
}
cout << setprecision(6);
}