-
Notifications
You must be signed in to change notification settings - Fork 124
Expand file tree
/
Copy pathmain.cpp
More file actions
executable file
·343 lines (297 loc) · 12.9 KB
/
main.cpp
File metadata and controls
executable file
·343 lines (297 loc) · 12.9 KB
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
/*
* Copyright 2016, Simula Research Laboratory
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include <popsift/common/device_prop.h>
#include <popsift/features.h>
#include <popsift/popsift.h>
#include <popsift/sift_conf.h>
#include <popsift/sift_config.h>
#include <popsift/version.hpp>
#include <boost/filesystem.hpp>
#include <boost/program_options.hpp>
#include <cmath>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <sstream>
#include <stdexcept>
#include <string>
#ifdef USE_DEVIL
#include <devil_cpp_wrapper.hpp>
#endif
#include "pgmread.h"
#if POPSIFT_IS_DEFINED(POPSIFT_USE_NVTX)
#include <nvToolsExtCuda.h>
#else
#define nvtxRangePushA(a)
#define nvtxRangePop()
#endif
using namespace std;
static bool print_dev_info = false;
static bool print_time_info = false;
static bool write_as_uchar = false;
static bool write_with_ori = false;
static bool dont_write = false;
static bool pgmread_loading = false;
static bool float_mode = false;
static void parseargs(int argc, char** argv, popsift::Config& config, string& inputFile) {
using namespace boost::program_options;
options_description options("Options");
{
options.add_options()
("help,h", "Print usage")
("verbose,v", bool_switch()->notifier([&](bool i) {if(i) config.setVerbose(); }), "")
("log,l", bool_switch()->notifier([&](bool i) {if(i) config.setLogMode(popsift::Config::All); }), "Write debugging files")
("input-file,i", value<std::string>(&inputFile)->required(), "Input file");
}
options_description parameters("Parameters");
{
parameters.add_options()
("octaves",
value<int>(&config.octaves)->default_value(config.getOctaves()),
"Number of octaves")
("levels",
value<int>(&config.levels)->default_value(config.getLevels()),
"Number of levels per octave")
("sigma",
value<float>()->notifier([&](float f) { config.setSigma(f); })->default_value(config.getSigma()),
"Initial sigma value")
("threshold",
value<float>()->notifier([&](float f) { config.setThreshold(f); })->default_value(config.getThreshold()),
popsift::Config::getPeakThreshUsage().c_str() )
("edge-threshold",
value<float>()->notifier([&](float f) { config.setEdgeLimit(f); })->default_value(config.getEdgeLimit()),
popsift::Config::getEdgeThreshUsage().c_str() )
("edge-limit",
value<float>()->notifier([&](float f) { config.setEdgeLimit(f); }),
"synonym to --edge-threshold" )
("downsampling",
value<float>()->notifier([&](float f) { config.setDownsampling(f); })->default_value(config.getDownsampling()),
"Downscale width and height of input by 2^N")
("initial-blur",
value<float>()->notifier([&](float f) {config.setInitialBlur(f); })->default_value(config.getInitialBlur()),
"Assume initial blur, subtract when blurring first time");
}
options_description modes("Modes");
{
modes.add_options()
( "gauss-mode", value<std::string>()->notifier([&](const std::string& s) { config.setGaussMode(s); }),
popsift::Config::getGaussModeUsage() )
// "Choice of span (1-sided) for Gauss filters. Default is VLFeat-like computation depending on sigma. "
// "Options are: vlfeat, relative, relative-all, opencv, fixed9, fixed15"
( "desc-mode", value<std::string>()->notifier([&](const std::string& s) { config.setDescMode(s); }),
popsift::Config::getDescModeUsage() )
("popsift-mode", bool_switch()->notifier([&](bool b) { if(b) config.setMode(popsift::Config::PopSift); }),
"During the initial upscale, shift pixels by 1. In extrema refinement, steps up to 0.6, do not reject points when reaching max iterations, "
"first contrast threshold is .8 * peak thresh. Shift feature coords octave 0 back to original pos.")
("vlfeat-mode", bool_switch()->notifier([&](bool b) { if(b) config.setMode(popsift::Config::VLFeat); }),
"During the initial upscale, shift pixels by 1. That creates a sharper upscaled image. "
"In extrema refinement, steps up to 0.6, levels remain unchanged, "
"do not reject points when reaching max iterations, "
"first contrast threshold is .8 * peak thresh.")
("opencv-mode", bool_switch()->notifier([&](bool b) { if(b) config.setMode(popsift::Config::OpenCV); }),
"During the initial upscale, shift pixels by 0.5. "
"In extrema refinement, steps up to 0.5, "
"reject points when reaching max iterations, "
"first contrast threshold is floor(.5 * peak thresh). "
"Computed filter width are lower than VLFeat/PopSift")
("direct-scaling", bool_switch()->notifier([&](bool b) { if(b) config.setScalingMode(popsift::Config::ScaleDirect); }),
"Direct each octave from upscaled orig instead of blurred level.")
("norm-multi", value<int>()->notifier([&](int i) {config.setNormalizationMultiplier(i); }), "Multiply the descriptor by pow(2,<int>).")
( "norm-mode", value<std::string>()->notifier([&](const std::string& s) { config.setNormMode(s); }),
popsift::Config::getNormModeUsage() )
( "root-sift", bool_switch()->notifier([&](bool b) { if(b) config.setNormMode(popsift::Config::RootSift); }),
"synonym to --norm-mode=RootSift" )
("filter-max-extrema", value<int>()->notifier([&](int f) {config.setFilterMaxExtrema(f); }), "Approximate max number of extrema.")
("filter-grid", value<int>()->notifier([&](int f) {config.setFilterGridSize(f); }), "Grid edge length for extrema filtering (ie. value 4 leads to a 4x4 grid)")
("filter-sort", value<std::string>()->notifier([&](const std::string& s) {config.setFilterSorting(s); }), "Sort extrema in each cell by scale, either random (default), up or down");
}
options_description informational("Informational");
{
informational.add_options()
("print-gauss-tables", bool_switch()->notifier([&](bool b) { if(b) config.setPrintGaussTables(); }), "A debug output printing Gauss filter size and tables")
("print-dev-info", bool_switch(&print_dev_info)->default_value(false), "A debug output printing CUDA device information")
("print-time-info", bool_switch(&print_time_info)->default_value(false), "A debug output printing image processing time after load()")
("write-as-uchar", bool_switch(&write_as_uchar)->default_value(false), "Output descriptors rounded to int.\n"
"Scaling to sensible ranges is not automatic, should be combined with --norm-multi=9 or similar")
("write-with-ori", bool_switch(&write_with_ori)->default_value(false), "Output points are written with sigma and orientation.\n")
("dont-write", bool_switch(&dont_write)->default_value(false), "Suppress descriptor output")
("pgmread-loading", bool_switch(&pgmread_loading)->default_value(false), "Use the old image loader instead of LibDevIL")
("float-mode", bool_switch(&float_mode)->default_value(false), "Upload image to GPU as float instead of byte")
;
//("test-direct-scaling")
}
options_description all("Allowed options");
all.add(options).add(parameters).add(modes).add(informational);
variables_map vm;
try
{
store(parse_command_line(argc, argv, all), vm);
if (vm.count("help")) {
std::cout << all << '\n';
exit(EXIT_SUCCESS);
}
notify(vm); // Notify does processing (e.g., raise exceptions if required args are missing)
}
catch(boost::program_options::error& e)
{
std::cerr << "Error: " << e.what() << std::endl << std::endl;
std::cerr << "Usage:\n\n" << all << std::endl;
exit(EXIT_FAILURE);
}
}
static void collectFilenames( list<string>& inputFiles, const boost::filesystem::path& inputFile )
{
vector<boost::filesystem::path> vec;
std::copy( boost::filesystem::directory_iterator( inputFile ),
boost::filesystem::directory_iterator(),
std::back_inserter(vec) );
for (const auto& currPath : vec)
{
if( boost::filesystem::is_regular_file(currPath) )
{
inputFiles.push_back( currPath.string() );
}
else if( boost::filesystem::is_directory(currPath) )
{
collectFilenames( inputFiles, currPath);
}
}
}
SiftJob* process_image( const string& inputFile, PopSift& PopSift )
{
SiftJob* job;
unsigned char* image_data;
#ifdef USE_DEVIL
if( ! pgmread_loading )
{
if( float_mode )
{
cerr << "Cannot combine float-mode test with DevIL image reader" << endl;
exit( -1 );
}
nvtxRangePushA( "load and convert image - devil" );
ilImage img;
if( img.Load( inputFile.c_str() ) == false ) {
cerr << "Could not load image " << inputFile << endl;
return 0;
}
if( img.Convert( IL_LUMINANCE ) == false ) {
cerr << "Failed converting image " << inputFile << " to unsigned greyscale image" << endl;
exit( -1 );
}
const auto w = img.Width();
const auto h = img.Height();
cout << "Loading " << w << " x " << h << " image " << inputFile << endl;
image_data = img.GetData();
nvtxRangePop( ); // "load and convert image - devil"
job = PopSift.enqueue( w, h, image_data );
img.Clear();
}
else
#endif
{
nvtxRangePushA( "load and convert image - pgmread" );
int w{};
int h{};
image_data = readPGMfile( inputFile, w, h );
if( image_data == nullptr ) {
exit( EXIT_FAILURE );
}
nvtxRangePop( ); // "load and convert image - pgmread"
if( ! float_mode )
{
// PopSift.init( w, h );
job = PopSift.enqueue( w, h, image_data );
delete [] image_data;
}
else
{
auto f_image_data = new float [w * h];
for( int i=0; i<w*h; i++ )
{
f_image_data[i] = float( image_data[i] ) / 256.0f;
}
job = PopSift.enqueue( w, h, f_image_data );
delete [] image_data;
delete [] f_image_data;
}
}
return job;
}
void read_job( SiftJob* job, bool really_write )
{
popsift::Features* feature_list = job->get();
cerr << "Number of feature points: " << feature_list->getFeatureCount()
<< " number of feature descriptors: " << feature_list->getDescriptorCount()
<< endl;
if( really_write ) {
nvtxRangePushA( "Writing features to disk" );
std::ofstream of( "output-features.txt" );
feature_list->print( of, write_as_uchar, write_with_ori );
}
delete feature_list;
if( really_write ) {
nvtxRangePop( ); // Writing features to disk
}
}
int main(int argc, char **argv)
{
cudaDeviceReset();
popsift::Config config;
list<string> inputFiles;
string inputFile{};
std::cout << "PopSift version: " << POPSIFT_VERSION_STRING << std::endl;
try {
parseargs( argc, argv, config, inputFile ); // Parse command line
std::cout << inputFile << std::endl;
}
catch (std::exception& e) {
std::cout << e.what() << std::endl;
return EXIT_FAILURE;
}
if( boost::filesystem::exists( inputFile ) ) {
if( boost::filesystem::is_directory( inputFile ) ) {
cout << "BOOST " << inputFile << " is directory" << endl;
collectFilenames( inputFiles, inputFile );
if( inputFiles.empty() ) {
cerr << "No files in directory, nothing to do" << endl;
return EXIT_SUCCESS;
}
} else if( boost::filesystem::is_regular_file( inputFile ) ) {
inputFiles.push_back( inputFile );
} else {
cout << "Input file is neither regular file nor directory, nothing to do" << endl;
return EXIT_FAILURE;
}
}
popsift::cuda::device_prop_t deviceInfo;
deviceInfo.set( 0, print_dev_info );
if( print_dev_info ) deviceInfo.print( );
PopSift PopSift( config,
popsift::Config::ExtractingMode,
float_mode ? PopSift::FloatImages : PopSift::ByteImages );
std::queue<SiftJob*> jobs;
for(const auto& currFile : inputFiles)
{
SiftJob* job = process_image( currFile, PopSift );
jobs.push( job );
}
while( !jobs.empty() )
{
SiftJob* job = jobs.front();
jobs.pop();
if( job ) {
read_job( job, ! dont_write );
delete job;
}
}
PopSift.uninit( );
return EXIT_SUCCESS;
}