-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSiftGPU.cpp
More file actions
1437 lines (1255 loc) · 37.8 KB
/
SiftGPU.cpp
File metadata and controls
1437 lines (1255 loc) · 37.8 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
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
////////////////////////////////////////////////////////////////////////////
// File: SiftGPU.cpp
// Author: Changchang Wu
// Description : Implementation of the SIFTGPU classes.
// SiftGPU: The SiftGPU Tool.
// SiftGPUEX: SiftGPU + viewer
// SiftParam: Sift Parameters
//
// Copyright (c) 2007 University of North Carolina at Chapel Hill
// All Rights Reserved
//
// Permission to use, copy, modify and distribute this software and its
// documentation for educational, research and non-profit purposes, without
// fee, and without a written agreement is hereby granted, provided that the
// above copyright notice and the following paragraph appear in all copies.
//
// The University of North Carolina at Chapel Hill make no representations
// about the suitability of this software for any purpose. It is provided
// 'as is' without express or implied warranty.
//
// Please send BUG REPORTS to ccwu@cs.unc.edu
//
////////////////////////////////////////////////////////////////////////////
#include "GL/glew.h"
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <math.h>
#include <time.h>
using namespace std;
#include "GlobalUtil.h"
#include "SiftGPU.h"
#include "GLTexImage.h"
#include "ShaderMan.h"
#include "FrameBufferObject.h"
#include "SiftPyramid.h"
#include "PyramidGL.h"
//CUDA works only with vc8 or higher
#if defined(SIFTGPU_CUDA_ENABLED)
#include "PyramidCU.h"
#endif
#if defined(CL_SIFTGPU_ENABLED)
#include "PyramidCL.h"
#endif
////
#if defined(_WIN32)
#include "direct.h"
#pragma warning (disable : 4786)
#pragma warning (disable : 4996)
#else
//compatible with linux
#define _stricmp strcasecmp
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#endif
#if !defined(_MAX_PATH)
#if defined (PATH_MAX)
#define _MAX_PATH PATH_MAX
#else
#define _MAX_PATH 512
#endif
#endif
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//
//just want to make this class invisible
class ImageList:public std::vector<std::string> {};
SiftGPU::SiftGPU(int np)
{
_texImage = new GLTexInput;
_imgpath = new char[_MAX_PATH];
_outpath = new char[_MAX_PATH];
_imgpath[0] = _outpath[0] = 0;
_initialized = 0;
_image_loaded = 0;
_current = 0;
_list = new ImageList();
_pyramid = NULL;
}
SiftGPUEX::SiftGPUEX()
{
_view = _sub_view = 0;
_view_debug = 0;
GlobalUtil::_UseSiftGPUEX = 1;
srand((unsigned int)time(NULL));
RandomizeColor();
}
void SiftGPUEX::RandomizeColor()
{
float hsv[3] = {0, 0.8f, 1.0f};
for(int i = 0; i < COLOR_NUM*3; i+=3)
{
hsv[0] = (rand()%100)*0.01f; //i/float(COLOR_NUM);
HSVtoRGB(hsv, _colors+i);
}
}
SiftGPU::~SiftGPU()
{
if(_pyramid) delete _pyramid;
delete _texImage;
delete _list;
delete[] _imgpath;
delete[] _outpath;
}
inline void SiftGPU::InitSiftGPU()
{
if(_initialized || GlobalUtil::_GoodOpenGL ==0) return;
//Parse sift parameters
ParseSiftParam();
#if !defined(SIFTGPU_CUDA_ENABLED)
if(GlobalUtil::_UseCUDA)
{
GlobalUtil::_UseCUDA = 0;
std::cerr << "---------------------------------------------------------------------------\n"
<< "CUDA not supported in this binary! To enable it, please use SiftGPU_CUDA_Enable\n"
<< "solution for VS2005+ or set siftgpu_enable_cuda to 1 in makefile\n"
<< "----------------------------------------------------------------------------\n";
}
#else
if(GlobalUtil::_UseCUDA == 0 && GlobalUtil::_UseOpenCL == 0)
{
// GlobalUtil::InitGLParam(0);
}
if(GlobalUtil::_GoodOpenGL == 0)
{
GlobalUtil::_UseCUDA = 1;
std::cerr << "Switch from OpenGL to CUDA\n";
}
if(GlobalUtil::_UseCUDA && !PyramidCU::CheckCudaDevice(GlobalUtil::_DeviceIndex))
{
std::cerr << "Switch from CUDA to OpenGL\n";
GlobalUtil::_UseCUDA = 0;
}
#endif
if(GlobalUtil::_verbose) std::cout <<"\n[SiftGPU Language]:\t"
<< (GlobalUtil::_UseCUDA? "CUDA" :
(GlobalUtil::_UseOpenCL? "OpenCL" : "GLSL")) <<"\n";
#if defined(SIFTGPU_CUDA_ENABLED)
if(GlobalUtil::_UseCUDA)
_pyramid = new PyramidCU(*this);
else
#endif
#if defined(CL_SIFTGPU_ENABLED)
if(GlobalUtil::_UseOpenCL)
_pyramid = new PyramidCL(*this);
else
#endif
if(GlobalUtil::_usePackedTex)
_pyramid = new PyramidPacked(*this);
else
_pyramid = new PyramidNaive(*this);
if(GlobalUtil::_GoodOpenGL && GlobalUtil::_InitPyramidWidth > 0 && GlobalUtil::_InitPyramidHeight > 0)
{
GlobalUtil::StartTimer("Initialize Pyramids");
_pyramid->InitPyramid(GlobalUtil::_InitPyramidWidth, GlobalUtil::_InitPyramidHeight, 0);
GlobalUtil::StopTimer();
}
ClockTimer::InitHighResolution();
_initialized = 1;
}
int SiftGPU::RunSIFT(int index)
{
if(_list->size()>0 )
{
index = index % _list->size();
if(strcmp(_imgpath, _list->at(index).data()))
{
strcpy(_imgpath, _list->at(index).data());
_image_loaded = 0;
_current = index;
}
return RunSIFT();
}else
{
return 0;
}
}
int SiftGPU::RunSIFT( int width, int height, const void * data, unsigned int gl_format, unsigned int gl_type)
{
if(GlobalUtil::_GoodOpenGL ==0 ) return 0;
if(!_initialized) InitSiftGPU();
else GlobalUtil::SetGLParam();
if(GlobalUtil::_GoodOpenGL ==0 ) return 0;
if(width > 0 && height >0 && data != NULL)
{
_imgpath[0] = 0;
//try downsample the image on CPU
GlobalUtil::StartTimer("Upload Image data");
if(_texImage->SetImageData(width, height, data, gl_format, gl_type))
{
_image_loaded = 2; //gldata;
GlobalUtil::StopTimer();
_timing[0] = GlobalUtil::GetElapsedTime();
//if the size of image is different, the pyramid need to be reallocated.
GlobalUtil::StartTimer("Initialize Pyramid");
_pyramid->InitPyramid(width, height, _texImage->_down_sampled);
GlobalUtil::StopTimer();
_timing[1] = GlobalUtil::GetElapsedTime();
return RunSIFT();
}else
{
return 0;
}
}else
{
return 0;
}
}
int SiftGPU::RunSIFT(const char * imgpath)
{
if(imgpath && imgpath[0])
{
//set the new image
strcpy(_imgpath, imgpath);
_image_loaded = 0;
return RunSIFT();
}else
{
return 0;
}
}
int SiftGPU::RunSIFT(int num, const SiftKeypoint * keys, int keys_have_orientation)
{
if(num <=0) return 0;
_pyramid->SetKeypointList(num, (const float*) keys, 1, keys_have_orientation);
return RunSIFT();
}
int SiftGPU::RunSIFT()
{
//check image data
if(_imgpath[0]==0 && _image_loaded == 0) return 0;
//check OpenGL support
if(GlobalUtil::_GoodOpenGL ==0 ) return 0;
ClockTimer timer;
if(!_initialized)
{
//initialize SIFT GPU for once
InitSiftGPU();
if(GlobalUtil::_GoodOpenGL ==0 ) return 0;
}else
{
//in case some OpenGL parameters are changed by users
GlobalUtil::SetGLParam();
}
timer.StartTimer("RUN SIFT");
//process input image file
if( _image_loaded ==0)
{
int width, height;
//load and try down-sample on cpu
GlobalUtil::StartTimer("Load Input Image");
if(!_texImage->LoadImageFile(_imgpath, width, height)) return 0;
_image_loaded = 1;
GlobalUtil::StopTimer();
_timing[0] = GlobalUtil::GetElapsedTime();
//make sure the pyrmid can hold the new image.
GlobalUtil::StartTimer("Initialize Pyramid");
_pyramid->InitPyramid(width, height, _texImage->_down_sampled);
GlobalUtil::StopTimer();
_timing[1] = GlobalUtil::GetElapsedTime();
}else
{
//change some global states
if(!GlobalUtil::_UseCUDA && !GlobalUtil::_UseOpenCL)
{
GlobalUtil::FitViewPort(1,1);
_texImage->FitTexViewPort();
}
if(_image_loaded == 1)
{
_timing[0] = _timing[1] = 0;
}else
{//2
_image_loaded = 1;
}
}
if(_pyramid->_allocated ==0 ) return 0;
#ifdef DEBUG_SIFTGPU
_pyramid->BeginDEBUG(_imgpath);
#endif
//process the image
_pyramid->RunSIFT(_texImage);
//read back the timing
_pyramid->GetPyramidTiming(_timing + 2);
//write output once if there is only one input
if(_outpath[0] ){ SaveSIFT(_outpath); _outpath[0] = 0;}
//terminate the process when -exit is provided.
if(GlobalUtil::_ExitAfterSIFT && GlobalUtil::_UseSiftGPUEX) exit(0);
timer.StopTimer();
if(GlobalUtil::_verbose)std::cout<<endl;
return _pyramid->GetSucessStatus();
}
void SiftGPU::SetKeypointList(int num, const SiftKeypoint * keys, int keys_have_orientation)
{
_pyramid->SetKeypointList(num, (const float*)keys, 0, keys_have_orientation);
}
void SiftGPUEX::DisplayInput()
{
if(_texImage==NULL) return;
_texImage->VerifyTexture();
_texImage->BindTex();
_texImage->DrawImage();
_texImage->UnbindTex();
}
void SiftGPU::SetVerbose(int verbose)
{
GlobalUtil::_timingO = verbose>2;
GlobalUtil::_timingL = verbose>3;
if(verbose == -1)
{
//Loop between verbose level 0, 1, 2
if(GlobalUtil::_verbose)
{
GlobalUtil::_verbose = GlobalUtil::_timingS;
GlobalUtil::_timingS = 0;
if(GlobalUtil::_verbose ==0 && GlobalUtil::_UseSiftGPUEX)
std::cout << "Console output disabled, press Q/V to enable\n\n";
}else
{
GlobalUtil::_verbose = 1;
GlobalUtil::_timingS = 1;
}
}else if(verbose == -2)
{
//trick for disabling all output (still keeps the timing level)
GlobalUtil::_verbose = 0;
GlobalUtil::_timingS = 1;
}else
{
GlobalUtil::_verbose = verbose>0;
GlobalUtil::_timingS = verbose>1;
}
}
SiftParam::SiftParam()
{
_level_min = -1;
_dog_level_num = 3;
_level_max = 0;
_sigma0 = 0;
_sigman = 0;
_edge_threshold = 0;
_dog_threshold = 0;
}
float SiftParam::GetInitialSmoothSigma(int octave_min)
{
float sa = _sigma0 * powf(2.0f, float(_level_min)/float(_dog_level_num)) ;
float sb = _sigman / powf(2.0f, float(octave_min)) ;//
float sigma_skip0 = sa > sb + 0.001?sqrt(sa*sa - sb*sb): 0.0f;
return sigma_skip0;
}
void SiftParam::ParseSiftParam()
{
if(_dog_level_num ==0) _dog_level_num = 3;
if(_level_max ==0) _level_max = _dog_level_num + 1;
if(_sigma0 ==0.0f) _sigma0 = 1.6f * powf(2.0f, 1.0f / _dog_level_num) ;
if(_sigman == 0.0f) _sigman = 0.5f;
_level_num = _level_max -_level_min + 1;
_level_ds = _level_min + _dog_level_num;
if(_level_ds > _level_max ) _level_ds = _level_max ;
///
float _sigmak = powf(2.0f, 1.0f / _dog_level_num) ;
float dsigma0 = _sigma0 * sqrt (1.0f - 1.0f / (_sigmak*_sigmak) ) ;
float sa, sb;
sa = _sigma0 * powf(_sigmak, (float)_level_min) ;
sb = _sigman / powf(2.0f, (float)GlobalUtil::_octave_min_default) ;//
_sigma_skip0 = sa>sb+ 0.001?sqrt(sa*sa - sb*sb): 0.0f;
sa = _sigma0 * powf(_sigmak, float(_level_min )) ;
sb = _sigma0 * powf(_sigmak, float(_level_ds - _dog_level_num)) ;
_sigma_skip1 = sa>sb + 0.001? sqrt(sa*sa - sb*sb): 0.0f;
_sigma_num = _level_max - _level_min;
_sigma = new float[_sigma_num];
for(int i = _level_min + 1; i <= _level_max; i++)
{
_sigma[i-_level_min -1] = dsigma0 * powf(_sigmak, float(i)) ;
}
if(_dog_threshold ==0) _dog_threshold = 0.02f / _dog_level_num ;
if(_edge_threshold==0) _edge_threshold = 10.0f;
}
void SiftGPUEX::DisplayOctave(void (*UseDisplayShader)(), int i)
{
if(_pyramid == NULL)return;
const int grid_sz = (int)ceil(_level_num/2.0);
double scale = 1.0/grid_sz ;
int gx=0, gy=0, dx, dy;
if(_pyramid->_octave_min >0) scale *= (1<<_pyramid->_octave_min);
else if(_pyramid->_octave_min < 0) scale /= (1<<(-_pyramid->_octave_min));
i = i% _pyramid->_octave_num; //
if(i<0 ) i+= _pyramid->_octave_num;
scale *= ( 1<<(i));
UseDisplayShader();
glPushMatrix();
glScaled(scale, scale, scale);
for(int level = _level_min; level<= _level_max; level++)
{
GLTexImage * tex = _pyramid->GetLevelTexture(i+_pyramid->_octave_min, level);
dx = tex->GetImgWidth();
dy = tex->GetImgHeight();
glPushMatrix();
glTranslated(dx*gx, dy*gy, 0);
tex->BindTex();
tex->DrawImage();
tex->UnbindTex();
glPopMatrix();
gx++;
if(gx>=grid_sz)
{
gx =0;
gy++;
}
}
glPopMatrix();
ShaderMan::UnloadProgram();
}
void SiftGPUEX::DisplayPyramid( void (*UseDisplayShader)(), int dataName, int nskip1, int nskip2)
{
if(_pyramid == NULL)return;
int grid_sz = (_level_num -nskip1 - nskip2);
if(grid_sz > 4) grid_sz = (int)ceil(grid_sz*0.5);
double scale = 1.0/grid_sz;
int stepx = 0, stepy = 0, dx, dy=0, nstep;
if(_pyramid->_octave_min >0) scale *= (1<<_pyramid->_octave_min);
else if(_pyramid->_octave_min < 0) scale /= (1<<(-_pyramid->_octave_min));
glPushMatrix();
glScaled(scale, scale, scale);
for(int i = _pyramid->_octave_min; i < _pyramid->_octave_min+_pyramid->_octave_num; i++)
{
nstep = i==_pyramid->_octave_min? grid_sz: _level_num;
dx = 0;
UseDisplayShader();
for(int j = _level_min + nskip1; j <= _level_max-nskip2; j++)
{
GLTexImage * tex = _pyramid->GetLevelTexture(i, j, dataName);
if(tex->GetImgWidth() == 0 || tex->GetImgHeight() == 0) continue;
stepx = tex->GetImgWidth();
stepy = tex->GetImgHeight();
////
if(j == _level_min + nskip1 + nstep)
{
dy += stepy;
dx = 0;
}
glPushMatrix();
glTranslated(dx, dy, 0);
tex->BindTex();
tex->DrawImage();
tex->UnbindTex();
glPopMatrix();
dx += stepx;
}
ShaderMan::UnloadProgram();
dy+= stepy;
}
glPopMatrix();
}
void SiftGPUEX::DisplayLevel(void (*UseDisplayShader)(), int i)
{
if(_pyramid == NULL)return;
i = i%(_level_num * _pyramid->_octave_num);
if (i<0 ) i+= (_level_num * _pyramid->_octave_num);
int octave = _pyramid->_octave_min + i/_level_num;
int level = _level_min + i%_level_num;
double scale = 1.0;
if(octave >0) scale *= (1<<octave);
else if(octave < 0) scale /= (1<<(-octave));
GLTexImage * tex = _pyramid->GetLevelTexture(octave, level);
UseDisplayShader();
glPushMatrix();
glScaled(scale, scale, scale);
tex->BindTex();
tex->DrawImage();
tex->UnbindTex();
glPopMatrix();
ShaderMan::UnloadProgram();
}
void SiftGPUEX::DisplaySIFT()
{
if(_pyramid == NULL) return;
glEnable(GlobalUtil::_texTarget);
switch(_view)
{
case 0:
DisplayInput();
DisplayFeatureBox(_sub_view);
break;
case 1:
DisplayPyramid(ShaderMan::UseShaderDisplayGaussian, SiftPyramid::DATA_GAUSSIAN);
break;
case 2:
DisplayOctave(ShaderMan::UseShaderDisplayGaussian, _sub_view);
break;
case 3:
DisplayLevel(ShaderMan::UseShaderDisplayGaussian, _sub_view);
break;
case 4:
DisplayPyramid(ShaderMan::UseShaderDisplayDOG, SiftPyramid::DATA_DOG, 1);
break;
case 5:
DisplayPyramid(ShaderMan::UseShaderDisplayGrad, SiftPyramid::DATA_GRAD, 1);
break;
case 6:
DisplayPyramid(ShaderMan::UseShaderDisplayDOG, SiftPyramid::DATA_DOG,2, 1);
DisplayPyramid(ShaderMan::UseShaderDisplayKeypoints, SiftPyramid::DATA_KEYPOINT, 2,1);
}
}
void SiftGPUEX::SetView(int view, int sub_view, char *title)
{
const char* view_titles[] =
{
"Original Image",
"Gaussian Pyramid",
"Octave Images",
"Level Image",
"Difference of Gaussian",
"Gradient",
"Keypoints"
};
const int view_num = 7;
_view = view % view_num;
if(_view <0) _view +=view_num;
_sub_view = sub_view;
if(_view_debug)
strcpy(title, "Debug...");
else
strcpy(title, view_titles[_view]);
}
void SiftGPU::PrintUsage()
{
std::cout
<<"SiftGPU Usage:\n"
<<"-h -help : Parameter information\n"
<<"-i <strings> : Filename(s) of the input image(s)\n"
<<"-il <string> : Filename of an image list file\n"
<<"-o <string> : Where to save SIFT features\n"
<<"-f <float> : Filter width factor; Width will be 2*factor+1 (default : 4.0)\n"
<<"-w <float> : Orientation sample window factor (default: 2.0)\n"
<<"-dw <float> * : Descriptor grid size factor (default : 3.0)\n"
<<"-fo <int> * : First octave to detect DOG keypoints(default : 0)\n"
<<"-no <int> : Maximum number of Octaves (default : no limit)\n"
<<"-d <int> : Number of DOG levels in an octave (default : 3)\n"
<<"-t <float> : DOG threshold (default : 0.02/3)\n"
<<"-e <float> : Edge Threshold (default : 10.0)\n"
<<"-m <int=2> : Multi Feature Orientations (default : 1)\n"
<<"-m2p : 2 Orientations packed as one float\n"
<<"-s <int=1> : Sub-Pixel, Sub-Scale Localization, Multi-Refinement(num)\n"
<<"-lcpu -lc <int> : CPU/GPU mixed Feature List Generation (default: 6)\n"
<<" Use GPU first, and use CPU when reduction size <= pow(2,num)\n"
<<" When <num> is missing or equals -1, no GPU will be used\n"
<<"-noprep : Upload raw data to GPU (default: RGB->LUM and down-sample on CPU)\n"
<<"-sd : Skip descriptor computation if specified\n"
<<"-unn * : Write unnormalized descriptor if specified\n"
<<"-b * : Write binary sift file if specified\n"
<<"-fs <int> : Block Size for freature storage <default : 4>\n"
<<"-cuda <int=0> : Use CUDA SiftGPU, and specify the device index\n"
<<"-tight : Automatically resize pyramid to fit new images tightly\n"
<<"-p <W>x<H> : Inititialize the pyramids to contain image of WxH (eg -p 1024x768)\n"
<<"-tc[1|2|3] <int> *: Threshold for limiting the overall number of features (3 methods)\n"
<<"-v <int> : Level of timing details. Same as calling Setverbose() function\n"
<<"-loweo : (0, 0) at center of top-left pixel (default: corner)\n"
<<"-maxd <int> * : Max working dimension (default : 2560 (unpacked) / 3200 (packed))\n"
<<"-nomc : Disabling auto-downsamping that try to fit GPU memory cap\n"
<<"-exit : Exit program after processing the input image\n"
<<"-unpack : Use the old unpacked implementation\n"
<<"-di : Use dynamic array indexing if available (default : no)\n"
<<" It could make computation faster on cards like GTX 280\n"
<<"-ofix * : use 0 as feature orientations.\n"
<<"-ofix-not * : disable -ofix.\n"
<<"-winpos <X>x<Y> * : Screen coordinate used in Win32 to select monitor/GPU.\n"
<<"-display <string>*: Display name used in Linux/Mac to select monitor/GPU.\n"
<<"\n"
<<"NOTE: parameters marked with * can be changed after initialization\n"
<<"\n";
}
void SiftGPU::ParseParam(const int argc, const char **argv)
{
#define CHAR1_TO_INT(x) ((x >= 'A' && x <= 'Z') ? x + 32 : x)
#define CHAR2_TO_INT(str, i) (str[i] ? CHAR1_TO_INT(str[i]) + (CHAR1_TO_INT(str[i+1]) << 8) : 0)
#define CHAR3_TO_INT(str, i) (str[i] ? CHAR1_TO_INT(str[i]) + (CHAR2_TO_INT(str, i + 1) << 8) : 0)
#define STRING_TO_INT(str) (CHAR1_TO_INT(str[0]) + (CHAR3_TO_INT(str, 1) << 8))
#ifdef _MSC_VER
//charizing is microsoft only
#define MAKEINT1(a) (#@a )
#else
#define mychar0 '0'
#define mychar1 '1'
#define mychar2 '2'
#define mychar3 '3'
#define mychara 'a'
#define mycharb 'b'
#define mycharc 'c'
#define mychard 'd'
#define mychare 'e'
#define mycharf 'f'
#define mycharg 'g'
#define mycharh 'h'
#define mychari 'i'
#define mycharj 'j'
#define mychark 'k'
#define mycharl 'l'
#define mycharm 'm'
#define mycharn 'n'
#define mycharo 'o'
#define mycharp 'p'
#define mycharq 'q'
#define mycharr 'r'
#define mychars 's'
#define mychart 't'
#define mycharu 'u'
#define mycharv 'v'
#define mycharw 'w'
#define mycharx 'x'
#define mychary 'y'
#define mycharz 'z'
#define MAKEINT1(a) (mychar##a )
#endif
#define MAKEINT2(a, b) (MAKEINT1(a) + (MAKEINT1(b) << 8))
#define MAKEINT3(a, b, c) (MAKEINT1(a) + (MAKEINT2(b, c) << 8))
#define MAKEINT4(a, b, c, d) (MAKEINT1(a) + (MAKEINT3(b, c, d) << 8))
const char* arg, *param, * opt;
int setMaxD = 0, opti;
for(int i = 0; i< argc; i++)
{
arg = argv[i];
if(arg == NULL || arg[0] != '-' || !arg[1])continue;
opt = arg+1;
opti = STRING_TO_INT(opt);
param = argv[i+1];
////////////////////////////////
switch(opti)
{
case MAKEINT1(h):
case MAKEINT4(h, e, l, p):
PrintUsage();
break;
case MAKEINT4(c, u, d, a):
#if defined(SIFTGPU_CUDA_ENABLED)
if(!_initialized)
{
GlobalUtil::_UseCUDA = 1;
int device = -1;
if(i+1 <argc && sscanf(param, "%d", &device) && device >=0)
{
GlobalUtil::_DeviceIndex = device;
i++;
}
}
#else
std::cerr << "---------------------------------------------------------------------------\n"
<< "CUDA not supported in this binary! To enable it, please use SiftGPU_CUDA_Enable\n"
<< "solution for VS2005+ or set siftgpu_enable_cuda to 1 in makefile\n"
<< "----------------------------------------------------------------------------\n";
#endif
break;
case MAKEINT2(c, l):
#if defined(CL_SIFTGPU_ENABLED)
if(!_initialized) GlobalUtil::_UseOpenCL = 1;
#else
std::cerr << "---------------------------------------------------------------------------\n"
<< "OpenCL not supported in this binary! Define CL_SIFTGPU_CUDA_ENABLED to..\n"
<< "----------------------------------------------------------------------------\n";
#endif
break;
case MAKEINT4(p, a, c, k):
if(!_initialized) GlobalUtil::_usePackedTex = 1;
break;
case MAKEINT4(u, n, p, a): //unpack
if(!_initialized)
{
GlobalUtil::_usePackedTex = 0;
if(!setMaxD) GlobalUtil::_texMaxDim = 2560;
}
break;
case MAKEINT4(l, c, p, u):
case MAKEINT2(l, c):
if(!_initialized)
{
int gskip = -1;
if(i+1 <argc) sscanf(param, "%d", &gskip);
if(gskip >= 0)
{
GlobalUtil::_ListGenSkipGPU = gskip;
}else
{
GlobalUtil::_ListGenGPU = 0;
}
}
break;
case MAKEINT4(p, r, e, p):
GlobalUtil::_PreProcessOnCPU = 1;
break;
case MAKEINT4(n, o, p, r): //noprep
GlobalUtil::_PreProcessOnCPU = 0;
break;
case MAKEINT4(f, b, o, 1):
FrameBufferObject::UseSingleFBO =1;
break;
case MAKEINT4(f, b, o, s):
FrameBufferObject::UseSingleFBO = 0;
break;
case MAKEINT2(s, d):
if(!_initialized) GlobalUtil::_DescriptorPPT =0;
break;
case MAKEINT3(u, n, n):
GlobalUtil::_NormalizedSIFT =0;
break;
case MAKEINT4(n, d, e, s):
GlobalUtil::_NormalizedSIFT =1;
break;
case MAKEINT1(b):
GlobalUtil::_BinarySIFT = 1;
break;
case MAKEINT4(t, i, g, h): //tight
GlobalUtil::_ForceTightPyramid = 1;
break;
case MAKEINT4(e, x, i, t):
GlobalUtil::_ExitAfterSIFT = 1;
break;
case MAKEINT2(d, i):
GlobalUtil::_UseDynamicIndexing = 1;
break;
case MAKEINT4(s, i, g, n):
if(!_initialized || GlobalUtil::_UseCUDA) GlobalUtil::_KeepExtremumSign = 1;
break;
case MAKEINT1(m):
case MAKEINT2(m, o):
if(!_initialized)
{
int mo = 2; //default multi-orientation
if(i+1 <argc) sscanf(param, "%d", &mo);
//at least two orientation
GlobalUtil::_MaxOrientation = min(max(1, mo), 4);
}
break;
case MAKEINT3(m, 2, p):
if(!_initialized)
{
GlobalUtil::_MaxOrientation = 2;
GlobalUtil::_OrientationPack2 = 1;
}
break;
case MAKEINT1(s):
if(!_initialized)
{
int sp = 1; //default refinement
if(i+1 <argc) sscanf(param, "%d", &sp);
//at least two orientation
GlobalUtil::_SubpixelLocalization = min(max(0, sp),5);
}
break;
case MAKEINT4(o, f, i, x):
GlobalUtil::_FixedOrientation = (_stricmp(opt, "ofix")==0);
break;
case MAKEINT4(l, o, w, e): // loweo
GlobalUtil::_LoweOrigin = 1;
break;
case MAKEINT4(n, a, r, r): // narrow
GlobalUtil::_NarrowFeatureTex = 1;
break;
case MAKEINT4(d, e, b, u): // debug
GlobalUtil::_debug = 1;
break;
case MAKEINT2(k, 0):
GlobalUtil::_KeyPointListForceLevel0 = 1;
break;
case MAKEINT2(k, x):
GlobalUtil::_KeyPointListForceLevel0 = 0;
break;
case MAKEINT2(d, a):
GlobalUtil::_DarknessAdaption = 1;
break;
case MAKEINT3(f, m, c):
GlobalUtil::_FitMemoryCap = 1;
break;
case MAKEINT4(n, o, m, c):
GlobalUtil::_FitMemoryCap = 0;
break;
default:
if(i + 1 >= argc) break;
switch(opti)
{
case MAKEINT1(i):
strcpy(_imgpath, param);
i++;
//get the file list..
_list->push_back(param);
while( i+1 < argc && argv[i+1][0] !='-')
{
_list->push_back(argv[++i]);
}
break;
case MAKEINT2(i, l):
LoadImageList(param);
i++;
break;
case MAKEINT1(o):
strcpy(_outpath, param);
i++;
break;
case MAKEINT1(f):
{
float factor = 0.0f;
if(sscanf(param, "%f", &factor) && factor > 0 )
{
GlobalUtil::_FilterWidthFactor = factor;
i++;
}
}
break;
case MAKEINT2(o, t):
{
float factor = 0.0f;
if(sscanf(param, "%f", &factor) && factor>0 )
{
GlobalUtil::_MulitiOrientationThreshold = factor;
i++;
}
break;
}
case MAKEINT1(w):
{
float factor = 0.0f;
if(sscanf(param, "%f", &factor) && factor>0 )
{
GlobalUtil::_OrientationWindowFactor = factor;
i++;
}
break;
}
case MAKEINT2(d, w):
{
float factor = 0.0f;
if(sscanf(param, "%f", &factor) && factor > 0 )
{
GlobalUtil::_DescriptorWindowFactor = factor;
i++;
}
break;
}
case MAKEINT2(f, o):
{
int first_octave = -3;
if(sscanf(param, "%d", &first_octave) && first_octave >=-2 )
{
GlobalUtil::_octave_min_default = first_octave;
i++;
}
break;
}
case MAKEINT2(n, o):
if(!_initialized)
{
int octave_num=-1;
if(sscanf(param, "%d", &octave_num))
{
octave_num = max(-1, octave_num);
if(octave_num ==-1 || octave_num >=1)
{
GlobalUtil::_octave_num_default = octave_num;
i++;
}
}
}