himrep
SiftGPU_Extractor.cpp
1 
2 #include <cstdio>
3 #include "SiftGPU_Extractor.h"
4 
5 
6  SiftGPU_Extractor::SiftGPU_Extractor()
7  {
8  SiftGPU* (*pCreateNewSiftGPU)(int) = NULL;
9  SiftMatchGPU* (*pCreateNewSiftMatchGPU)(int) = NULL;
10 
11  char * pPath;
12  pPath = getenv ("SIFTGPU_DIR");
13  //printf ("\n\nThe current path is: %s\n\n",pPath);
14 
15 
16  #ifdef SIFTGPU_DLL_RUNTIME
17  string str = pPath;
18  #ifdef _WIN32
19  str.append("/bin/SIFTGPU.dll");
20  #ifdef _DEBUG
21  HMODULE hsiftgpu = LoadLibrary(str.c_str());
22  #else
23  HMODULE hsiftgpu = LoadLibrary(str.c_str());
24  #endif
25  #else
26  str.append("/bin/libsiftgpu.so");
27  void * hsiftgpu = dlopen(str.c_str(), RTLD_LAZY);
28  #endif
29 
30  #ifdef REMOTE_SIFTGPU
31  ComboSiftGPU* (*pCreateRemoteSiftGPU) (int, char*) = NULL;
32  pCreateRemoteSiftGPU = (ComboSiftGPU* (*) (int, char*)) GET_MYPROC(hsiftgpu, "CreateRemoteSiftGPU");
33  combo = pCreateRemoteSiftGPU(REMOTE_SERVER_PORT, REMOTE_SERVER);
34  sift = combo;
35  matcher = combo;
36  #else
37  //SiftGPU* (*pCreateNewSiftGPU)(int) = NULL;
38  //SiftMatchGPU* (*pCreateNewSiftMatchGPU)(int) = NULL;
39  pCreateNewSiftGPU = (SiftGPU* (*) (int)) GET_MYPROC(hsiftgpu, "CreateNewSiftGPU");
40  pCreateNewSiftMatchGPU = (SiftMatchGPU* (*)(int)) GET_MYPROC(hsiftgpu, "CreateNewSiftMatchGPU");
41  sift = pCreateNewSiftGPU(1);
42  matcher = pCreateNewSiftMatchGPU(4096);
43  #endif
44 
45  #elif defined(REMOTE_SIFTGPU)
46  combo = CreateRemoteSiftGPU(REMOTE_SERVER_PORT, REMOTE_SERVER);
47  sift = combo;
48  matcher = combo;
49  #else
50  //this will use overloaded new operators
51  sift = new SiftGPU;
52  matcher = new SiftMatchGPU(4096);
53  #endif
54 
55  char * argv[] = {(char*)"-fo", (char*)"-1", (char*) "-v",(char*) "1", (char*)"-winpos",(char*)"-maxd", (char*)"1024"};
56  int argc = sizeof(argv)/sizeof(char*);
57 
58 
59  sift->ParseParam(argc, argv);
60  //verbose on sift to remove unwanted printouts (put 1 for data)
61  sift->SetVerbose(0);
63 
64  if(sift->CreateContextGL() != SiftGPU::SIFTGPU_FULL_SUPPORTED)
65  fprintf(stdout,"boh, some error\n");
66 
67  matcher->VerifyContextGL();
68  }
69 
70 
71 
72 
73 
74  bool SiftGPU_Extractor::setDenseGrid(int width, int height, int step, int scale)
75  {
76  int widthNodes=(int)(width-step)/step;
77  int heightNodes=(int)(height-step)/step;
78 
79  keypoints_grid.resize(widthNodes*heightNodes);
80  int idx=0;
81  for(int row=step; row<=height-step; row+=step)
82  {
83  for(int col=step; col<=width-step; col+=step)
84  {
85  //int idx=(row/step)*widthNodes + col/step;
86  keypoints_grid[idx].x=col;
87  keypoints_grid[idx].y=row;
88  keypoints_grid[idx].s=scale;
89  keypoints_grid[idx].o=0.0f;
90  idx++;
91  }
92  }
93 
94  return true;
95  }
96 
97 
98 
99  bool SiftGPU_Extractor::setDenseGrid(IplImage *img, int step, int scale)
100  {
101  setDenseGrid(img->width,img->height,step,scale);
102  }
103 
104 
105 
106 
107  bool SiftGPU_Extractor::extractSift(IplImage *img,vector<SiftGPU::SiftKeypoint> *keypoints, vector<float> *descriptors, int feature_size)
108  {
109  if(img->nChannels==1)
110  sift->RunSIFT(img->width,img->height,img->imageData,GL_LUMINANCE,GL_UNSIGNED_BYTE );
111  else if(img->nChannels==3)
112  sift->RunSIFT(img->width,img->height,img->imageData,GL_RGB,GL_UNSIGNED_BYTE );
113  else
114  return false;
115 
116  if(feature_size!=128)
117  fprintf(stdout,"Error! wrong feature size!\n");
118 
119  int feature_num=sift->GetFeatureNum();
120 
121  if(feature_num<=0)
122  return false;
123 
124  if(keypoints!=NULL && descriptors!=NULL)
125  {
126  keypoints->resize(feature_num);
127  descriptors->resize(feature_size*feature_num);
128  sift->GetFeatureVector(&(keypoints->at(0)),&(descriptors->at(0)));
129  }
130 
131  return true;
132  }
133 
134 
135 
136  bool SiftGPU_Extractor::extractDenseSift(IplImage *img,vector<SiftGPU::SiftKeypoint> *keypoints, vector<float> *descriptors, int feature_size)
137  {
138  if(keypoints_grid.size()==0)
139  return false;
140 
141  sift->SetKeypointList(keypoints_grid.size(),&keypoints_grid[0],0);
142  return this->extractSift(img,keypoints,descriptors,feature_size);
143  }
144 
145 
146  int SiftGPU_Extractor::getFeatureNum()
147  {
148  return sift->GetFeatureNum();
149  }
150 
151 
152  bool SiftGPU_Extractor::getFeatureVector(vector<SiftGPU::SiftKeypoint> *keypoints, vector<float> *descriptors, int feature_size)
153  {
154  if(feature_size!=128)
155  fprintf(stdout,"Error! wrong feature size!\n");
156 
157  int feature_num=sift->GetFeatureNum();
158 
159  keypoints->resize(feature_num);
160  descriptors->resize(feature_size*feature_num);
161  sift->GetFeatureVector(&(keypoints->at(0)),&(descriptors->at(0)));
162  return true;
163  }
164