You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

821 lines
28 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /** @file scalespace.c
  2. ** @brief Scale Space - Definition
  3. ** @author Karel Lenc
  4. ** @author Andrea Vedaldi
  5. ** @author Michal Perdoch
  6. **/
  7. /*
  8. Copyright (C) 2007-12 Andrea Vedaldi and Brian Fulkerson.
  9. All rights reserved.
  10. This file is part of the VLFeat library and is made available under
  11. the terms of the BSD license (see the COPYING file).
  12. */
  13. /**
  14. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  15. @page scalespace Gaussian Scale Space (GSS)
  16. @author Karel Lenc
  17. @author Andrea Vedaldi
  18. @author Michal Perdoch
  19. @tableofcontents
  20. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  21. @ref scalespace.h implements a Gaussian scale space, a data structure
  22. representing an image at multiple resolutions
  23. @cite{witkin83scale-space} @cite{koenderink84the-structure}
  24. @cite{lindeberg94scale-space}. Scale spaces have many use, including
  25. the detection of co-variant local features
  26. @cite{lindeberg98principles} such as SIFT, Hessian-Affine,
  27. Harris-Affine, Harris-Laplace, etc. @ref scalespace-starting
  28. demonstreates how to use the C API to compute the scalespace of an
  29. image. For further details refer to:
  30. - @subpage scalespace-fundamentals
  31. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  32. @section scalespace-starting Getting started
  33. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  34. Given an input image `image`, the following example uses the
  35. ::VlScaleSpace object to compute its Gaussian scale space and return
  36. the image `level` at scale `(o,s)`, where `o` is the octave and `s` is
  37. the octave subdivision or sublevel:
  38. @code
  39. float* level ;
  40. VlScaleSpace ss = vl_scalespace_new(imageWidth, imageHeight) ;
  41. vl_scalespace_put_image(ss, image) ;
  42. level = vl_scalespace_get_level(ss, o, s) ;
  43. @endcode
  44. The image `level` is obtained by convolving `image` by a Gaussian
  45. filter of isotropic standard deviation given by
  46. @code
  47. double sigma = vl_scalespace_get_sigma(ss, o, s) ;
  48. @endcode
  49. The resolution of `level` is in general different from the resolution
  50. of `image` and is determined by the octave `o`. It can be obtained as
  51. follows:
  52. @code
  53. VlScaleSpaceOctaveGeometry ogeom = vl_scalespace_get_octave_geometry(ss, o) ;
  54. ogeom.width // width of level (in number of pixels)
  55. ogeom.height // height of level (in number of pixels)
  56. ogeom.step // spatial sampling step
  57. @endcode
  58. The parameter `ogeom.step` is the sampling step relatively to the
  59. sampling of the input image `image`. The ranges of valid octaves and
  60. scale sublevels can be obtained as
  61. @code
  62. VlScaleSpaceGeometry geom = vl_scalespace_get_geometry(ss) ;
  63. geom.firstOctave // Index of the fisrt octave
  64. geom.lastOctave // Index of the last octave
  65. geom.octaveResolution ; // Number of octave subdivisions
  66. geom.octaveFirstSubdivision // Index of the first octave subdivision
  67. geom.octaveLastSubdivision // Index of the last octave subdivision
  68. @endcode
  69. So for example `o` minimum value is `geom.firstOctave` and maximum
  70. value is `geom.lastOctave`. The subdivision index `s` naturally spans
  71. the range 0 to `geom.octaveResolution-1`. However, the scale space
  72. object is flexible in that it allows different ranges of subdivisions
  73. to be computed and `s` varies in the range
  74. `geom.octaveFirstSubdivision` to `geom.octaveLastSubdivision`. See
  75. @ref scalespace-fundamentals for further details.
  76. The geometry of the scale space can be customized upon creation, as
  77. follows:
  78. @code
  79. VlScaleSpaceGeometry geom = vl_scalespace_get_default_geometry(imageWidth, imageHeight) ;
  80. geom.firstOctave = -1 ;
  81. geom.octaveFirstSubdivision = -1 ;
  82. geom.octaveLastSubdivision = geom.octaveResolution ;
  83. VlScaleSpacae ss = vl_scalespace_new_with_geometry (geom) ;
  84. @endcode
  85. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  86. @page scalespace-fundamentals Gaussian scale space fundamentals
  87. @tableofcontents
  88. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  89. This page discusses the notion of *Gaussian scale space* and the
  90. relative data structure. For the C API see @ref scalespace.h and @ref
  91. scalespace-starting.
  92. A *scale space* is representation of an image at multiple resolution
  93. levels. An image is a function $\ell(x,y)$ of two coordinates $x$,
  94. $y$; the scale space $\ell(x,y,\sigma)$ adds a third coordinate
  95. $\sigma$ indexing the *scale*. Here the focus is the Gaussian scale
  96. space, where the image $\ell(x,y,\sigma)$ is obtained by smoothing
  97. $\ell(x,y)$ by a Gaussian kernel of isotropic standard deviation
  98. $\sigma$.
  99. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  100. @section scalespace-definition Scale space definition
  101. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  102. Formally, the *Gaussian scale space* of an image $\ell(x,y)$ is
  103. defined as
  104. \[
  105. \ell(x,y,\sigma) =
  106. [g_{\sigma} * \ell](x,y,\sigma)
  107. \]
  108. where $g_\sigma$ denotes a 2D Gaussian kernel of isotropic standard
  109. deviation $\sigma$:
  110. \[
  111. g_{\sigma}(x,y) = \frac{1}{2\pi\sigma^2}
  112. \exp\left(
  113. - \frac{x^2 + y^2}{2\sigma^2}
  114. \right).
  115. \]
  116. An important detail is that the algorithm computing the scale space
  117. assumes that the input image $\ell(x,y)$ is pre-smoothed, roughly
  118. capturing the effect of the finite pixel size in a CCD. This is
  119. modelled by assuming that the input is not $\ell(x,y)$, but
  120. $\ell(x,y,\sigma_n)$, where $\sigma_n$ is a *nominal smoothing*,
  121. usually taken to be 0.5 (half a pixel standard deviation). This also
  122. means that $\sigma = \sigma_n = 0.5$ is the *finest scale* that can
  123. actually be computed.
  124. The scale space structure stores samples of the function
  125. $\ell(x,y,\sigma)$. The density of the sampling of the spatial
  126. coordinates $x$ and $y$ is adjusted as a function of the scale
  127. $\sigma$, corresponding to the intuition that images at a coarse
  128. resolution can be sampled more coarsely without loss of
  129. information. Thus, the scale space has the structure of a *pyramid*: a
  130. collection of digital images sampled at progressively coarser spatial
  131. resolution and hence of progressively smaller size (in pixels).
  132. The following figure illustrates the scale space pyramid structure:
  133. @image html scalespace-basic.png "A scalespace structure with 2 octaves and S=3 subdivisions per octave"
  134. The pyramid is organised in a number of *octaves*, indexed by a
  135. parameter `o`. Each octave is further subdivided into *sublevels*,
  136. indexed by a parameter `s`. These are related to the scale $\sigma$ by
  137. the equation
  138. \[
  139. \sigma(s,o) = \sigma_o 2^{\displaystyle o + \frac{s}{\mathtt{octaveResolution}}}
  140. \]
  141. where `octaveResolution` is the resolution of the octave subsampling
  142. $\sigma_0$ is the *base smoothing*.
  143. At each octave the spatial resolution is doubled, in the sense that
  144. samples are take with a step of
  145. \[
  146. \mathtt{step} = 2^o.
  147. \]
  148. Hence, denoting as `level[i,j]` the corresponding samples, one has
  149. $\ell(x,y,\sigma) = \mathtt{level}[i,j]$, where
  150. \[
  151. (x,y) = (i,j) \times \mathtt{step},
  152. \quad
  153. \sigma = \sigma(o,s),
  154. \quad
  155. 0 \leq i < \mathtt{lwidth},
  156. \quad
  157. 0 \leq j < \mathtt{lheight},
  158. \]
  159. where
  160. \[
  161. \mathtt{lwidth} = \lfloor \frac{\mathtt{width}}{2^\mathtt{o}}\rfloor, \quad
  162. \mathtt{lheight} = \lfloor \frac{\mathtt{height}}{2^\mathtt{o}}\rfloor.
  163. \]
  164. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  165. @section scalespace-geometry Scale space geometry
  166. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  167. In addition to the parameters discussed above, the geometry of the
  168. data stored in a scale space structure depends on the range of
  169. allowable octaves `o` and scale sublevels `s`.
  170. While `o` may range in any reasonable value given the size of the
  171. input image `image`, usually its minimum value is either 0 or -1. The
  172. latter corresponds to doubling the resolution of the image in the
  173. first octave of the scale space and it is often used in feature
  174. extraction. While there is no information added to the image by
  175. upsampling in this manner, fine scale filters, including derivative
  176. filters, are much easier to compute by upsalmpling first. The maximum
  177. practical value is dictated by the image resolution, as it should be
  178. $2^o\leq\min\{\mathtt{width},\mathtt{height}\}$. VLFeat has the
  179. flexibility of specifying the range of `o` using the `firstOctave` and
  180. `lastOctave` parameters of the ::VlScaleSpaceGeometry structure.
  181. The sublevel `s` varies naturally in the range
  182. $\{0,\dots,\mathtt{octaveResolution}-1\}$. However, it is often
  183. convenient to store a few extra levels per octave (e.g. to compute the
  184. local maxima of a function in scale or the Difference of Gaussian
  185. cornerness measure). Thus VLFeat scale space structure allows this
  186. parameter to vary in an arbitrary range, specified by the parameters
  187. `octaveFirstSubdivision` and `octaveLastSubdivision` of
  188. ::VlScaleSpaceGeometry.
  189. Overall the possible values of the indexes `o` and `s` are:
  190. \[
  191. \mathtt{firstOctave} \leq o \leq \mathtt{lastOctave},
  192. \qquad
  193. \mathtt{octaveFirstSubdivision} \leq s \leq \mathtt{octaveLastSubdivision}.
  194. \]
  195. Note that, depending on these ranges, there could be *redundant pairs*
  196. of indexes `o` and `s` that represent the *same* pyramid level at more
  197. than one sampling resolution. In practice, the ability to generate
  198. such redundant information is very useful in algorithms using
  199. scalespaces, as coding multiscale operations using a fixed sampling
  200. resolution is far easier. For example, the DoG feature detector
  201. computes the scalespace with three redundant levels per octave, as
  202. follows:
  203. @image html scalespace.png "A scalespace containing redundant representation of certain scale levels."
  204. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  205. @section scalespace-algorithm Algorithm and limitations
  206. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  207. Given $\ell(x,y,\sigma_n)$, any of a vast number digitial filtering
  208. techniques can be used to compute the scale levels. Presently, VLFeat
  209. uses a basic FIR implementation of the Gaussian filters.
  210. The FIR implementation is obtained by sampling the Gaussian function
  211. and re-normalizing it to have unit norm. This simple construction does
  212. not account properly for sampling effect, which may be a problem for
  213. very small Gausisan kernels. As a rule of thumb, such filters work
  214. sufficiently well for, say, standard deviation $\sigma$ at least 1.6
  215. times the sampling step. A work around to apply this basic FIR
  216. implementation to very small Gaussian filters is to upsample the image
  217. first.
  218. The limitations on the FIR filters have relatively important for the
  219. pyramid construction, as the latter is obtained by *incremental
  220. smoothing*: each successive level is obtained from the previous one by
  221. adding the needed amount of smoothing. In this manner, the size of the
  222. FIR filters remains small, which makes them efficient; at the same
  223. time, for what discussed, excessively small filters are not
  224. represented properly.
  225. */
  226. #include "scalespace.h"
  227. #include "mathop.h"
  228. #include <assert.h>
  229. #include <stdlib.h>
  230. #include <string.h>
  231. #include <math.h>
  232. #include <stdio.h>
  233. /** @file scalespace.h
  234. ** @struct VlScaleSpace
  235. ** @brief Scale space class
  236. **
  237. ** This is an opaque class used to compute the scale space of an
  238. ** image.
  239. **/
  240. struct _VlScaleSpace
  241. {
  242. VlScaleSpaceGeometry geom ; /**< Geometry of the scale space */
  243. float **octaves ; /**< Data */
  244. } ;
  245. /* ---------------------------------------------------------------- */
  246. /** @brief Get the default geometry for a given image size.
  247. ** @param width image width.
  248. ** @param height image height.
  249. ** @return the default scale space geometry.
  250. **
  251. ** Both @a width and @a height must be at least one pixel wide.
  252. **/
  253. VlScaleSpaceGeometry
  254. vl_scalespace_get_default_geometry (vl_size width, vl_size height)
  255. {
  256. VlScaleSpaceGeometry geom ;
  257. assert(width >= 1) ;
  258. assert(height >= 1) ;
  259. geom.width = width ;
  260. geom.height = height ;
  261. geom.firstOctave = 0 ;
  262. geom.lastOctave = VL_MAX(floor(vl_log2_d(VL_MIN(width, height))) - 3, 0) ;
  263. geom.octaveResolution= 3 ;
  264. geom.octaveFirstSubdivision = 0 ;
  265. geom.octaveLastSubdivision = geom.octaveResolution - 1 ;
  266. geom.baseScale = 1.6 * pow(2.0, 1.0 / geom.octaveResolution) ;
  267. geom.nominalScale = 0.5 ;
  268. return geom ;
  269. }
  270. #define is_valid_geometry(geom) (\
  271. geom.firstOctave <= geom.lastOctave && \
  272. geom.octaveResolution >= 1 && \
  273. geom.octaveFirstSubdivision <= geom.octaveLastSubdivision && \
  274. geom.baseScale >= 0.0 && \
  275. geom.nominalScale >= 0.0)
  276. /** @brief Check scale space geometries for equality
  277. ** @param a first geometry.
  278. ** @param b second geometry.
  279. ** @return true if equal.
  280. **/
  281. vl_bool
  282. vl_scalespacegeometry_is_equal (VlScaleSpaceGeometry a,
  283. VlScaleSpaceGeometry b)
  284. {
  285. return
  286. a.width == b.width &&
  287. a.height == b.height &&
  288. a.firstOctave == b.firstOctave &&
  289. a.lastOctave == b.lastOctave &&
  290. a.octaveResolution == b.octaveResolution &&
  291. a.octaveFirstSubdivision == b.octaveLastSubdivision &&
  292. a.baseScale == b.baseScale &&
  293. a.nominalScale == b.nominalScale ;
  294. }
  295. /** @brief Get the geometry of the scale space.
  296. ** @param self object.
  297. ** @return the scale space geometry.
  298. **/
  299. VlScaleSpaceGeometry
  300. vl_scalespace_get_geometry (VlScaleSpace const * self)
  301. {
  302. return self->geom ;
  303. }
  304. /** @brief Get the geometry of an octave of the scalespace.
  305. ** @param self object.
  306. ** @param o octave index.
  307. ** @return the geometry of octave @a o.
  308. **/
  309. VlScaleSpaceOctaveGeometry
  310. vl_scalespace_get_octave_geometry (VlScaleSpace const * self, vl_index o)
  311. {
  312. VlScaleSpaceOctaveGeometry ogeom ;
  313. ogeom.width = VL_SHIFT_LEFT(self->geom.width, -o) ;
  314. ogeom.height = VL_SHIFT_LEFT(self->geom.height, -o) ;
  315. ogeom.step = pow(2.0, o) ;
  316. return ogeom ;
  317. }
  318. /** @brief Get the data of a scale space level
  319. ** @param self object.
  320. ** @param o octave index.
  321. ** @param s level index.
  322. ** @return pointer to the data for octave @a o, level @a s.
  323. **
  324. ** The octave index @a o must be in the range @c firstOctave
  325. ** to @c lastOctave and the scale index @a s must be in the
  326. ** range @c octaveFirstSubdivision to @c octaveLastSubdivision.
  327. **/
  328. float *
  329. vl_scalespace_get_level (VlScaleSpace *self, vl_index o, vl_index s)
  330. {
  331. VlScaleSpaceOctaveGeometry ogeom = vl_scalespace_get_octave_geometry(self,o) ;
  332. float * octave ;
  333. assert(self) ;
  334. assert(o >= self->geom.firstOctave) ;
  335. assert(o <= self->geom.lastOctave) ;
  336. assert(s >= self->geom.octaveFirstSubdivision) ;
  337. assert(s <= self->geom.octaveLastSubdivision) ;
  338. octave = self->octaves[o - self->geom.firstOctave] ;
  339. return octave + ogeom.width * ogeom.height * (s - self->geom.octaveFirstSubdivision) ;
  340. }
  341. /** @brief Get the data of a scale space level (const)
  342. ** @param self object.
  343. ** @param o octave index.
  344. ** @param s level index.
  345. ** @return pointer to the data for octave @a o, level @a s.
  346. **
  347. ** This function is the same as ::vl_scalespace_get_level but reutrns
  348. ** a @c const pointer to the data.
  349. **/
  350. float const *
  351. vl_scalespace_get_level_const (VlScaleSpace const * self, vl_index o, vl_index s)
  352. {
  353. return vl_scalespace_get_level((VlScaleSpace*)self, o, s) ;
  354. }
  355. /** ------------------------------------------------------------------
  356. ** @brief Get the scale of a given octave and sublevel
  357. ** @param self object.
  358. ** @param o octave index.
  359. ** @param s sublevel index.
  360. **
  361. ** The function returns the scale $\sigma(o,s)$ as a function of the
  362. ** octave index @a o and sublevel @a s.
  363. **/
  364. double
  365. vl_scalespace_get_level_sigma (VlScaleSpace const *self, vl_index o, vl_index s)
  366. {
  367. return self->geom.baseScale * pow(2.0, o + (double) s / self->geom.octaveResolution) ;
  368. }
  369. /** ------------------------------------------------------------------
  370. ** @internal @brief Upsample the rows and take the transpose
  371. ** @param destination output image.
  372. ** @param source input image.
  373. ** @param width input image width.
  374. ** @param height input image height.
  375. **
  376. ** The output image has dimensions @a height by 2 @a width (so the
  377. ** destination buffer must be at least as big as two times the
  378. ** input buffer).
  379. **
  380. ** Upsampling is performed by linear interpolation.
  381. **/
  382. static void
  383. copy_and_upsample
  384. (float *destination,
  385. float const *source, vl_size width, vl_size height)
  386. {
  387. vl_index x, y, ox, oy ;
  388. float v00, v10, v01, v11 ;
  389. assert(destination) ;
  390. assert(source) ;
  391. for(y = 0 ; y < (signed)height ; ++y) {
  392. oy = (y < ((signed)height - 1)) * width ;
  393. v10 = source[0] ;
  394. v11 = source[oy] ;
  395. for(x = 0 ; x < (signed)width ; ++x) {
  396. ox = x < ((signed)width - 1) ;
  397. v00 = v10 ;
  398. v01 = v11 ;
  399. v10 = source[ox] ;
  400. v11 = source[ox + oy] ;
  401. destination[0] = v00 ;
  402. destination[1] = 0.5f * (v00 + v10) ;
  403. destination[2*width] = 0.5f * (v00 + v01) ;
  404. destination[2*width+1] = 0.25f * (v00 + v01 + v10 + v11) ;
  405. destination += 2 ;
  406. source ++;
  407. }
  408. destination += 2*width ;
  409. }
  410. }
  411. /** ------------------------------------------------------------------
  412. ** @internal @brief Downsample
  413. ** @param destination output imgae buffer.
  414. ** @param source input image buffer.
  415. ** @param width input image width.
  416. ** @param height input image height.
  417. ** @param numOctaves octaves (non negative).
  418. **
  419. ** The function downsamples the image @a d times, reducing it to @c
  420. ** 1/2^d of its original size. The parameters @a width and @a height
  421. ** are the size of the input image. The destination image @a dst is
  422. ** assumed to be <code>floor(width/2^d)</code> pixels wide and
  423. ** <code>floor(height/2^d)</code> pixels high.
  424. **/
  425. static void
  426. copy_and_downsample
  427. (float *destination,
  428. float const *source,
  429. vl_size width, vl_size height, vl_size numOctaves)
  430. {
  431. vl_index x, y ;
  432. vl_size step = 1 << numOctaves ; /* step = 2^numOctaves */
  433. assert(destination) ;
  434. assert(source) ;
  435. if (numOctaves == 0) {
  436. memcpy(destination, source, sizeof(float) * width * height) ;
  437. } else {
  438. for(y = 0 ; y < (signed)height ; y += step) {
  439. float const *p = source + y * width ;
  440. for(x = 0 ; x < (signed)width - ((signed)step - 1) ; x += step) {
  441. *destination++ = *p ;
  442. p += step ;
  443. }
  444. }
  445. }
  446. }
  447. /* ---------------------------------------------------------------- */
  448. /** @brief Create a new scale space object
  449. ** @param width image width.
  450. ** @param height image height.
  451. ** @return new scale space object.
  452. **
  453. ** This function is the same as ::vl_scalespace_new_with_geometry()
  454. ** but it uses ::vl_scalespace_get_default_geometry to initialise
  455. ** the geometry of the scale space from the image size.
  456. **
  457. ** @sa ::vl_scalespace_new_with_geometry(), ::vl_scalespace_delete().
  458. **/
  459. VlScaleSpace *
  460. vl_scalespace_new (vl_size width, vl_size height)
  461. {
  462. VlScaleSpaceGeometry geom ;
  463. geom = vl_scalespace_get_default_geometry(width, height) ;
  464. return vl_scalespace_new_with_geometry(geom) ;
  465. }
  466. /** ------------------------------------------------------------------
  467. ** @brief Create a new scale space with the specified geometry
  468. ** @param geom scale space geomerty.
  469. ** @return new scale space object.
  470. **
  471. ** If the geometry is not valid (see ::VlScaleSpaceGeometry), the
  472. ** result is unpredictable.
  473. **
  474. ** The function returns `NULL` if it was not possible to allocate the
  475. ** object because of an out-of-memory condition.
  476. **
  477. ** @sa ::VlScaleSpaceGeometry, ::vl_scalespace_delete().
  478. **/
  479. VlScaleSpace *
  480. vl_scalespace_new_with_geometry (VlScaleSpaceGeometry geom)
  481. {
  482. vl_index o ;
  483. vl_size numSublevels = geom.octaveLastSubdivision - geom.octaveFirstSubdivision + 1 ;
  484. vl_size numOctaves = geom.lastOctave - geom.firstOctave + 1 ;
  485. VlScaleSpace *self ;
  486. assert(is_valid_geometry(geom)) ;
  487. numOctaves = geom.lastOctave - geom.firstOctave + 1 ;
  488. numSublevels = geom.octaveLastSubdivision - geom.octaveFirstSubdivision + 1 ;
  489. self = (VlScaleSpace*)vl_calloc(1, sizeof(VlScaleSpace)) ;
  490. if (self == NULL) goto err_alloc_self ;
  491. self->geom = geom ;
  492. self->octaves = (float**)vl_calloc(numOctaves, sizeof(float*)) ;
  493. if (self->octaves == NULL) goto err_alloc_octave_list ;
  494. for (o = self->geom.firstOctave ; o <= self->geom.lastOctave ; ++o) {
  495. VlScaleSpaceOctaveGeometry ogeom = vl_scalespace_get_octave_geometry(self,o) ;
  496. vl_size octaveSize = ogeom.width * ogeom.height * numSublevels ;
  497. self->octaves[o - self->geom.firstOctave] = (float*)vl_malloc(octaveSize * sizeof(float)) ;
  498. if (self->octaves[o - self->geom.firstOctave] == NULL) goto err_alloc_octaves;
  499. }
  500. return self ;
  501. err_alloc_octaves:
  502. for (o = self->geom.firstOctave ; o <= self->geom.lastOctave ; ++o) {
  503. if (self->octaves[o - self->geom.firstOctave]) {
  504. vl_free(self->octaves[o - self->geom.firstOctave]) ;
  505. }
  506. }
  507. err_alloc_octave_list:
  508. vl_free(self) ;
  509. err_alloc_self:
  510. return NULL ;
  511. }
  512. /* ---------------------------------------------------------------- */
  513. /** @brief Create a new copy of the object
  514. ** @param self object to copy from.
  515. **
  516. ** The function returns `NULL` if the copy cannot be made due to an
  517. ** out-of-memory condition.
  518. **/
  519. VlScaleSpace *
  520. vl_scalespace_new_copy (VlScaleSpace* self)
  521. {
  522. vl_index o ;
  523. VlScaleSpace * copy = vl_scalespace_new_shallow_copy(self) ;
  524. if (copy == NULL) return NULL ;
  525. for (o = self->geom.firstOctave ; o <= self->geom.lastOctave ; ++o) {
  526. VlScaleSpaceOctaveGeometry ogeom = vl_scalespace_get_octave_geometry(self,o) ;
  527. vl_size numSubevels = self->geom.octaveLastSubdivision - self->geom.octaveFirstSubdivision + 1;
  528. memcpy(copy->octaves[o - self->geom.firstOctave],
  529. self->octaves[o - self->geom.firstOctave],
  530. ogeom.width * ogeom.height * numSubevels * sizeof(float)) ;
  531. }
  532. return copy ;
  533. }
  534. /* ---------------------------------------------------------------- */
  535. /** @brief Create a new shallow copy of the object
  536. ** @param self object to copy from.
  537. **
  538. ** The function works like ::vl_scalespace_new_copy() but only allocates
  539. ** the scale space, without actually copying the data.
  540. **/
  541. VlScaleSpace *
  542. vl_scalespace_new_shallow_copy (VlScaleSpace* self)
  543. {
  544. return vl_scalespace_new_with_geometry (self->geom) ;
  545. }
  546. /* ---------------------------------------------------------------- */
  547. /** @brief Delete object
  548. ** @param self object to delete.
  549. ** @sa ::vl_scalespace_new()
  550. **/
  551. void
  552. vl_scalespace_delete (VlScaleSpace * self)
  553. {
  554. if (self) {
  555. if (self->octaves) {
  556. vl_index o ;
  557. for (o = self->geom.firstOctave ; o <= self->geom.lastOctave ; ++o) {
  558. if (self->octaves[o - self->geom.firstOctave]) {
  559. vl_free(self->octaves[o - self->geom.firstOctave]) ;
  560. }
  561. }
  562. vl_free(self->octaves) ;
  563. }
  564. vl_free(self) ;
  565. }
  566. }
  567. /* ---------------------------------------------------------------- */
  568. /** @internal @brief Fill octave starting from the first level
  569. ** @param self object instance.
  570. ** @param o octave to process.
  571. **
  572. ** The function takes the first sublevel of octave @a o (the one at
  573. ** sublevel `octaveFirstLevel` and iteratively
  574. ** smoothes it to obtain the other octave levels.
  575. **/
  576. void
  577. _vl_scalespace_fill_octave (VlScaleSpace *self, vl_index o)
  578. {
  579. vl_index s ;
  580. VlScaleSpaceOctaveGeometry ogeom = vl_scalespace_get_octave_geometry(self, o) ;
  581. for(s = self->geom.octaveFirstSubdivision + 1 ;
  582. s <= self->geom.octaveLastSubdivision ; ++s) {
  583. double sigma = vl_scalespace_get_level_sigma(self, o, s) ;
  584. double previousSigma = vl_scalespace_get_level_sigma(self, o, s - 1) ;
  585. double deltaSigma = sqrtf(sigma*sigma - previousSigma*previousSigma) ;
  586. float* level = vl_scalespace_get_level (self, o, s) ;
  587. float* previous = vl_scalespace_get_level (self, o, s-1) ;
  588. vl_imsmooth_f (level, ogeom.width,
  589. previous, ogeom.width, ogeom.height, ogeom.width,
  590. deltaSigma / ogeom.step, deltaSigma / ogeom.step) ;
  591. }
  592. }
  593. /** ------------------------------------------------------------------
  594. ** @internal @brief Initialize the first level of an octave from an image
  595. ** @param self ::VlScaleSpace object instance.
  596. ** @param image image data.
  597. ** @param o octave to start.
  598. **
  599. ** The function initializes the first level of octave @a o from
  600. ** image @a image. The dimensions of the image are the ones set
  601. ** during the creation of the ::VlScaleSpace object instance.
  602. **/
  603. static void
  604. _vl_scalespace_start_octave_from_image (VlScaleSpace *self,
  605. float const *image,
  606. vl_index o)
  607. {
  608. float *level ;
  609. double sigma, imageSigma ;
  610. vl_index op ;
  611. assert(self) ;
  612. assert(image) ;
  613. assert(o >= self->geom.firstOctave) ;
  614. assert(o <= self->geom.lastOctave) ;
  615. /*
  616. * Copy the image to self->geom.octaveFirstSubdivision of octave o, upscaling or
  617. * downscaling as needed.
  618. */
  619. level = vl_scalespace_get_level(self, VL_MAX(0, o), self->geom.octaveFirstSubdivision) ;
  620. copy_and_downsample(level, image, self->geom.width, self->geom.height, VL_MAX(0, o)) ;
  621. for (op = -1 ; op >= o ; --op) {
  622. VlScaleSpaceOctaveGeometry ogeom = vl_scalespace_get_octave_geometry(self, op + 1) ;
  623. float *succLevel = vl_scalespace_get_level(self, op + 1, self->geom.octaveFirstSubdivision) ;
  624. level = vl_scalespace_get_level(self, op, self->geom.octaveFirstSubdivision) ;
  625. copy_and_upsample(level, succLevel, ogeom.width, ogeom.height) ;
  626. }
  627. /*
  628. * Adjust the smoothing of the first level just initialised, accounting
  629. * for the fact that the input image is assumed to be a nominal scale
  630. * level.
  631. */
  632. sigma = vl_scalespace_get_level_sigma(self, o, self->geom.octaveFirstSubdivision) ;
  633. imageSigma = self->geom.nominalScale ;
  634. if (sigma > imageSigma) {
  635. VlScaleSpaceOctaveGeometry ogeom = vl_scalespace_get_octave_geometry(self, o) ;
  636. double deltaSigma = sqrt (sigma*sigma - imageSigma*imageSigma) ;
  637. level = vl_scalespace_get_level (self, o, self->geom.octaveFirstSubdivision) ;
  638. vl_imsmooth_f (level, ogeom.width,
  639. level, ogeom.width, ogeom.height, ogeom.width,
  640. deltaSigma / ogeom.step, deltaSigma / ogeom.step) ;
  641. }
  642. }
  643. /** @internal @brief Initialize the first level of an octave from the previous octave
  644. ** @param self object.
  645. ** @param o octave to initialize.
  646. **
  647. ** The function initializes the first level of octave @a o from the
  648. ** content of octave <code>o - 1</code>.
  649. **/
  650. static void
  651. _vl_scalespace_start_octave_from_previous_octave (VlScaleSpace *self, vl_index o)
  652. {
  653. double sigma, prevSigma ;
  654. float *level, *prevLevel ;
  655. vl_index prevLevelIndex ;
  656. VlScaleSpaceOctaveGeometry ogeom ;
  657. assert(self) ;
  658. assert(o > self->geom.firstOctave) ; /* must not be the first octave */
  659. assert(o <= self->geom.lastOctave) ;
  660. /*
  661. * From the previous octave pick the level which is closer to
  662. * self->geom.octaveFirstSubdivision in this octave.
  663. * The is self->geom.octaveFirstSubdivision + self->numLevels since there are
  664. * self->geom.octaveResolution levels in an octave, provided that
  665. * this value does not exceed self->geom.octaveLastSubdivision.
  666. */
  667. prevLevelIndex = VL_MIN(self->geom.octaveFirstSubdivision
  668. + (signed)self->geom.octaveResolution,
  669. self->geom.octaveLastSubdivision) ;
  670. prevLevel = vl_scalespace_get_level (self, o - 1, prevLevelIndex) ;
  671. level = vl_scalespace_get_level (self, o, self->geom.octaveFirstSubdivision) ;
  672. ogeom = vl_scalespace_get_octave_geometry(self, o - 1) ;
  673. copy_and_downsample (level, prevLevel, ogeom.width, ogeom.height, 1) ;
  674. /*
  675. * Add remaining smoothing, if any.
  676. */
  677. sigma = vl_scalespace_get_level_sigma(self, o, self->geom.octaveFirstSubdivision) ;
  678. prevSigma = vl_scalespace_get_level_sigma(self, o - 1, prevLevelIndex) ;
  679. if (sigma > prevSigma) {
  680. VlScaleSpaceOctaveGeometry ogeom = vl_scalespace_get_octave_geometry(self, o) ;
  681. double deltaSigma = sqrt (sigma*sigma - prevSigma*prevSigma) ;
  682. level = vl_scalespace_get_level (self, o, self->geom.octaveFirstSubdivision) ;
  683. /* todo: this may fail due to an out-of-memory condition */
  684. vl_imsmooth_f (level, ogeom.width,
  685. level, ogeom.width, ogeom.height, ogeom.width,
  686. deltaSigma / ogeom.step, deltaSigma / ogeom.step) ;
  687. }
  688. }
  689. /** @brief Initialise Scale space with new image
  690. ** @param self ::VlScaleSpace object instance.
  691. ** @param image image to process.
  692. **
  693. ** Compute the data of all the defined octaves and scales of the scale
  694. ** space @a self.
  695. **/
  696. void
  697. vl_scalespace_put_image (VlScaleSpace *self, float const *image)
  698. {
  699. vl_index o ;
  700. _vl_scalespace_start_octave_from_image(self, image, self->geom.firstOctave) ;
  701. _vl_scalespace_fill_octave(self, self->geom.firstOctave) ;
  702. for (o = self->geom.firstOctave + 1 ; o <= self->geom.lastOctave ; ++o) {
  703. _vl_scalespace_start_octave_from_previous_octave(self, o) ;
  704. _vl_scalespace_fill_octave(self, o) ;
  705. }
  706. }