/
surf_match.patch
198 lines (179 loc) · 5.84 KB
/
surf_match.patch
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
--- orginal_code/SURF-V1.0.9/match.cpp 2006-12-20 03:28:30.000000000 -0600
+++ match.cpp 2009-03-11 13:26:07.000000000 -0500
@@ -31,7 +31,7 @@
*/
#include <vector>
-#include <string>
+#include <string.h>
#include <iostream>
#include <fstream>
#include <cmath>
@@ -39,12 +39,10 @@
#include "ipoint.h"
// Define to compile with PGM output
-#define GRAPHICS
+//#define GRAPHICS
-#ifdef GRAPHICS
#include "image.h"
#include "imload.h"
-#endif
using namespace std;
using namespace surf;
@@ -69,6 +67,7 @@
int match = -1;
for (unsigned i = 0; i < ipts.size(); i++) {
+
// Take advantage of Laplacian to speed up matching
if (ipts[i].laplace != ip1.laplace)
continue;
@@ -90,6 +89,30 @@
return -1;
}
+// Writes the interestpoint match for later reading
+inline void write_ip_record(std::ofstream &f, Ipoint const& p){
+ float buffer_f = (float)p.x;
+ f.write((char*)&(buffer_f), sizeof(float)); //x
+ buffer_f = (float)p.y;
+ f.write((char*)&(buffer_f), sizeof(float)); //y
+ int buffer_i = (int)p.x;
+ f.write((char*)&(buffer_i), sizeof(int)); //ix
+ buffer_i = (int)p.y;
+ f.write((char*)&(buffer_i), sizeof(int)); //iy
+ buffer_f = (float)p.ori;
+ f.write((char*)&(buffer_f), sizeof(float)); //ori
+ buffer_f = (float)p.scale;
+ f.write((char*)&(buffer_f), sizeof(float)); //scale
+ buffer_f = (float)p.strength;
+ f.write((char*)&(buffer_f),sizeof(float)); //interest
+ bool buffer_b = false;
+ f.write((char*)&(buffer_b),sizeof(bool)); //polarity
+ f.write((char*)&(buffer_i),sizeof(unsigned)); // octave
+ f.write((char*)&(buffer_i),sizeof(unsigned)); // scale_lvl
+ buffer_i = 0;
+ f.write((char*)&(buffer_i), sizeof(int)); //size of descriptor... nothing...
+}
+
// Find all possible matches between two images
vector< int > findMatches(const vector< Ipoint >& ipts1, const vector< Ipoint >& ipts2) {
vector< int > matches(ipts1.size());
@@ -98,9 +121,7 @@
int match = findMatch(ipts1[i], ipts2);
matches[i] = match;
if (match != -1) {
- cout << " Matched feature " << i << " in image 1 with feature "
- << match << " in image 2." << endl;
- c++;
+ c++;
}
}
cout << " --> Matched " << c << " features of " << ipts1.size() << " in image 1." << endl;
@@ -127,10 +148,10 @@
// Load the interest points in Mikolajczyk's format
for (unsigned n = 0; n < count; n++) {
// circular regions with diameter 5 x scale
- float x, y, a, b, c;
+ float x, y, a, b, c, ori;
// Read in region data, though not needed for actual matching
- ipfile >> x >> y >> a >> b >> c;
+ ipfile >> x >> y >> a >> b >> c >> ori;
float det = sqrt((a-c)*(a-c) + 4.0*b*b);
float e1 = 0.5*(a+c + det);
@@ -142,6 +163,7 @@
ipts[n].x = x;
ipts[n].y = y;
ipts[n].scale = sc/2.5;
+ ipts[n].ori = ori;
// Read in Laplacian
ipfile >> ipts[n].laplace;
@@ -159,7 +181,7 @@
(y1 >= im->getHeight() && y2 >= im->getHeight()))
return;
- bool steep = std::abs(y2 - y1) > std::abs(x2 - x1);
+ bool steep = abs(y2 - y1) > abs(x2 - x1);
if (steep) {
int t;
t = x1;
@@ -182,7 +204,7 @@
}
int deltax = x2 - x1;
- int deltay = std::abs(y2 - y1);
+ int deltay = abs(y2 - y1);
int error = 0;
int y = y1;
@@ -215,12 +237,14 @@
int main(int argc, char **argv) {
Image *im1, *im2;
-#ifdef GRAPHICS
+
ImLoad ImageLoader;
vector< Ipoint > ipts1, ipts2;
bool drawc = false;
-#endif
+
char ofname[100];
+ string matchname;
+ matchname.clear();
im1 = im2 = NULL;
ofname[0] = 0;
@@ -232,7 +256,6 @@
loadIpoints(argv[++arg], ipts1);
if (! strcmp(argv[arg], "-k2"))
loadIpoints(argv[++arg], ipts2);
-#ifdef GRAPHICS
if (! strcmp(argv[arg], "-im1"))
im1 = ImageLoader.readImage(argv[++arg]);
if (! strcmp(argv[arg], "-im2"))
@@ -241,12 +264,13 @@
strcpy(ofname, argv[++arg]);
if (! strcmp(argv[arg], "-c"))
drawc = true;
-#endif
+ if (! strcmp(argv[arg], "-m"))
+ matchname = argv[++arg];
}
if (ipts1.size() == 0 || ipts2.size() == 0) {
cout << "Usage:" << endl;
- cout << " match -k1 out1.surf -k2 out2.surf -im1 img1.pgm -im2 img2.pgm -o out.pgm" << endl << endl;
+ cout << " match -k1 out1.surf -k2 out2.surf -im1 img1.pgm -im2 img2.pgm -o out.pgm -m output.match" << endl << endl;
cout << "For each feature in first descriptor file, find best in second according to "
<< "nearest neighbor ratio strategy. Display matches in out.pgm, generated "
<< "from img1.pgm and img2.pgm. Use -c to draw crosses at interest points." << endl;
@@ -255,7 +279,33 @@
vector< int > matches = findMatches(ipts1, ipts2);
-#ifdef GRAPHICS
+ // Determining if to save a match file
+ if (matchname.size()) {
+ int count = 0;
+ for (unsigned i = 0; i < matches.size(); ++i){
+ if (matches[i] != -1)
+ ++count;
+ }
+ if (count > 0 ) {
+ std::ofstream outputFile(matchname.c_str(), std::ios::out);
+ outputFile.write((char*)&count, sizeof(int));
+ outputFile.write((char*)&count, sizeof(int));
+ //Writing left side..
+ for (unsigned i = 0; i < matches.size(); ++i){
+ if (matches[i] != -1) {
+ write_ip_record(outputFile,ipts1[i]);
+ }
+ }
+ //Writing right side..
+ for (unsigned i = 0; i < matches.size(); ++i){
+ if (matches[i] != -1) {
+ write_ip_record(outputFile,ipts2[matches[i]]);
+ }
+ }
+ }
+ }
+
+
if (im1 != NULL && im2 != NULL && ofname[0] != 0) {
Image res(max(im1->getWidth(), im2->getWidth()), im1->getHeight() + im2->getHeight());
for (int x = 0; x < im1->getWidth(); x++)
@@ -283,7 +333,6 @@
ImageLoader.saveImage(ofname, &res);
}
-#endif
return 0;
}