-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSiftGPU.cpp
More file actions
executable file
·289 lines (231 loc) · 9.55 KB
/
SiftGPU.cpp
File metadata and controls
executable file
·289 lines (231 loc) · 9.55 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
#include <iostream>
#include <vector>
#include <algorithm>
#include "GlobalUtil.h"
#include "SiftGPU.h"
#include "SiftPyramid.h"
#include "ProgramCU.h"
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
SiftGPU::SiftGPU()
{
_initialized = 0;
_image_loaded = 0;
_current = 0;
_pyramid = NULL;
}
SiftGPU::~SiftGPU()
{
if (_pyramid) delete _pyramid;
}
void SiftGPU::InitSiftGPU()
{
if (_initialized) return; //如果已经初始化了,则直接返回
//Parse sift parameters
ParseSiftParam();
_pyramid = new SiftPyramid(*this);
if ((GlobalUtil::_InitPyramidWidth & 0xfffffffc) != GlobalUtil::_InitPyramidWidth) { //确保提取的图像宽度是4的倍数
std::cout << "ERROR: image width must be a multiple of 4" << std::endl;
return;
}
if (GlobalUtil::_InitPyramidWidth > 0 && GlobalUtil::_InitPyramidHeight > 0)
{
_pyramid->InitPyramid(GlobalUtil::_InitPyramidWidth, GlobalUtil::_InitPyramidHeight);
}
_initialized = 1; //标示已经初始化
}
int SiftGPU::RunSIFT(float* d_colorData, const float* d_depthData)
{
if (!_initialized) return 0;
if (d_colorData != NULL)
{
if (!_pyramid->isAllocated()) return 0;
//process the image
_pyramid->RunSIFT(d_colorData, d_depthData);
return 1;
}
return 0;
}
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;
std::vector<float> sigmas;
sigmas.push_back(GetInitialSmoothSigma(GlobalUtil::_octave_min_default));
for (int i = _level_min + 1; i <= _level_max; i++) {
sigmas.push_back(_sigma[i]);
}
ProgramCU::InitFilterKernels(sigmas, m_filterWidths);
}
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 (defaut : 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 specifiy 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 (defaut: 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 (defualt : 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::SetParams(unsigned int siftWidth, unsigned int siftHeight, bool enableTiming, unsigned int featureCountThreshold, float siftDepthMin, float siftDepthMax)
{
GlobalUtil::_SiftDepthMin = siftDepthMin;
GlobalUtil::_SiftDepthMax = siftDepthMax;
//!!!TODO TRY THIS
//GlobalUtil::_LoweOrigin = 1;
// soft limit for num features detected
GlobalUtil::_FeatureCountThreshold = (int)featureCountThreshold;
// pyramid size to allocate
GlobalUtil::_InitPyramidWidth = siftWidth;
GlobalUtil::_InitPyramidHeight = siftHeight;
// don't use subpixel localization
GlobalUtil::_SubpixelLocalization = 0;
// first octave to detect = 0;
GlobalUtil::_octave_min_default = 0;
// use 4 octaves
GlobalUtil::_octave_num_default = 4;
if (GlobalUtil::_MaxOrientation < 2) {
std::cout << "MAX ORIENTATION != 2 not supported" << std::endl;
while (1);
}
if (enableTiming) GlobalUtil::_EnableDetailedTimings = true;
}
float SiftParam::GetLevelSigma(int lev)
{
return _sigma0 * powf(2.0f, float(lev) / float(_dog_level_num)); //bug fix 9/12/2007
}
int SiftGPU::GetFeatureNum()
{
return _pyramid->GetFeatureNum();
}
unsigned int SiftGPU::GetKeyPointsAndDescriptorsCUDA(SIFTImageGPU& siftImage, const float* d_depthData, unsigned int maxNumKeyPoints /*= (unsigned int)-1*/)
{
_pyramid->GetKeyPointsCUDA((float4*)siftImage.d_keyPoints, d_depthData, maxNumKeyPoints);
_pyramid->GetFeatureVectorCUDA((unsigned char*)siftImage.d_keyPointDescs, maxNumKeyPoints);
int feature_num=_pyramid->GetFeatureNum();
cutilSafeCall(cudaMemcpy(siftImage.d_keyPointCounter, &feature_num, sizeof(int), cudaMemcpyHostToDevice));
return feature_num;
}
void SiftGPU::GetKeyPointsCUDA(SiftKeypoint* d_keypoints, float* d_depthData, unsigned int maxNumKeyPoints /*= (unsigned int)-1*/)
{
_pyramid->GetKeyPointsCUDA((float4*)d_keypoints, d_depthData, maxNumKeyPoints);
}
void SiftGPU::GetDescriptorsCUDA(unsigned char* d_descriptors, unsigned int maxNumKeyPoints /*= (unsigned int)-1*/)
{
_pyramid->GetFeatureVectorCUDA(d_descriptors, maxNumKeyPoints);
}
//void SiftGPU::CopyFeatureVectorToCPU(SiftKeypoint * keys, float * descriptors)
//{
// // keys.resize(_pyramid->GetFeatureNum());
// if (GlobalUtil::_DescriptorPPT)
// {
// // descriptors.resize(128*_pyramid->GetFeatureNum());
// _pyramid->CopyFeaturesToCPU((float*)(keys), descriptors);
// }
// else
// {
// //descriptors.resize(0);
// _pyramid->CopyFeaturesToCPU((float*)(&keys[0]), NULL);
// }
//}
int SiftGPU::AllocatePyramid(int width, int height)
{
_pyramid->setOctaveMin(GlobalUtil::_octave_min_default);
if (GlobalUtil::_octave_min_default >= 0)
{
width >>= GlobalUtil::_octave_min_default;
height >>= GlobalUtil::_octave_min_default;
}
else
{
width <<= (-GlobalUtil::_octave_min_default);
height <<= (-GlobalUtil::_octave_min_default);
}
_pyramid->ResizePyramid(width, height);
return _pyramid->getPyramidHeight() == height && width == _pyramid->getPyramidWidth();
}
void SiftGPU::EvaluateTimings()
{
_pyramid->EvaluateTimings();
}
SiftGPU* CreateNewSiftGPU()
{
return new SiftGPU();
}