|
1 |
/*IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
|
|
2 |
|
|
3 |
By downloading, copying, installing or using the software you agree to this license.
|
|
4 |
If you do not agree to this license, do not download, install,
|
|
5 |
copy or use the software.
|
|
6 |
|
|
7 |
|
|
8 |
License Agreement
|
|
9 |
For Open Source Computer Vision Library
|
|
10 |
|
|
11 |
Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
|
|
12 |
Copyright (C) 2008-2010, Willow Garage Inc., all rights reserved.
|
|
13 |
Third party copyrights are property of their respective owners.
|
|
14 |
|
|
15 |
Redistribution and use in source and binary forms, with or without modification,
|
|
16 |
are permitted provided that the following conditions are met:
|
|
17 |
|
|
18 |
* Redistribution's of source code must retain the above copyright notice,
|
|
19 |
this list of conditions and the following disclaimer.
|
|
20 |
|
|
21 |
* Redistribution's in binary form must reproduce the above copyright notice,
|
|
22 |
this list of conditions and the following disclaimer in the documentation
|
|
23 |
and/or other materials provided with the distribution.
|
|
24 |
|
|
25 |
* The name of the copyright holders may not be used to endorse or promote products
|
|
26 |
derived from this software without specific prior written permission.
|
|
27 |
|
|
28 |
This software is provided by the copyright holders and contributors "as is" and
|
|
29 |
any express or implied warranties, including, but not limited to, the implied
|
|
30 |
warranties of merchantability and fitness for a particular purpose are disclaimed.
|
|
31 |
In no event shall the Intel Corporation or contributors be liable for any direct,
|
|
32 |
indirect, incidental, special, exemplary, or consequential damages
|
|
33 |
(including, but not limited to, procurement of substitute goods or services;
|
|
34 |
loss of use, data, or profits; or business interruption) however caused
|
|
35 |
and on any theory of liability, whether in contract, strict liability,
|
|
36 |
or tort (including negligence or otherwise) arising in any way out of
|
|
37 |
the use of this software, even if advised of the possibility of such damage.*/
|
|
38 |
|
|
39 |
#include "precomp.hpp"
|
|
40 |
#include "opencv2/imgproc/gaussian_pyramid.hpp"
|
|
41 |
namespace cv{
|
|
42 |
|
|
43 |
Pyramid::Pyramid()
|
|
44 |
{
|
|
45 |
}
|
|
46 |
/**
|
|
47 |
* Pyramid class constructor
|
|
48 |
* octavesN_: number of octaves
|
|
49 |
* layersN_: number of layers before subsampling layer
|
|
50 |
* sigma0_: starting sigma (depends on detector's type, i.e. SIFT sigma0 = 1.6, Harris sigma0 = 1)
|
|
51 |
* omin_: if omin<0 an octave is added before first octave. In this octave the image size is doubled
|
|
52 |
* _DOG: if true, a DOG pyramid is build
|
|
53 |
*/
|
|
54 |
Pyramid::Pyramid(const Mat & img, int octavesN_, int layersN_, float sigma0_, int omin_, bool _DOG) :
|
|
55 |
params(octavesN_, layersN_, sigma0_, omin_)
|
|
56 |
{
|
|
57 |
|
|
58 |
build(img, _DOG);
|
|
59 |
|
|
60 |
}
|
|
61 |
|
|
62 |
/**
|
|
63 |
* Build gaussian pyramid with layersN_ + 3 layers and 2^(1/layersN_) step between layers
|
|
64 |
* each octave is downsampled of a factor of 2
|
|
65 |
*/
|
|
66 |
|
|
67 |
void Pyramid::build(const Mat& img, bool DOG)
|
|
68 |
{
|
|
69 |
|
|
70 |
Size ksize(0, 0);
|
|
71 |
int gsize;
|
|
72 |
|
|
73 |
Size imgSize = img.size();
|
|
74 |
int minSize = MIN(imgSize.width, imgSize.height);
|
|
75 |
int octavesN = MIN(params.octavesN, floor(log2((double) minSize)));
|
|
76 |
float sigma0 = params.sigma0;
|
|
77 |
float sigma = sigma0;
|
|
78 |
int layersN = params.layersN + 3;
|
|
79 |
int omin = params.omin;
|
|
80 |
float k = params.step;
|
|
81 |
|
|
82 |
/*layer to downsample*/
|
|
83 |
int down_lay = 1 / log(k);
|
|
84 |
|
|
85 |
int octave, layer;
|
|
86 |
double sigmaN = 0.5;
|
|
87 |
|
|
88 |
vector<Mat> layers, DOG_layers;
|
|
89 |
/* standard deviation of current layer*/
|
|
90 |
float sigma_curr = sigma;
|
|
91 |
/* standard deviation of previous layer*/
|
|
92 |
float sigma_prev = sigma;
|
|
93 |
|
|
94 |
if (omin < 0)
|
|
95 |
{
|
|
96 |
omin = -1;
|
|
97 |
Mat tmp_img;
|
|
98 |
Mat blurred_img;
|
|
99 |
gsize = ceil(sigmaN * 3) * 2 + 1;
|
|
100 |
GaussianBlur(img, blurred_img, Size(gsize,gsize), sigmaN);
|
|
101 |
resize(blurred_img, tmp_img, ksize, 2, 2, INTER_AREA);
|
|
102 |
layers.push_back(tmp_img);
|
|
103 |
|
|
104 |
for (layer = 1; layer < layersN; layer++)
|
|
105 |
{
|
|
106 |
sigma_curr = getSigma(layer);
|
|
107 |
sigma = sqrt(powf(sigma_curr, 2) - powf(sigma_prev, 2));
|
|
108 |
Mat prev_lay = layers[layer - 1], curr_lay, DOG_lay;
|
|
109 |
/* smoothing is applied on previous layer so sigma_curr^2 = sigma^2 + sigma_prev^2 */
|
|
110 |
gsize = ceil(sigma * 3) * 2 + 1;
|
|
111 |
GaussianBlur(prev_lay, curr_lay, Size(gsize,gsize), sigma);
|
|
112 |
layers.push_back(curr_lay);
|
|
113 |
if (DOG)
|
|
114 |
{
|
|
115 |
absdiff(curr_lay, prev_lay, DOG_lay);
|
|
116 |
DOG_layers.push_back(DOG_lay);
|
|
117 |
}
|
|
118 |
sigma_prev = sigma_curr;
|
|
119 |
|
|
120 |
}
|
|
121 |
Octave tmp_oct(layers);
|
|
122 |
octaves.push_back(tmp_oct);
|
|
123 |
layers.clear();
|
|
124 |
|
|
125 |
if (DOG)
|
|
126 |
{
|
|
127 |
DOGOctave tmp_DOG_Oct(DOG_layers);
|
|
128 |
DOG_octaves.push_back(tmp_DOG_Oct);
|
|
129 |
DOG_layers.clear();
|
|
130 |
}
|
|
131 |
|
|
132 |
}
|
|
133 |
|
|
134 |
/* Presmoothing on first layer */
|
|
135 |
double sb = sigmaN / powf(2.0f, omin);
|
|
136 |
sigma = sigma0;
|
|
137 |
if (sigma0 > sb)
|
|
138 |
sigma = sqrt(sigma0 * sigma0 - sb * sb);
|
|
139 |
|
|
140 |
/*1° step on image*/
|
|
141 |
Mat tmpImg;
|
|
142 |
gsize = ceil(sigma * 3) * 2 + 1;
|
|
143 |
GaussianBlur(img, tmpImg, Size(gsize,gsize), sigma);
|
|
144 |
layers.push_back(tmpImg);
|
|
145 |
|
|
146 |
/*for every octave build layers*/
|
|
147 |
sigma_prev = sigma;
|
|
148 |
|
|
149 |
for (octave = 0; octave < octavesN; octave++)
|
|
150 |
{
|
|
151 |
for (layer = 1; layer < layersN; layer++)
|
|
152 |
{
|
|
153 |
sigma_curr = getSigma(layer);
|
|
154 |
sigma = sqrt(powf(sigma_curr, 2) - powf(sigma_prev, 2));
|
|
155 |
|
|
156 |
Mat prev_lay = layers[layer - 1], curr_lay, DOG_lay;
|
|
157 |
gsize = ceil(sigma * 3) * 2 + 1;
|
|
158 |
GaussianBlur(prev_lay, curr_lay, Size(gsize,gsize), sigma);
|
|
159 |
layers.push_back(curr_lay);
|
|
160 |
|
|
161 |
if (DOG)
|
|
162 |
{
|
|
163 |
absdiff(curr_lay, prev_lay, DOG_lay);
|
|
164 |
DOG_layers.push_back(DOG_lay);
|
|
165 |
}
|
|
166 |
sigma_prev = sigma_curr;
|
|
167 |
}
|
|
168 |
|
|
169 |
Mat resized_lay;
|
|
170 |
resize(layers[down_lay], resized_lay, ksize, 1.0f / 2, 1.0f / 2, INTER_AREA);
|
|
171 |
|
|
172 |
Octave tmp_oct(layers);
|
|
173 |
octaves.push_back(tmp_oct);
|
|
174 |
if (DOG)
|
|
175 |
{
|
|
176 |
DOGOctave tmp_DOG_Oct(DOG_layers);
|
|
177 |
DOG_octaves.push_back(tmp_DOG_Oct);
|
|
178 |
DOG_layers.clear();
|
|
179 |
}
|
|
180 |
sigma_curr = sigma_prev = sigma0;
|
|
181 |
layers.clear();
|
|
182 |
layers.push_back(resized_lay);
|
|
183 |
|
|
184 |
}
|
|
185 |
|
|
186 |
}
|
|
187 |
/**
|
|
188 |
* Return layer at indicated octave and layer numbers
|
|
189 |
*/
|
|
190 |
Mat Pyramid::getLayer(int octave, int layer)
|
|
191 |
{
|
|
192 |
return octaves[octave].getLayerAt(layer);
|
|
193 |
}
|
|
194 |
|
|
195 |
/**
|
|
196 |
* Return DOG layer at indicated octave and layer numbers
|
|
197 |
*/
|
|
198 |
Mat Pyramid::getDOGLayer(int octave, int layer)
|
|
199 |
{
|
|
200 |
assert(!DOG_octaves.empty());
|
|
201 |
return DOG_octaves[octave].getLayerAt(layer);
|
|
202 |
}
|
|
203 |
|
|
204 |
/**
|
|
205 |
* Return sigma value of indicated octave and layer
|
|
206 |
*/
|
|
207 |
float Pyramid::getSigma(int octave, int layer)
|
|
208 |
{
|
|
209 |
|
|
210 |
return pow(2.0f, octave) * powf(params.step, layer) * params.sigma0;
|
|
211 |
}
|
|
212 |
|
|
213 |
/**
|
|
214 |
* Return sigma value of indicated layer
|
|
215 |
* sigma value of layer is the same at each octave
|
|
216 |
* i.e. sigma of first layer at each octave is sigma0
|
|
217 |
*/
|
|
218 |
float Pyramid::getSigma(int layer)
|
|
219 |
{
|
|
220 |
|
|
221 |
return powf(params.step, layer) * params.sigma0;
|
|
222 |
}
|
|
223 |
|
|
224 |
/**
|
|
225 |
* Destructor of Pyramid class
|
|
226 |
*/
|
|
227 |
Pyramid::~Pyramid()
|
|
228 |
{
|
|
229 |
clear();
|
|
230 |
}
|
|
231 |
|
|
232 |
/**
|
|
233 |
* Clear octaves and params
|
|
234 |
*/
|
|
235 |
void Pyramid::clear()
|
|
236 |
{
|
|
237 |
octaves.clear();
|
|
238 |
params.clear();
|
|
239 |
}
|
|
240 |
|
|
241 |
/**
|
|
242 |
* Empty Pyramid
|
|
243 |
* @return
|
|
244 |
*/
|
|
245 |
bool Pyramid::empty()
|
|
246 |
{
|
|
247 |
return octaves.empty();
|
|
248 |
}
|
|
249 |
|
|
250 |
Pyramid::Params::Params()
|
|
251 |
{
|
|
252 |
}
|
|
253 |
|
|
254 |
/**
|
|
255 |
* Params for Pyramid class
|
|
256 |
*
|
|
257 |
*/
|
|
258 |
Pyramid::Params::Params(int octavesN_, int layersN_, float sigma0_, int omin_) :
|
|
259 |
octavesN(octavesN_), layersN(layersN_), sigma0(sigma0_), omin(omin_)
|
|
260 |
{
|
|
261 |
assert(layersN > 0 && octavesN_>0);
|
|
262 |
step = powf(2, 1.0f / layersN);
|
|
263 |
}
|
|
264 |
|
|
265 |
/**
|
|
266 |
* Returns Pyramid's params
|
|
267 |
*/
|
|
268 |
Pyramid::Params Pyramid::getParams()
|
|
269 |
{
|
|
270 |
return params;
|
|
271 |
}
|
|
272 |
|
|
273 |
/**
|
|
274 |
* Set to zero all params
|
|
275 |
*/
|
|
276 |
void Pyramid::Params::clear()
|
|
277 |
{
|
|
278 |
octavesN = 0;
|
|
279 |
layersN = 0;
|
|
280 |
sigma0 = 0;
|
|
281 |
omin = 0;
|
|
282 |
step = 0;
|
|
283 |
}
|
|
284 |
|
|
285 |
/**
|
|
286 |
* Create an Octave with layers
|
|
287 |
*/
|
|
288 |
Pyramid::Octave::Octave(vector<Mat> layers)
|
|
289 |
{
|
|
290 |
|
|
291 |
(*this).layers = layers;
|
|
292 |
}
|
|
293 |
|
|
294 |
/**
|
|
295 |
* Return layers of the Octave
|
|
296 |
*/
|
|
297 |
vector<Mat> Pyramid::Octave::getLayers()
|
|
298 |
{
|
|
299 |
return layers;
|
|
300 |
}
|
|
301 |
|
|
302 |
Pyramid::Octave::Octave()
|
|
303 |
{
|
|
304 |
}
|
|
305 |
|
|
306 |
/**
|
|
307 |
* Return the Octave's layer at index i
|
|
308 |
*/
|
|
309 |
Mat Pyramid::Octave::getLayerAt(int i)
|
|
310 |
{
|
|
311 |
assert(i < (int) layers.size());
|
|
312 |
return layers[i];
|
|
313 |
}
|
|
314 |
|
|
315 |
Pyramid::Octave::~Octave()
|
|
316 |
{
|
|
317 |
}
|
|
318 |
|
|
319 |
Pyramid::DOGOctave::DOGOctave()
|
|
320 |
{
|
|
321 |
}
|
|
322 |
|
|
323 |
Pyramid::DOGOctave::DOGOctave(vector<Mat> layers)
|
|
324 |
{
|
|
325 |
(*this).layers = layers;
|
|
326 |
}
|
|
327 |
|
|
328 |
Pyramid::DOGOctave::~DOGOctave()
|
|
329 |
{
|
|
330 |
}
|
|
331 |
|
|
332 |
vector<Mat> Pyramid::DOGOctave::getLayers()
|
|
333 |
{
|
|
334 |
return layers;
|
|
335 |
}
|
|
336 |
|
|
337 |
Mat Pyramid::DOGOctave::getLayerAt(int i)
|
|
338 |
{
|
|
339 |
assert(i < (int) layers.size());
|
|
340 |
return layers[i];
|
|
341 |
}
|
|
342 |
} // namespace cv
|