-
Notifications
You must be signed in to change notification settings - Fork 10
/
sift-driver.cpp
308 lines (252 loc) · 8.86 KB
/
sift-driver.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
// file: sift-driver.cpp
// author: Andrea Vedaldi
// description: SIFT command line utility implementation
// AUTORIGTHS
#include "sift.hpp"
#include<string>
#include<iostream>
#include<fstream>
#include<sstream>
#include<algorithm>
#include<limits>
extern "C" {
#include<getopt.h>
}
using namespace std ;
int
main(int argc, char** argv)
{
int first = -1 ;
int octaves = -1 ;
int levels = 3 ;
int nodescr = 0 ;
int noorient = 0 ;
int savegss = 0 ;
int verbose = 0 ;
std::string prefix("") ;
static struct option longopts[] = {
{ "verbose", no_argument, NULL, 'v' },
{ "help", no_argument, NULL, 'h' },
{ "output", required_argument, NULL, 'o' },
{ "first-octave", required_argument, NULL, 'f' },
{ "octaves", required_argument, NULL, 'O' },
{ "levels", required_argument, NULL, 'S' },
{ "no-descriptors", no_argument, &nodescr, 1 },
{ "no-orientations", no_argument, &noorient, 1 },
{ "save-gss", no_argument, &savegss, 1 },
{ NULL, 0, NULL, 0 }
};
int ch ;
try {
while ( (ch = getopt_long(argc, argv, "hvo:O:S:", longopts, NULL)) != -1) {
switch (ch) {
case '?' :
VL_THROW("Invalid option '" << argv[optind-1] << "'.") ;
break;
case ':' :
VL_THROW("Missing option argument for '" << argv[optind-1] << "'.") ;
break;
case 'h' :
std::cout
<< argv[0] << " [--verbose] [--help] [--output OUTPREFIX]" << endl
<< " [--save-gss] [--no-descriptors] [--no-orientations] " << endl
<< " [--levels NLEVELS] [--octaves NOCTAVES] [--first-octave FIRSTOCTAVE] " << endl
<< " IMAGE [IMAGE2 ...]" << endl ;
return 0 ;
case 'v' :
verbose = 1 ;
break ;
case 'f': // first octave
{
std::istringstream iss(optarg) ;
iss >> first ;
if( iss.fail() )
VL_THROW("Invalid argument '" << optarg << "'.") ;
}
break ;
case 'O' : // octaves
{
std::istringstream iss(optarg) ;
iss >> octaves ;
if( iss.fail() )
VL_THROW("Invalid argument '" << optarg << "'.") ;
if( octaves < 1 ) {
VL_THROW("Number of octaves cannot be smaller than one.");
}
}
break ;
case 'S' : // levels
{
std::istringstream iss(optarg) ;
iss >> levels ;
if( iss.fail() )
VL_THROW("Invalid argument '" << optarg << "'.") ;
if( levels < 1 ) {
VL_THROW("Number of levels cannot be smaller than one.") ;
}
}
break ;
case 'o' : // output
{
prefix = std::string(optarg) ;
break ;
}
case 0 : // all other options
break ;
default:
assert(false) ;
}
}
argc -= optind;
argv += optind;
if( argc == 0 ) VL_THROW("No input image specfied.") ;
if( prefix.size() != 0 && argc > 1 ) {
VL_THROW("--output cannot be used with multiple images.") ;
}
}
catch( VL::Exception const & e ) {
std::cerr<<"error: "<<e.msg<<std::endl ;
exit(1) ;
}
while( argc > 0 ) {
VL::PgmBuffer buffer ;
std::string name(argv[0]) ;
// get prefix
if( prefix.size() == 0 ) {
int i ;
std::size_t const not_found = std::numeric_limits<std::size_t>::max() - 1 ;
if( ( (i = name.rfind(".pgm",not_found)) != not_found ) ||
( (i = name.rfind(".PGM",not_found)) != not_found ) ) {
prefix = std::string(name, 0, i) ;
} else {
prefix = name ;
}
}
// ---------------------------------------------------------------
// Load PGM image
// ---------------------------------------------------------------
try {
ifstream in( name.c_str() ) ;
if(! in.good()) VL_THROW("Could not open '"<< name <<"'.") ;
extractPgm( in, buffer ) ;
}
catch( VL::Exception const& e ) {
cerr<<e.msg<<endl ;
return 1 ;
}
if( verbose )
cout << "Loaded PGM image ("
<< buffer.width<<" x "<<buffer.height<<")."
<< endl ;
// ---------------------------------------------------------------
// Gaussian scale space
// ---------------------------------------------------------------
if( verbose )
cout << "Computing Gaussian scale space ... " << std::flush ;
int O = octaves ;
int const S = levels ;
int const omin = first ;
float const sigman = .5 ;
float const sigma0 = 1.6 * powf(2.0f, 1.0f / S) ;
// autoselect of number of octaves?
if( O < 1 ) {
O =
std::max
( int(floor(log2(std::min(buffer.width,buffer.height))) - omin -4),
1 ) ;
}
VL::Sift sift( buffer.data, buffer.width, buffer.height,
sigman, sigma0,
O, S,
omin, -1, S+1 ) ;
if(verbose)
cout << "(" << O << " octaves starting from " << omin <<", "
<< S << " levels) done." << endl ;
// ---------------------------------------------------------------
// Save Gaussian scale space
// ---------------------------------------------------------------
if( savegss ) {
if(verbose)
cout << "Saving Gaussian scale space." << endl ;
for(int o = omin ; o < omin + O ; ++o) {
for(int s = 0 ; s < S ; ++s ) {
std::ostringstream suffix ;
suffix<<'.'<<o<<'.'<<s<<".pgm" ;
std::string outname = prefix + suffix.str() ;
if(verbose)
cout << "Saving octave " <<o
<< " level "<<s
<< " to '"<< outname << "' ... " << std::flush ;
ofstream out( outname.c_str() ) ;
VL::insertPgm( out,
sift.getLevel(o,s),
sift.getOctaveWidth(o),
sift.getOctaveHeight(o) ) ;
if(verbose) cout << "done." <<endl ;
}
}
}
// ---------------------------------------------------------------
// SIFT detector
// ---------------------------------------------------------------
if(verbose)
cout << "Running SIFT detector ... " << std::flush ;
sift.detectKeypoints() ;
if(verbose)
cout << "done ("
<< sift.keypointsEnd() - sift.keypointsBegin()
<< " keypoints)." << endl ;
// ---------------------------------------------------------------
// SIFT orientations and descriptors
// ---------------------------------------------------------------
if( verbose ) {
if( ! noorient && nodescr) cout << "Computing keypoint orientations" ;
if( noorient && ! nodescr) cout << "Computing keypoint descriptors" ;
if( ! noorient && ! nodescr) cout << "Computing orientations and descriptors" ;
if( noorient && nodescr) cout << "Finalizing" ;
}
{
std::string outname = prefix + ".key" ;
ofstream out( outname.c_str() ) ;
if(verbose)
std::cout << " (saving keypoints to '" << outname << "') ... "
<< std::flush ;
for( VL::Sift::KeypointsConstIter iter = sift.keypointsBegin() ;
iter != sift.keypointsEnd() ;
++iter ) {
// detect orientations
float angles [4] ;
int nangles ;
if( ! noorient ) {
nangles = sift.computeKeypointOrientations(angles, *iter) ;
} else {
nangles = 1;
angles[0] = 0.0f ;
}
// compute descriptors
for(int a = 0 ; a < nangles ; ++a) {
out << iter->x << " "
<< iter->y << " "
<< iter->sigma ;
if( ! noorient ) {
out << " " << angles[a] ;
}
if( ! nodescr ) {
float descr [128] ;
sift.computeKeypointDescriptor(descr, *iter, angles[a]) ;
for(int i = 0 ; i < 128 ; ++i) {
out << " " << (int)( 512.0f * descr[i] ) ;
}
}
// next keypoint
out << endl ;
}
}
if(verbose)
std::cout << "done." << std::endl ;
}
argc-- ;
argv++ ;
} // next image
return 0 ;
}