1 | /* |
---|
2 | * Speeded-Up Robust Features (SURF) |
---|
3 | * http://people.ee.ethz.ch/~surf |
---|
4 | * |
---|
5 | * Sample application for feature matching using nearest-neighbor |
---|
6 | * ratio method. |
---|
7 | * |
---|
8 | * BUILD USING "make match.ln". |
---|
9 | * |
---|
10 | * Author: Andreas Ess |
---|
11 | * |
---|
12 | * Copyright (2006): ETH Zurich, Switzerland |
---|
13 | * Katholieke Universiteit Leuven, Belgium |
---|
14 | * All rights reserved. |
---|
15 | * |
---|
16 | * For details, see the paper: |
---|
17 | * Herbert Bay, Tinne Tuytelaars, Luc Van Gool, |
---|
18 | * "SURF: Speeded Up Robust Features" |
---|
19 | * Proceedings of the ninth European Conference on Computer Vision, May 2006 |
---|
20 | * |
---|
21 | * Permission to use, copy, modify, and distribute this software and |
---|
22 | * its documentation for educational, research, and non-commercial |
---|
23 | * purposes, without fee and without a signed licensing agreement, is |
---|
24 | * hereby granted, provided that the above copyright notice and this |
---|
25 | * paragraph appear in all copies modifications, and distributions. |
---|
26 | * |
---|
27 | * Any commercial use or any redistribution of this software |
---|
28 | * requires a license from one of the above mentioned establishments. |
---|
29 | * |
---|
30 | * For further details, contact Andreas Ess (aess@vision.ee.ethz.ch). |
---|
31 | */ |
---|
32 | |
---|
33 | #include <vector> |
---|
34 | #include <string> |
---|
35 | #include <iostream> |
---|
36 | #include <fstream> |
---|
37 | #include <cmath> |
---|
38 | |
---|
39 | #include "ipoint.h" |
---|
40 | |
---|
41 | // Define to compile with PGM output |
---|
42 | #define GRAPHICS |
---|
43 | |
---|
44 | #ifdef GRAPHICS |
---|
45 | #include "image.h" |
---|
46 | #include "imload.h" |
---|
47 | #endif |
---|
48 | |
---|
49 | using namespace std; |
---|
50 | using namespace surf; |
---|
51 | |
---|
52 | // Length of descriptor vector, ugly, global variable |
---|
53 | unsigned int vlen; |
---|
54 | |
---|
55 | // Calculate square distance of two vectors |
---|
56 | double distSquare(double *v1, double *v2, int n) { |
---|
57 | double dsq = 0.; |
---|
58 | while (n--) { |
---|
59 | dsq += (*v1 - *v2) * (*v1 - *v2); |
---|
60 | v1++; |
---|
61 | v2++; |
---|
62 | } |
---|
63 | return dsq; |
---|
64 | } |
---|
65 | |
---|
66 | // Find closest interest point in a list, given one interest point |
---|
67 | int findMatch(const Ipoint& ip1, const vector< Ipoint >& ipts) { |
---|
68 | double mind = 1e100, second = 1e100; |
---|
69 | int match = -1; |
---|
70 | |
---|
71 | for (unsigned i = 0; i < ipts.size(); i++) { |
---|
72 | // Take advantage of Laplacian to speed up matching |
---|
73 | if (ipts[i].laplace != ip1.laplace) |
---|
74 | continue; |
---|
75 | |
---|
76 | double d = distSquare(ipts[i].ivec, ip1.ivec, vlen); |
---|
77 | |
---|
78 | if (d < mind) { |
---|
79 | second = mind; |
---|
80 | mind = d; |
---|
81 | match = i; |
---|
82 | } else if (d < second) { |
---|
83 | second = d; |
---|
84 | } |
---|
85 | } |
---|
86 | |
---|
87 | if (mind < 0.7 * second) //0.8 |
---|
88 | return match; |
---|
89 | |
---|
90 | return -1; |
---|
91 | } |
---|
92 | |
---|
93 | vector <int> jeffsMatches(const vector< Ipoint >& ipts1, const vector< Ipoint >& ipts2) { |
---|
94 | vector< int > bestIndex1(ipts1.size()); |
---|
95 | vector< int > bestIndex2(ipts2.size()); |
---|
96 | vector< double > bestScore1(ipts1.size()); |
---|
97 | vector< double > bestScore2(ipts2.size()); |
---|
98 | vector< double > secondScore1(ipts1.size()); |
---|
99 | vector< double > secondScore2(ipts2.size()); |
---|
100 | |
---|
101 | for (unsigned int i = 0; i < ipts1.size(); i++) { |
---|
102 | bestIndex1[i] = -1; |
---|
103 | bestScore1[i]= 1e100; |
---|
104 | secondScore1[i] = 1e100; |
---|
105 | } |
---|
106 | for (unsigned int j = 0; j < ipts2.size(); j++) { |
---|
107 | bestIndex2[j] = -1; |
---|
108 | bestScore2[j]= 1e100; |
---|
109 | secondScore2[j] = 1e100; |
---|
110 | } |
---|
111 | |
---|
112 | int total = 0; |
---|
113 | for (unsigned int i = 0; i < ipts1.size(); i++) { |
---|
114 | for (unsigned int j = 0; j < ipts2.size(); j++) { |
---|
115 | //record best and second best scores and best index for each |
---|
116 | //point in each image |
---|
117 | double dist = distSquare(ipts1[i].ivec, ipts2[j].ivec, vlen); |
---|
118 | if (dist < 0.3) { |
---|
119 | //cout << " Matched feature " << i << " in image 1 with feature " |
---|
120 | // << j << " in image 2." << endl; |
---|
121 | //total++; |
---|
122 | } |
---|
123 | if (dist < bestScore1[i]) { |
---|
124 | bestIndex1[i] = j; |
---|
125 | secondScore1[i] = bestScore1[i]; |
---|
126 | bestScore1[i] = dist; |
---|
127 | } else if (dist < secondScore1[i]) { |
---|
128 | secondScore1[i] = dist; |
---|
129 | } |
---|
130 | |
---|
131 | if (dist < bestScore2[j]) { |
---|
132 | bestIndex2[j] = i; |
---|
133 | secondScore2[j] = bestScore2[j]; |
---|
134 | bestScore2[j] = dist; |
---|
135 | } else if (dist < secondScore2[j]) { |
---|
136 | secondScore2[j] = dist; |
---|
137 | } |
---|
138 | } |
---|
139 | } |
---|
140 | //cout << "Matched " << total << " points." << endl; |
---|
141 | //return bestIndex1; //for threshold |
---|
142 | |
---|
143 | |
---|
144 | //update second scores with best match from same image (for image 1) |
---|
145 | for (unsigned int i = 0; i < ipts1.size(); i++) { |
---|
146 | for (unsigned int j = i+1; j < ipts1.size(); j++) { |
---|
147 | double dist = distSquare(ipts1[i].ivec, ipts1[j].ivec, vlen); |
---|
148 | if (dist < secondScore1[i]){// && dist < 0.3) { |
---|
149 | //cout << "im1 point " << i << " best: " << bestScore1[i] << " second: " << secondScore1[i] << |
---|
150 | // "im1 point " << j << ": " << dist << endl; |
---|
151 | secondScore1[i] = dist; |
---|
152 | } |
---|
153 | } |
---|
154 | } |
---|
155 | |
---|
156 | //update second scores with best match from same image (for image 2) |
---|
157 | for (unsigned int i = 0; i < ipts2.size(); i++) { |
---|
158 | for (unsigned int j = i+1; j < ipts2.size(); j++) { |
---|
159 | double dist = distSquare(ipts2[i].ivec, ipts2[j].ivec, vlen); |
---|
160 | if (dist < secondScore2[j]){// && dist < 0.3){ |
---|
161 | //cout << "im2 point " << i << " best: " << bestScore2[i] << " second: " << secondScore2[i] << |
---|
162 | // "im2 point " << j << ": " << dist << endl; |
---|
163 | secondScore2[i] = dist; |
---|
164 | } |
---|
165 | } |
---|
166 | } |
---|
167 | |
---|
168 | |
---|
169 | //if the best score is no longer the best or isn't good enough relative |
---|
170 | // to the second, kill the index |
---|
171 | |
---|
172 | //image 1 |
---|
173 | for (unsigned int i = 0; i < ipts1.size(); i++) { |
---|
174 | if (secondScore1[i] * 0.85 < bestScore1[i]) { |
---|
175 | bestIndex1[i] = -1; |
---|
176 | } |
---|
177 | } |
---|
178 | |
---|
179 | //image 2 |
---|
180 | for (unsigned int i = 0; i < ipts2.size(); i++) { |
---|
181 | if (secondScore2[i] * 0.85 < bestScore2[i]) { |
---|
182 | bestIndex2[i] = -1; |
---|
183 | } |
---|
184 | } |
---|
185 | |
---|
186 | //if the best matches aren't mutual, kill the match |
---|
187 | |
---|
188 | for (unsigned int i = 0; i < ipts1.size(); i++) { |
---|
189 | int index = bestIndex1[i]; |
---|
190 | if (index != -1) { |
---|
191 | if (bestIndex2[index] != i) { |
---|
192 | //cout << "Non symetric match. im1(" << i << ")->" << index << " but im2(" << index << |
---|
193 | // ")->" << bestIndex2[index] << endl; |
---|
194 | bestIndex1[i] = -1; |
---|
195 | } else { |
---|
196 | cout << " Matched feature " << i << " in image 1 with feature " |
---|
197 | << index << " in image 2." << endl; |
---|
198 | total++; |
---|
199 | } |
---|
200 | } |
---|
201 | } |
---|
202 | |
---|
203 | cout << "Matched " << total << " points." << endl; |
---|
204 | return bestIndex1; |
---|
205 | } |
---|
206 | |
---|
207 | // Find all possible matches between two images |
---|
208 | vector< int > findMatches(const vector< Ipoint >& ipts1, const vector< Ipoint >& ipts2) { |
---|
209 | vector< int > matches(ipts1.size()); |
---|
210 | int c = 0; |
---|
211 | for (unsigned i = 0; i < ipts1.size(); i++) { |
---|
212 | int match = findMatch(ipts1[i], ipts2); |
---|
213 | matches[i] = match; |
---|
214 | if (match != -1) { |
---|
215 | cout << " Matched feature " << i << " in image 1 with feature " |
---|
216 | << match << " in image 2." << endl; |
---|
217 | c++; |
---|
218 | } |
---|
219 | } |
---|
220 | cout << " --> Matched " << c << " features of " << ipts1.size() << " in image 1." << endl; |
---|
221 | return matches; |
---|
222 | } |
---|
223 | |
---|
224 | // Load the interest points from a regular ASCII file |
---|
225 | void loadIpoints(string sFileName, vector< Ipoint >& ipts) { |
---|
226 | ifstream ipfile(sFileName.c_str()); |
---|
227 | if( !ipfile ) { |
---|
228 | cerr << "ERROR in loadIpoints(): " |
---|
229 | << "Couldn't open file '" << sFileName.c_str() << "'!" << endl; |
---|
230 | return; |
---|
231 | } |
---|
232 | |
---|
233 | // Load the file header |
---|
234 | unsigned count; |
---|
235 | ipfile >> vlen >> count; |
---|
236 | |
---|
237 | // create a new interest point vector |
---|
238 | ipts.clear(); |
---|
239 | ipts.resize(count); |
---|
240 | |
---|
241 | // Load the interest points in Mikolajczyk's format |
---|
242 | for (unsigned n = 0; n < count; n++) { |
---|
243 | // circular regions with diameter 5 x scale |
---|
244 | float x, y, a, b, c; |
---|
245 | |
---|
246 | // Read in region data, though not needed for actual matching |
---|
247 | ipfile >> x >> y >> a >> b >> c; |
---|
248 | |
---|
249 | float det = sqrt((a-c)*(a-c) + 4.0*b*b); |
---|
250 | float e1 = 0.5*(a+c + det); |
---|
251 | float e2 = 0.5*(a+c - det); |
---|
252 | float l1 = (1.0/sqrt(e1)); |
---|
253 | float l2 = (1.0/sqrt(e2)); |
---|
254 | float sc = sqrt( l1*l2 ); |
---|
255 | |
---|
256 | ipts[n].x = x; |
---|
257 | ipts[n].y = y; |
---|
258 | ipts[n].scale = sc/2.5; |
---|
259 | |
---|
260 | // Read in Laplacian |
---|
261 | ipfile >> ipts[n].laplace; |
---|
262 | |
---|
263 | // SURF makes Laplacian part of descriptor, so skip it.. |
---|
264 | ipts[n].ivec = new double[vlen - 1]; |
---|
265 | for (unsigned j = 0; j < vlen - 1; j++) |
---|
266 | ipfile >> ipts[n].ivec[j]; |
---|
267 | } |
---|
268 | } |
---|
269 | |
---|
270 | void drawLine(Image *im, int x1, int y1, int x2, int y2) { |
---|
271 | if ((x1 < 0 && x2 < 0) || (y1 < 0 && y2 < 0) || |
---|
272 | (x1 >= im->getWidth() && x2 >= im->getWidth()) || |
---|
273 | (y1 >= im->getHeight() && y2 >= im->getHeight())) |
---|
274 | return; |
---|
275 | |
---|
276 | bool steep = std::abs(y2 - y1) > std::abs(x2 - x1); |
---|
277 | if (steep) { |
---|
278 | int t; |
---|
279 | t = x1; |
---|
280 | x1 = y1; |
---|
281 | y1 = t; |
---|
282 | t = y2; |
---|
283 | y2 = x2; |
---|
284 | x2 = t; |
---|
285 | } |
---|
286 | |
---|
287 | if (x1 > x2) { |
---|
288 | // Swap points |
---|
289 | int t; |
---|
290 | t = x1; |
---|
291 | x1 = x2; |
---|
292 | x2 = t; |
---|
293 | t = y1; |
---|
294 | y1 = y2; |
---|
295 | y2 = t; |
---|
296 | } |
---|
297 | |
---|
298 | int deltax = x2 - x1; |
---|
299 | int deltay = std::abs(y2 - y1); |
---|
300 | |
---|
301 | int error = 0; |
---|
302 | int y = y1; |
---|
303 | int ystep = y1 < y2 ? 1 : -1; |
---|
304 | |
---|
305 | for (int x = x1; x < x2; x++) { |
---|
306 | if (steep) { |
---|
307 | if (x >= 0 && y >= 0 && y < im->getWidth() && x < im->getHeight()) |
---|
308 | im->setPix(y, x, 1); |
---|
309 | } else { |
---|
310 | if (x >= 0 && y >= 0 && x < im->getWidth() && y < im->getHeight()) |
---|
311 | im->setPix(x, y, 1); |
---|
312 | |
---|
313 | } |
---|
314 | error += deltay; |
---|
315 | |
---|
316 | if (2 * error > deltax) { |
---|
317 | y += ystep; |
---|
318 | error -= deltax; |
---|
319 | } |
---|
320 | } |
---|
321 | } |
---|
322 | |
---|
323 | void drawCross(Image *im, int x, int y, int s = 5) { |
---|
324 | for (int x1 = x - s; x1 <= x + s; x1++) |
---|
325 | im->setPix(x1, y, 1); |
---|
326 | for (int y1 = y - s; y1 <= y + s; y1++) |
---|
327 | im->setPix(x, y1, 1); |
---|
328 | } |
---|
329 | |
---|
330 | int main(int argc, char **argv) { |
---|
331 | Image *im1, *im2; |
---|
332 | #ifdef GRAPHICS |
---|
333 | ImLoad ImageLoader; |
---|
334 | vector< Ipoint > ipts1, ipts2; |
---|
335 | bool drawc = false; |
---|
336 | #endif |
---|
337 | char ofname[100]; |
---|
338 | |
---|
339 | im1 = im2 = NULL; |
---|
340 | ofname[0] = 0; |
---|
341 | |
---|
342 | // Read the arguments |
---|
343 | int arg = 0; |
---|
344 | while (++arg < argc) { |
---|
345 | if (! strcmp(argv[arg], "-k1")) |
---|
346 | loadIpoints(argv[++arg], ipts1); |
---|
347 | if (! strcmp(argv[arg], "-k2")) |
---|
348 | loadIpoints(argv[++arg], ipts2); |
---|
349 | #ifdef GRAPHICS |
---|
350 | if (! strcmp(argv[arg], "-im1")) |
---|
351 | im1 = ImageLoader.readImage(argv[++arg]); |
---|
352 | if (! strcmp(argv[arg], "-im2")) |
---|
353 | im2 = ImageLoader.readImage(argv[++arg]); |
---|
354 | if (! strcmp(argv[arg], "-o")) |
---|
355 | strcpy(ofname, argv[++arg]); |
---|
356 | if (! strcmp(argv[arg], "-c")) |
---|
357 | drawc = true; |
---|
358 | #endif |
---|
359 | } |
---|
360 | |
---|
361 | if (ipts1.size() == 0 || ipts2.size() == 0) { |
---|
362 | cout << "Usage:" << endl; |
---|
363 | cout << " match -k1 out1.surf -k2 out2.surf -im1 img1.pgm -im2 img2.pgm -o out.pgm" << endl << endl; |
---|
364 | cout << "For each feature in first descriptor file, find best in second according to " |
---|
365 | << "nearest neighbor ratio strategy. Display matches in out.pgm, generated " |
---|
366 | << "from img1.pgm and img2.pgm. Use -c to draw crosses at interest points." << endl; |
---|
367 | return 1; |
---|
368 | } |
---|
369 | |
---|
370 | //vector< int > matches = findMatches(ipts1, ipts2); |
---|
371 | vector< int > matches = jeffsMatches(ipts1, ipts2); |
---|
372 | |
---|
373 | #ifdef GRAPHICS |
---|
374 | if (im1 != NULL && im2 != NULL && ofname[0] != 0) { |
---|
375 | Image res(max(im1->getWidth(), im2->getWidth()), im1->getHeight() + im2->getHeight()); |
---|
376 | for (int x = 0; x < im1->getWidth(); x++) |
---|
377 | for (int y = 0; y < im1->getHeight(); y++) |
---|
378 | res.setPix(x, y, im1->getPix(x, y)); |
---|
379 | for (int x = 0; x < im2->getWidth(); x++) |
---|
380 | for (int y = 0; y < im2->getHeight(); y++) |
---|
381 | res.setPix(x, y + im1->getHeight(), im2->getPix(x, y)); |
---|
382 | |
---|
383 | // Draw lines for matches |
---|
384 | for (unsigned i = 0; i < matches.size(); i++) { |
---|
385 | if (matches[i] != -1) { |
---|
386 | drawLine(&res, (int)ipts1[i].x, (int)ipts1[i].y, |
---|
387 | (int)ipts2[matches[i]].x, (int)(ipts2[matches[i]].y + im1->getHeight())); |
---|
388 | } |
---|
389 | } |
---|
390 | |
---|
391 | // Draw crosses at interest point locations |
---|
392 | if (drawc) { |
---|
393 | for (unsigned i = 0; i < ipts1.size(); i++) |
---|
394 | drawCross(&res, (int)ipts1[i].x, (int)ipts1[i].y); |
---|
395 | for (unsigned i = 0; i < ipts2.size(); i++) |
---|
396 | drawCross(&res, (int)ipts2[i].x, (int)ipts2[i].y + im1->getHeight()); |
---|
397 | } |
---|
398 | |
---|
399 | ImageLoader.saveImage(ofname, &res); |
---|
400 | } |
---|
401 | #endif |
---|
402 | |
---|
403 | return 0; |
---|
404 | } |
---|