ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
SiftGPU.h
Go to the documentation of this file.
1 // File: SiftGPU.h
3 // Author: Changchang Wu
4 // Description : interface for the SIFTGPU class.
5 // SiftGPU: The SiftGPU Tool.
6 // SiftGPUEX: SiftGPU + viewer
7 // SiftParam: Sift Parameters
8 // SiftMatchGPU: GPU SIFT Matcher;
9 //
10 //
11 // Copyright (c) 2007 University of North Carolina at Chapel Hill
12 // All Rights Reserved
13 //
14 // Permission to use, copy, modify and distribute this software and its
15 // documentation for educational, research and non-profit purposes, without
16 // fee, and without a written agreement is hereby granted, provided that the
17 // above copyright notice and the following paragraph appear in all copies.
18 //
19 // The University of North Carolina at Chapel Hill make no representations
20 // about the suitability of this software for any purpose. It is provided
21 // 'as is' without express or implied warranty.
22 //
23 // Please send BUG REPORTS to ccwu@cs.unc.edu
24 //
26 
27 
28 #ifndef GPU_SIFT_H
29 #define GPU_SIFT_H
30 
31 #if defined(_WIN32)
32  #ifdef SIFTGPU_DLL
33  #ifdef DLL_EXPORT
34  #define SIFTGPU_EXPORT __declspec(dllexport)
35  #else
36  #define SIFTGPU_EXPORT __declspec(dllimport)
37  #endif
38  #else
39  #define SIFTGPU_EXPORT
40  #endif
41  #define SIFTGPU_EXPORT_EXTERN SIFTGPU_EXPORT
42  #if _MSC_VER > 1000
43  #pragma once
44  #endif
45 #else
46  #define SIFTGPU_EXPORT
47  #define SIFTGPU_EXPORT_EXTERN extern "C"
48 #endif
49 
50 #ifdef _MSC_VER
51 #if _MSC_VER >= 1600
52 #include <stdint.h>
53 #else
54 typedef __int8 int8_t;
55 typedef __int16 int16_t;
56 typedef __int32 int32_t;
57 typedef __int64 int64_t;
58 typedef unsigned __int8 uint8_t;
59 typedef unsigned __int16 uint16_t;
60 typedef unsigned __int32 uint32_t;
61 typedef unsigned __int64 uint64_t;
62 #endif
63 #elif __GNUC__ >= 3
64 #include <stdint.h>
65 #endif
66 
68 //clss SiftParam
69 //description: SIFT parameters
71 class GlobalUtil;
72 class SiftParam
73 {
74 public:
75  float* _sigma;
76  float _sigma_skip0; //
77  float _sigma_skip1; //
78 
79  //sigma of the first level
80  float _sigma0;
81  float _sigman;
83 
84  //how many dog_level in an octave
87 
88  //starting level in an octave
91  int _level_ds;
92  //dog threshold
94  //edge elimination
96  void ParseSiftParam();
97 public:
98  float GetLevelSigma(int lev);
99  float GetInitialSmoothSigma(int octave_min);
101 };
102 
103 class LiteWindow;
104 class GLTexInput;
105 class ShaderMan;
106 class SiftPyramid;
107 class ImageList;
109 //class SIftGPU
110 //description: Interface of SiftGPU lib
112 class SiftGPU:public SiftParam
113 {
114 public:
115  enum
116  {
118  SIFTGPU_PARTIAL_SUPPORTED = 1, // detction works, but not orientation/descriptor
120  };
121 
122  int gpu_index = 0;
123 
124  typedef struct SiftKeypoint
125  {
126  float x, y, s, o; //x, y, scale, orientation.
128 protected:
129  //when more than one images are specified
130  //_current indicates the active one
131  int _current;
132  //_initialized indicates if the shaders and OpenGL/SIFT parameters are initialized
133  //they are initialized only once for one SiftGPU inistance
134  //that is, SIFT parameters will not be changed
136  //_image_loaded indicates if the current images are loaded
138  //the name of current input image
139  char* _imgpath;
140  //_outpath containes the name of the output file
141  char* _outpath;
142  //the list of image filenames
144  //the texture that holds loaded input image
146  //the SiftPyramid
148  //print out the command line options
149  static void PrintUsage();
150  //Initialize OpenGL and SIFT paremeters, and create the shaders accordingly
151  void InitSiftGPU();
152  //load the image list from a file
153  void LoadImageList(const char *imlist);
154 public:
155  //timing results for 10 steps
156  float _timing[10];
157  inline const char* GetCurrentImagePath() {return _imgpath; }
158 public:
159  //set the image list for processing
160  SIFTGPU_EXPORT virtual void SetImageList(int nimage, const char** filelist);
161  //get the number of SIFT features in current image
162  SIFTGPU_EXPORT virtual int GetFeatureNum();
163  //save the SIFT result as a ANSCII/BINARY file
164  SIFTGPU_EXPORT virtual void SaveSIFT(const char * szFileName);
165  //Copy the SIFT result to two vectors
166  SIFTGPU_EXPORT virtual void GetFeatureVector(SiftKeypoint * keys, float * descriptors);
167  //Set keypoint list before running sift to get descriptors
168  SIFTGPU_EXPORT virtual void SetKeypointList(int num, const SiftKeypoint * keys, int keys_have_orientation = 1);
169  //Enable downloading results to CPU.
170  //create a new OpenGL context for processing
171  //call VerifyContextGL instead if you want to crate openGL context yourself, or your are
172  //mixing mixing siftgpu with other openGL code
173  SIFTGPU_EXPORT virtual int CreateContextGL();
174  //verify the current opengl context..
175  //(for example, you call wglmakecurrent yourself and verify the current context)
176  SIFTGPU_EXPORT virtual int VerifyContextGL();
177  //check if all siftgpu functions are supported
178  SIFTGPU_EXPORT virtual int IsFullSupported();
179  //set verbose mode
180  SIFTGPU_EXPORT virtual void SetVerbose(int verbose = 4);
181  //set SiftGPU to brief display mode, which is faster
182  inline void SetVerboseBrief(){SetVerbose(2);};
183  //parse SiftGPU parameters
184  SIFTGPU_EXPORT virtual void ParseParam(int argc, const char** argv);
185  //run SIFT on a new image given filename
186  SIFTGPU_EXPORT virtual int RunSIFT(const char * imgpath);
187  //run SIFT on an image in the image list given the file index
188  SIFTGPU_EXPORT virtual int RunSIFT(int index);
189  //run SIFT on a new image given the pixel data and format/type;
190  //gl_format (e.g. GL_LUMINANCE, GL_RGB) is the format of the pixel data
191  //gl_type (e.g. GL_UNSIGNED_BYTE, GL_FLOAT) is the data type of the pixel data;
192  //Check glTexImage2D(...format, type,...) for the accepted values
193  //Using image data of GL_LUMINANCE + GL_UNSIGNED_BYTE can minimize transfer time
194  SIFTGPU_EXPORT virtual int RunSIFT(int width, int height, const void * data,
195  unsigned int gl_format, unsigned int gl_type);
196  //run SIFT on current image (specified by arguments), or processing the current image again
197  SIFTGPU_EXPORT virtual int RunSIFT();
198  //run SIFT with keypoints on current image again.
199  SIFTGPU_EXPORT virtual int RunSIFT(int num, const SiftKeypoint * keys, int keys_have_orientation = 1);
200  //constructor, the parameter np is ignored..
201  SIFTGPU_EXPORT explicit SiftGPU(int np = 1);
202  //destructor
203  SIFTGPU_EXPORT virtual ~SiftGPU();
204  //set the active pyramid...dropped function
205  SIFTGPU_EXPORT virtual void SetActivePyramid(int) {};
206  //retrieve the number of images in the image list
207  SIFTGPU_EXPORT virtual int GetImageCount();
208  //set parameter GlobalUtil::_ForceTightPyramid
209  SIFTGPU_EXPORT virtual void SetTightPyramid(int tight = 1);
210  //allocate pyramid for a given size of image
211  SIFTGPU_EXPORT virtual int AllocatePyramid(int width, int height);
212  //none of the texture in processing can be larger
213  //automatic down-sample is used if necessary.
214  SIFTGPU_EXPORT virtual void SetMaxDimension(int sz);
218 };
219 
220 
221 
223 //class SIftGPUEX
224 //description: adds some visualization functions to the interface of SiftGPU
226 
227 class SiftGPUEX:public SiftGPU
228 {
229  //view mode
230  int _view;
231  //sub view mode
232  int _sub_view;
233  //whether display a debug view
234  int _view_debug;
235  //colors for SIFT feature display
236  enum{COLOR_NUM = 36};
237  float _colors[COLOR_NUM*3];
238  //display functions
239  void DisplayInput(); //display gray level image of input image
240  void DisplayDebug(); //display debug view
241  void DisplayFeatureBox(int i); //display SIFT features
242  void DisplayLevel(void (*UseDisplayShader)(), int i); //display one level image
243  void DisplayOctave(void (*UseDisplayShader)(), int i); //display all images in one octave
244  //display different content of Pyramid by specifying different data and display shader
245  //the first nskip1 levels and the last nskip2 levels are skiped in display
246  void DisplayPyramid( void (*UseDisplayShader)(), int dataName, int nskip1 = 0, int nskip2 = 0);
247  //use HSVtoRGB to generate random colors
248  static void HSVtoRGB(float hsv[3],float rgb[3]);
249 
250 public:
252  //change view mode
253  SIFTGPU_EXPORT void SetView(int view, int sub_view, char * title);
254  //display current view
256  //toggle debug mode on/off
258  //randomize the display colors
260  //retrieve the size of current input image
261  SIFTGPU_EXPORT void GetImageDimension(int &w, int&h);
262  //get the location of the window specified by user
263  SIFTGPU_EXPORT void GetInitWindowPotition(int& x, int& y);
264 };
265 
267 //This is a gpu-based sift match implementation.
269 {
270 public:
272  SIFTMATCH_SAME_AS_SIFTGPU = 0, //when siftgpu already initialized.
275  SIFTMATCH_CUDA_DEVICE0 = 3 //to use device i, use SIFTMATCH_CUDA_DEVICE0 + i
276  };
277 
278  int gpu_index = 0;
279 
280 private:
281  int __language;
282  SiftMatchGPU * __matcher;
283  virtual void InitSiftMatch(){}
284 protected:
286  //move the two functions here for derived class
287  SIFTGPU_EXPORT virtual int _CreateContextGL();
288  SIFTGPU_EXPORT virtual int _VerifyContextGL();
289 public:
290  //OpenGL Context creation/verification, initialization is done automatically inside
291  inline int CreateContextGL() {return _CreateContextGL();}
292  inline int VerifyContextGL() {return _VerifyContextGL();}
293 
294  //Consructor, the argument specifies the maximum number of features to match
295  SIFTGPU_EXPORT explicit SiftMatchGPU(int max_sift = 4096);
296 
297  //change gpu_language, check the enumerants in SIFTMATCH_LANGUAGE.
298  SIFTGPU_EXPORT virtual void SetLanguage(int gpu_language);
299 
300  //after calling SetLanguage, you can call SetDeviceParam to select GPU
301  //-winpos, -display, -cuda [device_id]
302  //This is only used when you call CreateContextGL..
303  //This function doesn't change the language.
304  SIFTGPU_EXPORT virtual void SetDeviceParam(int argc, char**argv);
305 
306  // Allocate all matrices the matrices and return true if successful.
307  virtual bool Allocate(int max_sift, int mbm);
308 
309  //change the maximum of features to match whenever you want
310  SIFTGPU_EXPORT virtual void SetMaxSift(int max_sift);
311  SIFTGPU_EXPORT virtual int GetMaxSift() const { return __max_sift; };
312  //desctructor
313  SIFTGPU_EXPORT virtual ~SiftMatchGPU();
314 
315  //Specifiy descriptors to match, index = [0/1] for two features sets respectively
316  //Option1, use float descriptors, and they be already normalized to 1.0
317  SIFTGPU_EXPORT virtual void SetDescriptors(int index, int num, const float* descriptors, int id = -1);
318  //Option 2 unsigned char descriptors. They must be already normalized to 512
319  SIFTGPU_EXPORT virtual void SetDescriptors(int index, int num, const unsigned char * descriptors, int id = -1);
320 
321  //match two sets of features, the function RETURNS the number of matches.
322  //Given two normalized descriptor d1,d2, the distance here is acos(d1 *d2);
323  SIFTGPU_EXPORT virtual int GetSiftMatch(
324  int max_match, // the length of the match_buffer.
325  uint32_t match_buffer[][2], //buffer to receive the matched feature indices
326  float distmax = 0.7, //maximum distance of sift descriptor
327  float ratiomax = 0.8, //maximum distance ratio
328  int mutual_best_match = 1); //mutual best match or one way
329 
330  //two functions for guded matching, two constraints can be used
331  //one homography and one fundamental matrix, the use is as follows
332  //1. for each image, first call SetDescriptor then call SetFeatureLocation
333  //2. Call GetGuidedSiftMatch
334  //input feature location is a vector of [float x, float y, float skip[gap]]
335  SIFTGPU_EXPORT virtual void SetFeautreLocation(int index, const float* locations, int gap = 0);
336  inline void SetFeatureLocation(int index, const SiftGPU::SiftKeypoint * keys)
337  {
338  SetFeautreLocation(index, (const float*) keys, 2);
339  }
340 
341  //use a guiding Homography H and a guiding Fundamental Matrix F to compute feature matches
342  //the function returns the number of matches.
344  int max_match, uint32_t match_buffer[][2], //buffer to recieve
345  float* H, //homography matrix, (Set NULL to skip)
346  float* F, //fundamental matrix, (Set NULL to skip)
347  float distmax = 0.7, //maximum distance of sift descriptor
348  float ratiomax = 0.8, //maximum distance ratio
349  float hdistmax = 32, //threshold for |H * x1 - x2|_2
350  float fdistmax = 16, //threshold for sampson error of x2'FX1
351  int mutual_best_match = 1); //mutual best or one wayx
352 };
353 
355 
356 //Two exported global functions used to create SiftGPU and SiftMatchGPU
359 
360 
362 class ComboSiftGPU: public SiftGPU, public SiftMatchGPU
363 {
364 };
366 
368 //Multi-process mode and remote mode
369 SIFTGPU_EXPORT_EXTERN ComboSiftGPU* CreateRemoteSiftGPU(int port = 7777, char* remote_server = NULL);
370 //Run SiftGPU computation on a remote computer/process/thread
371 //if( remote_server == NULL)
372 // a local server is created in a different process and connected
373 // multiple-GPU can be used by creating multiple instances
374 // GPU selection done through SiftGPU::ParseParam function
375 //otherwise,
376 // Assumes the existenc of a remote server and connects to it
377 // GPU selection skipped if already done on the server-end
378 // RUN server: server_siftgpu -server port [siftgpu_param]
379 //example:
380 // ComboSiftGPU * combo = CreateRemoteSiftGPU(7777, "my.gpuserver.com");
381 // SiftGPU* siftgpu = combo, SiftMatchGPU * matcher = combo;
382 // siftgpu->ParseParam... siftgpu->CreateContextGL..
383 // matcher->SetLanguage...matcher->VerifyContextGL...
384 // // GPU-selection is done throught siftgpu->ParseParam,
385 // // it doesn't really initialize SiftGPU untill you call CreateContextGL/VerifyContextGL
386 // delete combo;
387 
389 //two internally used function.
391 SIFTGPU_EXPORT void RunServerLoop(int port, int argc, char** argv);
392 #endif
int width
int height
ComboSiftGPU * CreateRemoteSiftGPU(int port=7777, char *remote_server=NULL)
SiftMatchGPU * CreateNewSiftMatchGPU(int max_sift=4096)
Definition: SiftMatch.cpp:696
#define SIFTGPU_EXPORT_EXTERN
Definition: SiftGPU.h:47
SiftGPU * CreateNewSiftGPU(int np=1)
Definition: SiftGPU.cpp:1426
int CreateLiteWindow(LiteWindow *window)
Definition: GlobalUtil.cpp:464
#define SIFTGPU_EXPORT
Definition: SiftGPU.h:46
ComboSiftGPU * CreateComboSiftGPU()
Definition: SiftGPU.cpp:1433
void RunServerLoop(int port, int argc, char **argv)
SiftGPU::SiftKeypoint SiftKeypoint
Definition: SiftGPU.h:354
#define NULL
void GetInitWindowPotition(int &x, int &y)
Definition: SiftGPU.cpp:1420
void RandomizeColor()
Definition: SiftGPU.cpp:109
void DisplaySIFT()
Definition: SiftGPU.cpp:601
void SetView(int view, int sub_view, char *title)
Definition: SiftGPU.cpp:633
SiftGPUEX()
Definition: SiftGPU.cpp:99
void ToggleDisplayDebug()
Definition: SiftGPU.cpp:1259
void GetImageDimension(int &w, int &h)
Definition: SiftGPU.cpp:1413
virtual void ParseParam(int argc, const char **argv)
Definition: SiftGPU.cpp:706
virtual void GetFeatureVector(SiftKeypoint *keys, float *descriptors)
Definition: SiftGPU.cpp:1317
virtual void SetKeypointList(int num, const SiftKeypoint *keys, int keys_have_orientation=1)
Definition: SiftGPU.cpp:355
int GetMaxDimension()
Definition: SiftGPU.cpp:1371
ImageList * _list
Definition: SiftGPU.h:143
virtual int VerifyContextGL()
Definition: SiftGPU.cpp:1296
SiftGPU(int np=1)
Definition: SiftGPU.cpp:84
virtual void SaveSIFT(const char *szFileName)
Definition: SiftGPU.cpp:1307
virtual void SetTightPyramid(int tight=1)
Definition: SiftGPU.cpp:1331
@ SIFTGPU_NOT_SUPPORTED
Definition: SiftGPU.h:117
@ SIFTGPU_FULL_SUPPORTED
Definition: SiftGPU.h:119
@ SIFTGPU_PARTIAL_SUPPORTED
Definition: SiftGPU.h:118
virtual int RunSIFT()
Definition: SiftGPU.cpp:273
int _image_loaded
Definition: SiftGPU.h:137
struct SiftGPU::SiftKeypoint SiftKeypoint
GLTexInput * _texImage
Definition: SiftGPU.h:145
static void PrintUsage()
Definition: SiftGPU.cpp:658
virtual int AllocatePyramid(int width, int height)
Definition: SiftGPU.cpp:1336
char * _imgpath
Definition: SiftGPU.h:139
virtual void SetImageList(int nimage, const char **filelist)
Definition: SiftGPU.cpp:1153
const char * GetCurrentImagePath()
Definition: SiftGPU.h:157
virtual void SetVerbose(int verbose=4)
Definition: SiftGPU.cpp:370
void InitSiftGPU()
Definition: SiftGPU.cpp:129
int _current
Definition: SiftGPU.h:131
int GetMaxOrientation()
Definition: SiftGPU.cpp:1366
virtual void SetActivePyramid(int)
Definition: SiftGPU.h:205
virtual void SetMaxDimension(int sz)
Definition: SiftGPU.cpp:1353
virtual int IsFullSupported()
Definition: SiftGPU.cpp:1302
virtual int GetImageCount()
Definition: SiftGPU.cpp:1376
int _initialized
Definition: SiftGPU.h:135
virtual int CreateContextGL()
Definition: SiftGPU.cpp:1278
float _timing[10]
Definition: SiftGPU.h:156
void LoadImageList(const char *imlist)
Definition: SiftGPU.cpp:1163
SiftPyramid * _pyramid
Definition: SiftGPU.h:147
virtual ~SiftGPU()
Definition: SiftGPU.cpp:119
char * _outpath
Definition: SiftGPU.h:141
int GetFeatureCountThreshold()
Definition: SiftGPU.cpp:1361
void SetVerboseBrief()
Definition: SiftGPU.h:182
int gpu_index
Definition: SiftGPU.h:122
virtual int GetFeatureNum()
Definition: SiftGPU.cpp:1312
matcher export
Definition: SiftGPU.h:269
virtual int _VerifyContextGL()
Definition: SiftMatch.cpp:575
SIFTMATCH_LANGUAGE
Definition: SiftGPU.h:271
@ SIFTMATCH_SAME_AS_SIFTGPU
Definition: SiftGPU.h:272
@ SIFTMATCH_GLSL
Definition: SiftGPU.h:273
@ SIFTMATCH_CUDA
Definition: SiftGPU.h:274
@ SIFTMATCH_CUDA_DEVICE0
Definition: SiftGPU.h:275
int CreateContextGL()
Definition: SiftGPU.h:291
virtual void SetLanguage(int gpu_language)
Definition: SiftMatch.cpp:620
virtual int GetMaxSift() const
Definition: SiftGPU.h:311
virtual void SetMaxSift(int max_sift)
Definition: SiftMatch.cpp:646
int VerifyContextGL()
Definition: SiftGPU.h:292
SiftMatchGPU(int max_sift=4096)
Definition: SiftMatch.cpp:613
virtual bool Allocate(int max_sift, int mbm)
Definition: SiftMatch.cpp:636
virtual int GetGuidedSiftMatch(int max_match, uint32_t match_buffer[][2], float *H, float *F, float distmax=0.7, float ratiomax=0.8, float hdistmax=32, float fdistmax=16, int mutual_best_match=1)
Definition: SiftMatch.cpp:676
virtual void SetDescriptors(int index, int num, const float *descriptors, int id=-1)
Definition: SiftMatch.cpp:666
virtual ~SiftMatchGPU()
Definition: SiftMatch.cpp:656
virtual void SetDeviceParam(int argc, char **argv)
Definition: SiftMatch.cpp:630
int gpu_index
Definition: SiftGPU.h:278
virtual int GetSiftMatch(int max_match, uint32_t match_buffer[][2], float distmax=0.7, float ratiomax=0.8, int mutual_best_match=1)
Definition: SiftMatch.cpp:691
void SetFeatureLocation(int index, const SiftGPU::SiftKeypoint *keys)
Definition: SiftGPU.h:336
int __max_sift
Definition: SiftGPU.h:285
virtual int _CreateContextGL()
Definition: SiftMatch.cpp:559
virtual void SetFeautreLocation(int index, const float *locations, int gap=0)
Definition: SiftMatch.cpp:671
float _sigman
Definition: SiftGPU.h:81
int _level_num
Definition: SiftGPU.h:86
float GetInitialSmoothSigma(int octave_min)
Definition: SiftGPU.cpp:415
int _level_ds
Definition: SiftGPU.h:91
float * _sigma
Definition: SiftGPU.h:75
int _level_min
Definition: SiftGPU.h:89
int _dog_level_num
Definition: SiftGPU.h:85
void ParseSiftParam()
Definition: SiftGPU.cpp:423
float _sigma_skip1
Definition: SiftGPU.h:77
float _sigma0
Definition: SiftGPU.h:80
int _sigma_num
Definition: SiftGPU.h:82
int _level_max
Definition: SiftGPU.h:90
float _dog_threshold
Definition: SiftGPU.h:93
float GetLevelSigma(int lev)
Definition: SiftGPU.cpp:1190
float _edge_threshold
Definition: SiftGPU.h:95
float _sigma_skip0
Definition: SiftGPU.h:76
CorePointDescSet * descriptors