Project

General

Profile

Statistics
| Branch: | Revision:

root / rgbdslam / external / siftgpu / src / SiftGPU / SiftGPU.h @ 9240aaa3

History | View | Annotate | Download (14.6 KB)

1 9240aaa3 Alex
////////////////////////////////////////////////////////////////////////////
2
//        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
//
25
////////////////////////////////////////////////////////////////////////////
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
42
    #define SIFTGPU_EXPORT_EXTERN SIFTGPU_EXPORT
43
44
        #if _MSC_VER > 1000
45
                #pragma once
46
        #endif
47
#else
48
        #define SIFTGPU_EXPORT
49
    #define SIFTGPU_EXPORT_EXTERN extern "C"
50
#endif
51
52
///////////////////////////////////////////////////////////////////
53
//clss SiftParam
54
//description: SIFT parameters
55
////////////////////////////////////////////////////////////////////
56
class GlobalUtil;
57
class SiftParam
58
{
59
public:
60
        float*                _sigma;
61
        float                _sigma_skip0; // 
62
        float                _sigma_skip1; //
63
        
64
        //sigma of the first level
65
        float                _sigma0;
66
        float                _sigman;
67
        int                        _sigma_num;
68
69
        //how many dog_level in an octave
70
        int                        _dog_level_num;
71
        int                        _level_num;
72
73
        //starting level in an octave
74
        int                        _level_min;
75
        int                        _level_max;
76
        int                        _level_ds;
77
        //dog threshold
78
        float                _dog_threshold;
79
        //edge elimination
80
        float                _edge_threshold;
81
        void                 ParseSiftParam();
82
public:
83
        float GetLevelSigma(int lev);
84
        float GetInitialSmoothSigma(int octave_min);
85
        SIFTGPU_EXPORT SiftParam();
86
};
87
88
class LiteWindow;
89
class GLTexInput;
90
class ShaderMan;
91
class SiftPyramid;
92
class ImageList;
93
////////////////////////////////////////////////////////////////
94
//class SIftGPU
95
//description: Interface of SiftGPU lib
96
////////////////////////////////////////////////////////////////
97
class SiftGPU:public SiftParam
98
{
99
public:
100
        enum
101
        {
102
                SIFTGPU_NOT_SUPPORTED = 0,
103
                SIFTGPU_PARTIAL_SUPPORTED = 1, // detction works, but not orientation/descriptor
104
                SIFTGPU_FULL_SUPPORTED = 2
105
        };
106
        typedef struct SiftKeypoint
107
        {
108
                float x, y, s, o; //x, y, scale, orientation.
109
        }SiftKeypoint;
110
protected: 
111
        //when more than one images are specified
112
        //_current indicates the active one
113
        int                _current;
114
        //_initialized indicates if the shaders and OpenGL/SIFT parameters are initialized
115
        //they are initialized only once for one SiftGPU inistance
116
        //that is, SIFT parameters will not be changed
117
        int                _initialized;
118
        //_image_loaded indicates if the current images are loaded
119
        int                _image_loaded;
120
        //the name of current input image
121
        char*        _imgpath;
122
        //_outpath containes the name of the output file
123
        char*        _outpath;
124
        //the list of image filenames
125
        ImageList *    _list;
126
        //the texture that holds loaded input image
127
        GLTexInput *   _texImage;
128
        //the SiftPyramid
129
        SiftPyramid *  _pyramid;
130
        //print out the command line options
131
        static void PrintUsage();
132
        //Initialize OpenGL and SIFT paremeters, and create the shaders accordingly
133
        void InitSiftGPU();
134
        //load the image list from a file
135
        void LoadImageList(char *imlist);
136
public:
137
        //timing results for 10 steps
138
        float                            _timing[10];
139
    inline const char*  GetCurrentImagePath() {return _imgpath; }
140
public:
141
        //set the image list for processing
142
        SIFTGPU_EXPORT virtual void SetImageList(int nimage, const char** filelist);
143
        //get the number of SIFT features in current image
144
        SIFTGPU_EXPORT virtual int        GetFeatureNum();
145
        //save the SIFT result as a ANSCII/BINARY file 
146
        SIFTGPU_EXPORT virtual void SaveSIFT(const char * szFileName);
147
        //Copy the SIFT result to two vectors
148
        SIFTGPU_EXPORT virtual void GetFeatureVector(SiftKeypoint * keys, float * descriptors);
149
        //Set keypoint list before running sift to get descriptors
150
        SIFTGPU_EXPORT virtual void SetKeypointList(int num, const SiftKeypoint * keys, int keys_have_orientation = 1);
151
        //Enable downloading results to CPU. 
152
        //create a new OpenGL context for processing
153
        //call VerifyContextGL instead if you want to crate openGL context yourself, or your are 
154
        //mixing mixing siftgpu with other openGL code
155
        SIFTGPU_EXPORT virtual int CreateContextGL();
156
        //verify the current opengl context..
157
        //(for example, you call wglmakecurrent yourself and verify the current context)
158
        SIFTGPU_EXPORT virtual int VerifyContextGL();
159
        //check if all siftgpu functions are supported
160
        SIFTGPU_EXPORT virtual int IsFullSupported();
161
        //set verbose mode
162
        SIFTGPU_EXPORT virtual void SetVerbose(int verbose = 4);
163
        //set SiftGPU to brief display mode, which is faster
164
        inline void SetVerboseBrief(){SetVerbose(2);};
165
        //parse SiftGPU parameters
166
        SIFTGPU_EXPORT virtual void ParseParam(int argc, char **argv);
167
        //run SIFT on a new image given filename
168
        SIFTGPU_EXPORT virtual int  RunSIFT(const char * imgpath);
169
        //run SIFT on an image in the image list given the file index
170
        SIFTGPU_EXPORT virtual int        RunSIFT(int index);
171
        //run SIFT on a new image given the pixel data and format/type;
172
        //gl_format (e.g. GL_LUMINANCE, GL_RGB) is the format of the pixel data
173
        //gl_type (e.g. GL_UNSIGNED_BYTE, GL_FLOAT) is the data type of the pixel data;
174
        //Check glTexImage2D(...format, type,...) for the accepted values
175
        //Using image data of GL_LUMINANCE + GL_UNSIGNED_BYTE can minimize transfer time
176
        SIFTGPU_EXPORT virtual int  RunSIFT(int width, int height,        const void * data, 
177
                                                                                unsigned int gl_format, unsigned int gl_type);
178
        //run SIFT on current image (specified by arguments), or processing the current image again
179
        SIFTGPU_EXPORT virtual int  RunSIFT();
180
        //run SIFT with keypoints on current image again. 
181
        SIFTGPU_EXPORT virtual int  RunSIFT(int num, const SiftKeypoint * keys, int keys_have_orientation = 1);
182
        //constructor, the parameter np is ignored..
183
        SIFTGPU_EXPORT SiftGPU(int np = 1);
184
        //destructor
185
        SIFTGPU_EXPORT virtual ~SiftGPU();
186
        //set the active pyramid...dropped function
187
    SIFTGPU_EXPORT virtual void SetActivePyramid(int index) {}
188
        //retrieve the number of images in the image list
189
        SIFTGPU_EXPORT virtual int GetImageCount();
190
        //set parameter GlobalUtil::_ForceTightPyramid
191
        SIFTGPU_EXPORT virtual void SetTightPyramid(int tight = 1);
192
        //allocate pyramid for a given size of image
193
        SIFTGPU_EXPORT virtual int AllocatePyramid(int width, int height);
194
        //none of the texture in processing can be larger
195
        //automatic down-sample is used if necessary. 
196
        SIFTGPU_EXPORT virtual void SetMaxDimension(int sz);
197
        ///
198
public:
199
        //overload the new operator because delete operator is virtual
200
        //and it is operating on the heap inside the dll (due to the 
201
        //compiler setting of /MT and /MTd). Without the overloaded operator
202
        //deleting a SiftGPU object will cause a heap corruption in the 
203
        //static link case (but not for the runtime dll loading).
204
        SIFTGPU_EXPORT void* operator new (size_t size); 
205
};
206
207
208
209
////////////////////////////////////////////////////////////////
210
//class SIftGPUEX
211
//description: adds some visualization functions to the interface of SiftGPU
212
////////////////////////////////////////////////////////////////
213
214
class SiftGPUEX:public SiftGPU
215
{
216
        //view mode
217
        int        _view;
218
        //sub view mode
219
        int _sub_view;
220
        //whether display a debug view
221
        int _view_debug;
222
        //colors for SIFT feature display
223
        enum{COLOR_NUM = 36};
224
        float _colors[COLOR_NUM*3];
225
        //display functions
226
        void DisplayInput();        //display gray level image of input image        
227
        void DisplayDebug();        //display debug view
228
        void DisplayFeatureBox(int i);        //display SIFT features
229
        void DisplayLevel(void (*UseDisplayShader)(), int i);                //display one level image
230
        void DisplayOctave(void (*UseDisplayShader)(), int i);                //display all images in one octave
231
        //display different content of Pyramid by specifying different data and display shader
232
        //the first nskip1 levels and the last nskip2 levels are skiped in display
233
        void DisplayPyramid( void (*UseDisplayShader)(), int dataName, int nskip1 = 0, int nskip2 = 0);
234
        //use HSVtoRGB to generate random colors
235
        static void HSVtoRGB(float hsv[3],float rgb[3]);
236
237
public:
238
        SIFTGPU_EXPORT SiftGPUEX();
239
        //change view mode
240
        SIFTGPU_EXPORT void SetView(int view, int sub_view, char * title);
241
        //display current view
242
        SIFTGPU_EXPORT void DisplaySIFT();
243
        //toggle debug mode on/off
244
        SIFTGPU_EXPORT void ToggleDisplayDebug();
245
        //randomize the display colors
246
        SIFTGPU_EXPORT void RandomizeColor();
247
        //retrieve the size of current input image
248
        SIFTGPU_EXPORT void GetImageDimension(int &w, int&h);
249
        //get the location of the window specified by user
250
        SIFTGPU_EXPORT void GetInitWindowPotition(int& x, int& y);
251
};
252
253
///matcher export
254
//This is a gpu-based sift match implementation. 
255
class SiftMatchGPU
256
{
257
public:
258
        enum SIFTMATCH_LANGUAGE        {
259
                SIFTMATCH_SAME_AS_SIFTGPU = 0, //when siftgpu already initialized.
260
                SIFTMATCH_GLSL = 2,
261
                SIFTMATCH_CUDA = 3,
262
        SIFTMATCH_CUDA_DEVICE0 = 3 //to use device i, use SIFTMATCH_CUDA_DEVICE0 + i
263
        };
264
private:
265
        int                                __max_sift;
266
        int                                __language;
267
        SiftMatchGPU *        __matcher;
268
        virtual void   InitSiftMatch(){}
269
protected:
270
        //move the two functions here for derived class
271
        SIFTGPU_EXPORT virtual int  _CreateContextGL();
272
        SIFTGPU_EXPORT virtual int  _VerifyContextGL();
273
public:
274
        //OpenGL Context creation/verification, initialization is done automatically inside
275
        inline int  CreateContextGL() {return _CreateContextGL();}
276
        inline int  VerifyContextGL() {return _VerifyContextGL();}
277
278
        //Consructor, the argument specifies the maximum number of features to match
279
        SIFTGPU_EXPORT SiftMatchGPU(int max_sift = 4096);
280
281
        //change gpu_language, check the enumerants in SIFTMATCH_LANGUAGE.
282
        SIFTGPU_EXPORT virtual void SetLanguage(int gpu_language);
283
284
    //after calling SetLanguage, you can call SetDeviceParam to select GPU
285
    //-winpos, -display, -cuda [device_id] 
286
    //This is only used when you call CreateContextGL..
287
        //This function doesn't change the language. 
288
    SIFTGPU_EXPORT virtual void SetDeviceParam(int argc, char**argv);
289
290
        //change the maximum of features to match whenever you want
291
        SIFTGPU_EXPORT virtual void SetMaxSift(int max_sift);
292
        //desctructor
293
        SIFTGPU_EXPORT virtual ~SiftMatchGPU();
294
295
        //Specifiy descriptors to match, index = [0/1] for two features sets respectively
296
        //Option1, use float descriptors, and they be already normalized to 1.0
297
        SIFTGPU_EXPORT virtual void SetDescriptors(int index, int num, const float* descriptors, int id  = -1);
298
        //Option 2 unsigned char descriptors. They must be already normalized to 512
299
        SIFTGPU_EXPORT virtual void SetDescriptors(int index, int num, const unsigned char * descriptors, int id = -1);
300
301
        //match two sets of features, the function RETURNS the number of matches.
302
        //Given two normalized descriptor d1,d2, the distance here is acos(d1 *d2);
303
        SIFTGPU_EXPORT virtual int  GetSiftMatch(
304
                                int max_match,        // the length of the match_buffer.
305
                                int match_buffer[][2], //buffer to receive the matched feature indices
306
                                float distmax = 0.7,        //maximum distance of sift descriptor
307
                                float ratiomax = 0.8,        //maximum distance ratio
308
                                int mutual_best_match = 1); //mutual best match or one way
309
310
        //two functions for guded matching, two constraints can be used 
311
        //one homography and one fundamental matrix, the use is as follows
312
        //1. for each image, first call SetDescriptor then call SetFeatureLocation
313
        //2. Call GetGuidedSiftMatch
314
        //input feature location is a vector of [float x, float y, float skip[gap]]
315
        SIFTGPU_EXPORT virtual void SetFeautreLocation(int index, const float* locations, int gap = 0);
316
        inline void SetFeatureLocation(int index, const SiftGPU::SiftKeypoint * keys)
317
        {
318
                SetFeautreLocation(index, (const float*) keys, 2);
319
        }
320
321
        //use a guiding Homography H and a guiding Fundamental Matrix F to compute feature matches
322
        //the function returns the number of matches.
323
        SIFTGPU_EXPORT virtual int  GetGuidedSiftMatch(
324
                                        int max_match, int match_buffer[][2], //buffer to recieve 
325
                                        float H[3][3],                        //homography matrix,  (Set NULL to skip)
326
                                        float F[3][3],                        //fundamental matrix, (Set NULL to skip)
327
                                        float distmax = 0.7,        //maximum distance of sift descriptor
328
                                        float ratiomax = 0.8,   //maximum distance ratio
329
                                        float hdistmax = 32,    //threshold for |H * x1 - x2|_1 
330
                                        float fdistmax = 16,    //threshold for sampson error of x2'FX1
331
                                        int mutual_best_match = 1); //mutual best or one way
332
333
public:
334
        //overload the new operator, the same reason as SiftGPU above
335
        SIFTGPU_EXPORT void* operator new (size_t size);
336
};
337
338
typedef SiftGPU::SiftKeypoint SiftKeypoint;
339
340
//Two exported global functions used to create SiftGPU and SiftMatchGPU
341
SIFTGPU_EXPORT_EXTERN SiftGPU * CreateNewSiftGPU(int np =1);
342
SIFTGPU_EXPORT_EXTERN SiftMatchGPU* CreateNewSiftMatchGPU(int max_sift = 4096);
343
344
345
////////////////////////////////////////////////////////////////////////////
346
class ComboSiftGPU: public SiftGPU, public SiftMatchGPU 
347
{
348
public:
349
        ///////////////////////////////////////////////
350
        SIFTGPU_EXPORT void* operator new (size_t size); 
351
};
352
SIFTGPU_EXPORT_EXTERN ComboSiftGPU* CreateComboSiftGPU(); 
353
354
/////////////////////////////////////////////////////////////////////////////////////////////
355
//Multi-process mode and remote mode
356
SIFTGPU_EXPORT_EXTERN ComboSiftGPU* CreateRemoteSiftGPU(int port = 7777, char* remote_server = NULL);
357
//Run SiftGPU computation on a remote computer/process/thread
358
//if( remote_server == NULL) 
359
//                        a local server is created in a different process and connected
360
//                        multiple-GPU can be used by creating multiple instances
361
//                        GPU selection done through SiftGPU::ParseParam function
362
//otherwise, 
363
//                        Assumes the existenc of a remote server and connects to it
364
//                        GPU selection skipped if already done on the server-end
365
//                        RUN server: server_siftgpu -server port [siftgpu_param]
366
//example:
367
//        ComboSiftGPU * combo = CreateRemoteSiftGPU(7777, "my.gpuserver.com");
368
//        SiftGPU* siftgpu = combo, SiftMatchGPU * matcher = combo;
369
//  siftgpu->ParseParam... siftgpu->CreateContextGL..
370
//  matcher->SetLanguage...matcher->VerifyContextGL...
371
//  // GPU-selection is done throught siftgpu->ParseParam, 
372
//  // it doesn't really initialize SiftGPU untill you call CreateContextGL/VerifyContextGL
373
//  delete combo;
374
375
////////////////////////////////////////////////////////////////////////
376
//two internally used function.
377
SIFTGPU_EXPORT int  CreateLiteWindow(LiteWindow* window);
378
SIFTGPU_EXPORT void RunServerLoop(int port, int argc, char** argv);
379
#endif