SiftGPU.h
Go to the documentation of this file.
00001 
00002 //      File:           SiftGPU.h
00003 //      Author:         Changchang Wu
00004 //      Description :   interface for the SIFTGPU class.
00005 //                                      SiftGPU:        The SiftGPU Tool.  
00006 //                                      SiftGPUEX:      SiftGPU + viewer
00007 //                                      SiftParam:      Sift Parameters
00008 //                                      SiftMatchGPU: GPU SIFT Matcher;
00009 //
00010 //
00011 //      Copyright (c) 2007 University of North Carolina at Chapel Hill
00012 //      All Rights Reserved
00013 //
00014 //      Permission to use, copy, modify and distribute this software and its
00015 //      documentation for educational, research and non-profit purposes, without
00016 //      fee, and without a written agreement is hereby granted, provided that the
00017 //      above copyright notice and the following paragraph appear in all copies.
00018 //      
00019 //      The University of North Carolina at Chapel Hill make no representations
00020 //      about the suitability of this software for any purpose. It is provided
00021 //      'as is' without express or implied warranty. 
00022 //
00023 //      Please send BUG REPORTS to ccwu@cs.unc.edu
00024 //
00026 
00027 
00028 #ifndef GPU_SIFT_H
00029 #define GPU_SIFT_H
00030 
00031 #if  defined(_WIN32) 
00032         #ifdef SIFTGPU_DLL
00033                 #ifdef DLL_EXPORT
00034                         #define SIFTGPU_EXPORT __declspec(dllexport)
00035                 #else
00036                         #define SIFTGPU_EXPORT __declspec(dllimport)
00037                 #endif
00038         #else
00039                 #define SIFTGPU_EXPORT
00040         #endif
00041 
00042     #define SIFTGPU_EXPORT_EXTERN SIFTGPU_EXPORT
00043 
00044         #if _MSC_VER > 1000
00045                 #pragma once
00046         #endif
00047 #else
00048         #define SIFTGPU_EXPORT
00049     #define SIFTGPU_EXPORT_EXTERN extern "C"
00050 #endif
00051 
00053 //clss SiftParam
00054 //description: SIFT parameters
00056 class GlobalUtil;
00057 class SiftParam
00058 {
00059 public:
00060         float*          _sigma;
00061         float           _sigma_skip0; // 
00062         float           _sigma_skip1; //
00063         
00064         //sigma of the first level
00065         float           _sigma0;
00066         float           _sigman;
00067         int                     _sigma_num;
00068 
00069         //how many dog_level in an octave
00070         int                     _dog_level_num;
00071         int                     _level_num;
00072 
00073         //starting level in an octave
00074         int                     _level_min;
00075         int                     _level_max;
00076         int                     _level_ds;
00077         //dog threshold
00078         float           _dog_threshold;
00079         //edge elimination
00080         float           _edge_threshold;
00081         void             ParseSiftParam();
00082 public:
00083         float GetLevelSigma(int lev);
00084         float GetInitialSmoothSigma(int octave_min);
00085         SIFTGPU_EXPORT SiftParam();
00086 };
00087 
00088 class LiteWindow;
00089 class GLTexInput;
00090 class ShaderMan;
00091 class SiftPyramid;
00092 class ImageList;
00094 //class SIftGPU
00095 //description: Interface of SiftGPU lib
00097 class SiftGPU:public SiftParam
00098 {
00099 public:
00100         enum
00101         {
00102                 SIFTGPU_NOT_SUPPORTED = 0,
00103                 SIFTGPU_PARTIAL_SUPPORTED = 1, // detction works, but not orientation/descriptor
00104                 SIFTGPU_FULL_SUPPORTED = 2
00105         };
00106         typedef struct SiftKeypoint
00107         {
00108                 float x, y, s, o; //x, y, scale, orientation.
00109         }SiftKeypoint;
00110 protected: 
00111         //when more than one images are specified
00112         //_current indicates the active one
00113         int             _current;
00114         //_initialized indicates if the shaders and OpenGL/SIFT parameters are initialized
00115         //they are initialized only once for one SiftGPU inistance
00116         //that is, SIFT parameters will not be changed
00117         int             _initialized;
00118         //_image_loaded indicates if the current images are loaded
00119         int             _image_loaded;
00120         //the name of current input image
00121         char*   _imgpath;
00122         //_outpath containes the name of the output file
00123         char*   _outpath;
00124         //the list of image filenames
00125         ImageList *    _list;
00126         //the texture that holds loaded input image
00127         GLTexInput *   _texImage;
00128         //the SiftPyramid
00129         SiftPyramid *  _pyramid;
00130         //print out the command line options
00131         static void PrintUsage();
00132         //Initialize OpenGL and SIFT paremeters, and create the shaders accordingly
00133         void InitSiftGPU();
00134         //load the image list from a file
00135         void LoadImageList(char *imlist);
00136 public:
00137         //timing results for 10 steps
00138         float                       _timing[10];
00139     inline const char*  GetCurrentImagePath() {return _imgpath; }
00140 public:
00141         //set the image list for processing
00142         SIFTGPU_EXPORT virtual void SetImageList(int nimage, const char** filelist);
00143         //get the number of SIFT features in current image
00144         SIFTGPU_EXPORT virtual int      GetFeatureNum();
00145         //save the SIFT result as a ANSCII/BINARY file 
00146         SIFTGPU_EXPORT virtual void SaveSIFT(const char * szFileName);
00147         //Copy the SIFT result to two vectors
00148         SIFTGPU_EXPORT virtual void GetFeatureVector(SiftKeypoint * keys, float * descriptors);
00149         //Set keypoint list before running sift to get descriptors
00150         SIFTGPU_EXPORT virtual void SetKeypointList(int num, const SiftKeypoint * keys, int keys_have_orientation = 1);
00151         //Enable downloading results to CPU. 
00152         //create a new OpenGL context for processing
00153         //call VerifyContextGL instead if you want to crate openGL context yourself, or your are 
00154         //mixing mixing siftgpu with other openGL code
00155         SIFTGPU_EXPORT virtual int CreateContextGL();
00156         //verify the current opengl context..
00157         //(for example, you call wglmakecurrent yourself and verify the current context)
00158         SIFTGPU_EXPORT virtual int VerifyContextGL();
00159         //check if all siftgpu functions are supported
00160         SIFTGPU_EXPORT virtual int IsFullSupported();
00161         //set verbose mode
00162         SIFTGPU_EXPORT virtual void SetVerbose(int verbose = 4);
00163         //set SiftGPU to brief display mode, which is faster
00164         inline void SetVerboseBrief(){SetVerbose(2);};
00165         //parse SiftGPU parameters
00166         SIFTGPU_EXPORT virtual void ParseParam(int argc, char **argv);
00167         //run SIFT on a new image given filename
00168         SIFTGPU_EXPORT virtual int  RunSIFT(const char * imgpath);
00169         //run SIFT on an image in the image list given the file index
00170         SIFTGPU_EXPORT virtual int      RunSIFT(int index);
00171         //run SIFT on a new image given the pixel data and format/type;
00172         //gl_format (e.g. GL_LUMINANCE, GL_RGB) is the format of the pixel data
00173         //gl_type (e.g. GL_UNSIGNED_BYTE, GL_FLOAT) is the data type of the pixel data;
00174         //Check glTexImage2D(...format, type,...) for the accepted values
00175         //Using image data of GL_LUMINANCE + GL_UNSIGNED_BYTE can minimize transfer time
00176         SIFTGPU_EXPORT virtual int  RunSIFT(int width, int height,      const void * data, 
00177                                                                                 unsigned int gl_format, unsigned int gl_type);
00178         //run SIFT on current image (specified by arguments), or processing the current image again
00179         SIFTGPU_EXPORT virtual int  RunSIFT();
00180         //run SIFT with keypoints on current image again. 
00181         SIFTGPU_EXPORT virtual int  RunSIFT(int num, const SiftKeypoint * keys, int keys_have_orientation = 1);
00182         //constructor, the parameter np is ignored..
00183         SIFTGPU_EXPORT SiftGPU(int np = 1);
00184         //destructor
00185         SIFTGPU_EXPORT virtual ~SiftGPU();
00186         //set the active pyramid...dropped function
00187     SIFTGPU_EXPORT virtual void SetActivePyramid(int index) {}
00188         //retrieve the number of images in the image list
00189         SIFTGPU_EXPORT virtual int GetImageCount();
00190         //set parameter GlobalUtil::_ForceTightPyramid
00191         SIFTGPU_EXPORT virtual void SetTightPyramid(int tight = 1);
00192         //allocate pyramid for a given size of image
00193         SIFTGPU_EXPORT virtual int AllocatePyramid(int width, int height);
00194         //none of the texture in processing can be larger
00195         //automatic down-sample is used if necessary. 
00196         SIFTGPU_EXPORT virtual void SetMaxDimension(int sz);
00198 public:
00199         //overload the new operator because delete operator is virtual
00200         //and it is operating on the heap inside the dll (due to the 
00201         //compiler setting of /MT and /MTd). Without the overloaded operator
00202         //deleting a SiftGPU object will cause a heap corruption in the 
00203         //static link case (but not for the runtime dll loading).
00204         SIFTGPU_EXPORT void* operator new (size_t size); 
00205 };
00206 
00207 
00208 
00210 //class SIftGPUEX
00211 //description: adds some visualization functions to the interface of SiftGPU
00213 
00214 class SiftGPUEX:public SiftGPU
00215 {
00216         //view mode
00217         int     _view;
00218         //sub view mode
00219         int _sub_view;
00220         //whether display a debug view
00221         int _view_debug;
00222         //colors for SIFT feature display
00223         enum{COLOR_NUM = 36};
00224         float _colors[COLOR_NUM*3];
00225         //display functions
00226         void DisplayInput();    //display gray level image of input image       
00227         void DisplayDebug();    //display debug view
00228         void DisplayFeatureBox(int i);  //display SIFT features
00229         void DisplayLevel(void (*UseDisplayShader)(), int i);           //display one level image
00230         void DisplayOctave(void (*UseDisplayShader)(), int i);          //display all images in one octave
00231         //display different content of Pyramid by specifying different data and display shader
00232         //the first nskip1 levels and the last nskip2 levels are skiped in display
00233         void DisplayPyramid( void (*UseDisplayShader)(), int dataName, int nskip1 = 0, int nskip2 = 0);
00234         //use HSVtoRGB to generate random colors
00235         static void HSVtoRGB(float hsv[3],float rgb[3]);
00236 
00237 public:
00238         SIFTGPU_EXPORT SiftGPUEX();
00239         //change view mode
00240         SIFTGPU_EXPORT void SetView(int view, int sub_view, char * title);
00241         //display current view
00242         SIFTGPU_EXPORT void DisplaySIFT();
00243         //toggle debug mode on/off
00244         SIFTGPU_EXPORT void ToggleDisplayDebug();
00245         //randomize the display colors
00246         SIFTGPU_EXPORT void RandomizeColor();
00247         //retrieve the size of current input image
00248         SIFTGPU_EXPORT void GetImageDimension(int &w, int&h);
00249         //get the location of the window specified by user
00250         SIFTGPU_EXPORT void GetInitWindowPotition(int& x, int& y);
00251 };
00252 
00254 //This is a gpu-based sift match implementation. 
00255 class SiftMatchGPU
00256 {
00257 public:
00258         enum SIFTMATCH_LANGUAGE {
00259                 SIFTMATCH_SAME_AS_SIFTGPU = 0, //when siftgpu already initialized.
00260                 SIFTMATCH_GLSL = 2,
00261                 SIFTMATCH_CUDA = 3,
00262         SIFTMATCH_CUDA_DEVICE0 = 3 //to use device i, use SIFTMATCH_CUDA_DEVICE0 + i
00263         };
00264 private:
00265         int                             __max_sift;
00266         int                             __language;
00267         SiftMatchGPU *  __matcher;
00268         virtual void   InitSiftMatch(){}
00269 protected:
00270         //move the two functions here for derived class
00271         SIFTGPU_EXPORT virtual int  _CreateContextGL();
00272         SIFTGPU_EXPORT virtual int  _VerifyContextGL();
00273 public:
00274         //OpenGL Context creation/verification, initialization is done automatically inside
00275         inline int  CreateContextGL() {return _CreateContextGL();}
00276         inline int  VerifyContextGL() {return _VerifyContextGL();}
00277 
00278         //Consructor, the argument specifies the maximum number of features to match
00279         SIFTGPU_EXPORT SiftMatchGPU(int max_sift = 4096);
00280 
00281         //change gpu_language, check the enumerants in SIFTMATCH_LANGUAGE.
00282         SIFTGPU_EXPORT virtual void SetLanguage(int gpu_language);
00283 
00284     //after calling SetLanguage, you can call SetDeviceParam to select GPU
00285     //-winpos, -display, -cuda [device_id] 
00286     //This is only used when you call CreateContextGL..
00287         //This function doesn't change the language. 
00288     SIFTGPU_EXPORT virtual void SetDeviceParam(int argc, char**argv);
00289 
00290         //change the maximum of features to match whenever you want
00291         SIFTGPU_EXPORT virtual void SetMaxSift(int max_sift);
00292         //desctructor
00293         SIFTGPU_EXPORT virtual ~SiftMatchGPU();
00294 
00295         //Specifiy descriptors to match, index = [0/1] for two features sets respectively
00296         //Option1, use float descriptors, and they be already normalized to 1.0
00297         SIFTGPU_EXPORT virtual void SetDescriptors(int index, int num, const float* descriptors, int id  = -1);
00298         //Option 2 unsigned char descriptors. They must be already normalized to 512
00299         SIFTGPU_EXPORT virtual void SetDescriptors(int index, int num, const unsigned char * descriptors, int id = -1);
00300 
00301         //match two sets of features, the function RETURNS the number of matches.
00302         //Given two normalized descriptor d1,d2, the distance here is acos(d1 *d2);
00303         SIFTGPU_EXPORT virtual int  GetSiftMatch(
00304                                 int max_match,  // the length of the match_buffer.
00305                                 int match_buffer[][2], //buffer to receive the matched feature indices
00306                                 float distmax = 0.7,    //maximum distance of sift descriptor
00307                                 float ratiomax = 0.8,   //maximum distance ratio
00308                                 int mutual_best_match = 1); //mutual best match or one way
00309 
00310         //two functions for guded matching, two constraints can be used 
00311         //one homography and one fundamental matrix, the use is as follows
00312         //1. for each image, first call SetDescriptor then call SetFeatureLocation
00313         //2. Call GetGuidedSiftMatch
00314         //input feature location is a vector of [float x, float y, float skip[gap]]
00315         SIFTGPU_EXPORT virtual void SetFeautreLocation(int index, const float* locations, int gap = 0);
00316         inline void SetFeatureLocation(int index, const SiftGPU::SiftKeypoint * keys)
00317         {
00318                 SetFeautreLocation(index, (const float*) keys, 2);
00319         }
00320 
00321         //use a guiding Homography H and a guiding Fundamental Matrix F to compute feature matches
00322         //the function returns the number of matches.
00323         SIFTGPU_EXPORT virtual int  GetGuidedSiftMatch(
00324                                         int max_match, int match_buffer[][2], //buffer to recieve 
00325                                         float H[3][3],                  //homography matrix,  (Set NULL to skip)
00326                                         float F[3][3],                  //fundamental matrix, (Set NULL to skip)
00327                                         float distmax = 0.7,    //maximum distance of sift descriptor
00328                                         float ratiomax = 0.8,   //maximum distance ratio
00329                                         float hdistmax = 32,    //threshold for |H * x1 - x2|_1 
00330                                         float fdistmax = 16,    //threshold for sampson error of x2'FX1
00331                                         int mutual_best_match = 1); //mutual best or one way
00332 
00333 public:
00334         //overload the new operator, the same reason as SiftGPU above
00335         SIFTGPU_EXPORT void* operator new (size_t size);
00336 };
00337 
00338 typedef SiftGPU::SiftKeypoint SiftKeypoint;
00339 
00340 //Two exported global functions used to create SiftGPU and SiftMatchGPU
00341 SIFTGPU_EXPORT_EXTERN SiftGPU * CreateNewSiftGPU(int np =1);
00342 SIFTGPU_EXPORT_EXTERN SiftMatchGPU* CreateNewSiftMatchGPU(int max_sift = 4096);
00343 
00344 
00346 class ComboSiftGPU: public SiftGPU, public SiftMatchGPU 
00347 {
00348 public:
00350         SIFTGPU_EXPORT void* operator new (size_t size); 
00351 };
00352 SIFTGPU_EXPORT_EXTERN ComboSiftGPU* CreateComboSiftGPU(); 
00353 
00355 //Multi-process mode and remote mode
00356 SIFTGPU_EXPORT_EXTERN ComboSiftGPU* CreateRemoteSiftGPU(int port = 7777, char* remote_server = NULL);
00357 //Run SiftGPU computation on a remote computer/process/thread
00358 //if( remote_server == NULL) 
00359 //                      a local server is created in a different process and connected
00360 //                      multiple-GPU can be used by creating multiple instances
00361 //                      GPU selection done through SiftGPU::ParseParam function
00362 //otherwise, 
00363 //                      Assumes the existenc of a remote server and connects to it
00364 //                      GPU selection skipped if already done on the server-end
00365 //                      RUN server: server_siftgpu -server port [siftgpu_param]
00366 //example:
00367 //      ComboSiftGPU * combo = CreateRemoteSiftGPU(7777, "my.gpuserver.com");
00368 //      SiftGPU* siftgpu = combo, SiftMatchGPU * matcher = combo;
00369 //  siftgpu->ParseParam... siftgpu->CreateContextGL..
00370 //  matcher->SetLanguage...matcher->VerifyContextGL...
00371 //  // GPU-selection is done throught siftgpu->ParseParam, 
00372 //  // it doesn't really initialize SiftGPU untill you call CreateContextGL/VerifyContextGL
00373 //  delete combo;
00374 
00376 //two internally used function.
00377 SIFTGPU_EXPORT int  CreateLiteWindow(LiteWindow* window);
00378 SIFTGPU_EXPORT void RunServerLoop(int port, int argc, char** argv);
00379 #endif 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines


rgbd_registration
Author(s): Ross Kidson
autogenerated on Thu May 23 2013 15:36:53