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.

775 lines
24 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /** @file dsift.c
  2. ** @brief Dense SIFT - Definition
  3. ** @author Andrea Vedaldi
  4. ** @author Brian Fulkerson
  5. **/
  6. /*
  7. Copyright (C) 2007-12 Andrea Vedaldi and Brian Fulkerson.
  8. All rights reserved.
  9. This file is part of the VLFeat library and is made available under
  10. the terms of the BSD license (see the COPYING file).
  11. */
  12. #include "dsift.h"
  13. #include "pgm.h"
  14. #include "mathop.h"
  15. #include "imopv.h"
  16. #include <math.h>
  17. #include <string.h>
  18. /**
  19. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  20. @page dsift Dense Scale Invariant Feature Transform (DSIFT)
  21. @author Andrea Vedaldi
  22. @author Brian Fulkerson
  23. @tableofcontents
  24. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  25. @ref dsift.h implements a dense version of @ref sift.h "SIFT". This is
  26. an object that can quickly compute descriptors for densely sampled
  27. keypoints with identical size and orientation. It can be reused for
  28. multiple images of the same size.
  29. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  30. @section dsift-intro Overview
  31. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  32. @sa @ref sift "The SIFT module", @ref dsift-tech "Technical details"
  33. This module implements a fast algorithm for the calculation of a large
  34. number of SIFT descriptors of densely sampled features of the same
  35. scale and orientation. See the @ref sift "SIFT module" for an
  36. overview of SIFT.
  37. The feature frames (keypoints) are indirectly specified by the
  38. sampling steps (::vl_dsift_set_steps) and the sampling bounds
  39. (::vl_dsift_set_bounds). The descriptor geometry (number and size of
  40. the spatial bins and number of orientation bins) can be customized
  41. (::vl_dsift_set_geometry, ::VlDsiftDescriptorGeometry).
  42. @image html dsift-geom.png "Dense SIFT descriptor geometry"
  43. By default, SIFT uses a Gaussian windowing function that discounts
  44. contributions of gradients further away from the descriptor
  45. centers. This function can be changed to a flat window by invoking
  46. ::vl_dsift_set_flat_window. In this case, gradients are accumulated
  47. using only bilinear interpolation, but instad of being reweighted by a
  48. Gassuain window, they are all weighted equally. However, after
  49. gradients have been accumulated into a spatial bin, the whole bin is
  50. reweighted by the average of the Gaussian window over the spatial
  51. support of that bin. This &ldquo;approximation&rdquo; substantially
  52. improves speed with little or no loss of performance in applications.
  53. Keypoints are sampled in such a way that the centers of the spatial
  54. bins are at integer coordinates within the image boundaries. For
  55. instance, the top-left bin of the top-left descriptor is centered on
  56. the pixel (0,0). The bin immediately to the right at
  57. (<code>binSizeX</code>,0), where <code>binSizeX</code> is a paramtere
  58. in the ::VlDsiftDescriptorGeometry structure. ::vl_dsift_set_bounds
  59. can be used to further restrict sampling to the keypoints in an image.
  60. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  61. @section dsift-usage Usage
  62. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  63. DSIFT is implemented by a ::VlDsiftFilter object that can be used
  64. to process a sequence of images of a given geometry.
  65. To use the <b>DSIFT filter</b>:
  66. - Initialize a new DSIFT filter object by ::vl_dsift_new (or the simplified
  67. ::vl_dsift_new_basic). Customize the descriptor parameters by
  68. ::vl_dsift_set_steps, ::vl_dsift_set_geometry, etc.
  69. - Process an image by ::vl_dsift_process.
  70. - Retrieve the number of keypoints (::vl_dsift_get_keypoint_num), the
  71. keypoints (::vl_dsift_get_keypoints), and their descriptors
  72. (::vl_dsift_get_descriptors).
  73. - Optionally repeat for more images.
  74. - Delete the DSIFT filter by ::vl_dsift_delete.
  75. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  76. @section dsift-tech Technical details
  77. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  78. This section extends the @ref sift-tech-descriptor "SIFT descriptor section"
  79. and specialzies it to the case of dense keypoints.
  80. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  81. @subsection dsift-tech-descriptor-dense Dense descriptors
  82. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  83. When computing descriptors for many keypoints differing only by their
  84. position (and with null rotation), further simplifications are
  85. possible. In this case, in fact,
  86. @f{eqnarray*}
  87. \mathbf{x} &=& m \sigma \hat{\mathbf{x}} + T,\\
  88. h(t,i,j)
  89. &=&
  90. m \sigma \int
  91. g_{\sigma_\mathrm{win}}(\mathbf{x} - T)\,
  92. w_\mathrm{ang}(\angle J(\mathbf{x}) - \theta_t)\,
  93. w\left(\frac{x - T_x}{m\sigma} - \hat{x}_i\right)\,
  94. w\left(\frac{y - T_y}{m\sigma} - \hat{y}_j\right)\,
  95. |J(\mathbf{x})|\,
  96. d\mathbf{x}.
  97. @f}
  98. Since many different values of @e T are sampled, this is conveniently
  99. expressed as a separable convolution. First, we translate by @f$
  100. \mathbf{x}_{ij} = m\sigma(\hat x_i,\ \hat y_i)^\top @f$ and we use the
  101. symmetry of the various binning and windowing functions to write
  102. @f{eqnarray*}
  103. h(t,i,j)
  104. &=&
  105. m \sigma \int
  106. g_{\sigma_\mathrm{win}}(T' - \mathbf{x} - \mathbf{x}_{ij})\,
  107. w_\mathrm{ang}(\angle J(\mathbf{x}) - \theta_t)\,
  108. w\left(\frac{T'_x - x}{m\sigma}\right)\,
  109. w\left(\frac{T'_y - y}{m\sigma}\right)\,
  110. |J(\mathbf{x})|\,
  111. d\mathbf{x},
  112. \\
  113. T' &=& T + m\sigma
  114. \left[\begin{array}{cc} x_i \\ y_j \end{array}\right].
  115. @f}
  116. Then we define kernels
  117. @f{eqnarray*}
  118. k_i(x) &=&
  119. \frac{1}{\sqrt{2\pi} \sigma_{\mathrm{win}}}
  120. \exp\left(
  121. -\frac{1}{2}
  122. \frac{(x-x_i)^2}{\sigma_{\mathrm{win}}^2}
  123. \right)
  124. w\left(\frac{x}{m\sigma}\right),
  125. \\
  126. k_j(y) &=&
  127. \frac{1}{\sqrt{2\pi} \sigma_{\mathrm{win}}}
  128. \exp\left(
  129. -\frac{1}{2}
  130. \frac{(y-y_j)^2}{\sigma_{\mathrm{win}}^2}
  131. \right)
  132. w\left(\frac{y}{m\sigma}\right),
  133. @f}
  134. and obtain
  135. @f{eqnarray*}
  136. h(t,i,j) &=& (k_ik_j * \bar J_t)\left( T + m\sigma
  137. \left[\begin{array}{cc} x_i \\ y_j \end{array}\right] \right),
  138. \\
  139. \bar J_t(\mathbf{x}) &=& w_\mathrm{ang}(\angle J(\mathbf{x}) - \theta_t)\,|J(\mathbf{x})|.
  140. @f}
  141. Furthermore, if we use a flat rather than Gaussian windowing function,
  142. the kernels do not depend on the bin, and we have
  143. @f{eqnarray*}
  144. k(z) &=&
  145. \frac{1}{\sigma_{\mathrm{win}}}
  146. w\left(\frac{z}{m\sigma}\right),
  147. \\
  148. h(t,i,j) &=& (k(x)k(y) * \bar J_t)\left( T + m\sigma
  149. \left[\begin{array}{cc} x_i \\ y_j \end{array}\right] \right),
  150. @f}
  151. (here @f$ \sigma_\mathrm{win} @f$ is the side of the flat window).
  152. @note In this case the binning functions @f$ k(z) @f$ are triangular
  153. and the convolution can be computed in time independent on the filter
  154. (i.e. descriptor bin) support size by integral signals.
  155. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  156. @subsection dsift-tech-sampling Sampling
  157. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  158. To avoid resampling and dealing with special boundary conditions, we
  159. impose some mild restrictions on the geometry of the descriptors that
  160. can be computed. In particular, we impose that the bin centers @f$ T +
  161. m\sigma (x_i,\ y_j) @f$ are always at integer coordinates within the
  162. image boundaries. This eliminates the need for costly interpolation.
  163. This condition amounts to (expressed in terms of the @e x coordinate,
  164. and equally applicable to @e y)
  165. @f[
  166. \{0,\dots, W-1\} \ni T_x + m\sigma x_i =
  167. T_x + m\sigma i - \frac{N_x-1}{2}
  168. = \bar T_x + m\sigma i,
  169. \qquad i = 0,\dots,N_x-1.
  170. @f]
  171. Notice that for this condition to be satisfied, the @em descriptor
  172. center @f$ T_x @f$ needs to be either fractional or integer depending
  173. on @f$ N_x @f$ being even or odd. To eliminate this complication,
  174. it is simpler to use as a reference not the descriptor center @e T,
  175. but the coordinates of the upper-left bin @f$ \bar T @f$. Thus we
  176. sample the latter on a regular (integer) grid
  177. @f[
  178. \left[\begin{array}{cc}
  179. 0 \\
  180. 0
  181. \end{array}\right]
  182. \leq
  183. \bar T =
  184. \left[\begin{array}{cc}
  185. \bar T_x^{\min} + p \Delta_x \\
  186. \bar T_y^{\min} + q \Delta_y \\
  187. \end{array}\right]
  188. \leq
  189. \left[\begin{array}{cc}
  190. W - 1 - m\sigma N_x \\
  191. H - 1 - m\sigma N_y
  192. \end{array}\right],
  193. \quad
  194. \bar T =
  195. \left[\begin{array}{cc}
  196. T_x - \frac{N_x - 1}{2} \\
  197. T_y - \frac{N_y - 1}{2} \\
  198. \end{array}\right]
  199. @f]
  200. and we impose that the bin size @f$ m \sigma @f$ is integer as well.
  201. **/
  202. /** ------------------------------------------------------------------
  203. ** @internal @brief Initialize new convolution kernel
  204. ** @param binSize
  205. ** @param numBins
  206. ** @param binIndex negative to use flat window.
  207. ** @param windowSize
  208. ** @return a pointer to new filter.
  209. **/
  210. float *
  211. _vl_dsift_new_kernel (int binSize, int numBins, int binIndex, double windowSize)
  212. {
  213. int filtLen = 2 * binSize - 1 ;
  214. float * ker = (float*)vl_malloc (sizeof(float) * filtLen) ;
  215. float * kerIter = ker ;
  216. float delta = binSize * (binIndex - 0.5F * (numBins - 1)) ;
  217. /*
  218. float sigma = 0.5F * ((numBins - 1) * binSize + 1) ;
  219. float sigma = 0.5F * ((numBins) * binSize) ;
  220. */
  221. float sigma = (float) binSize * (float) windowSize ;
  222. int x ;
  223. for (x = - binSize + 1 ; x <= + binSize - 1 ; ++ x) {
  224. float z = (x - delta) / sigma ;
  225. *kerIter++ = (1.0F - fabsf(x) / binSize) *
  226. ((binIndex >= 0) ? expf(- 0.5F * z*z) : 1.0F) ;
  227. }
  228. return ker ;
  229. }
  230. static float
  231. _vl_dsift_get_bin_window_mean
  232. (int binSize, int numBins, int binIndex, double windowSize)
  233. {
  234. float delta = binSize * (binIndex - 0.5F * (numBins - 1)) ;
  235. /*float sigma = 0.5F * ((numBins - 1) * binSize + 1) ;*/
  236. float sigma = (float) binSize * (float) windowSize ;
  237. int x ;
  238. float acc = 0.0 ;
  239. for (x = - binSize + 1 ; x <= + binSize - 1 ; ++ x) {
  240. float z = (x - delta) / sigma ;
  241. acc += ((binIndex >= 0) ? expf(- 0.5F * z*z) : 1.0F) ;
  242. }
  243. return acc /= (2 * binSize - 1) ;
  244. }
  245. /** ------------------------------------------------------------------
  246. ** @internal @brief Normalize histogram
  247. ** @param begin first element of the histogram.
  248. ** @param end last plus one element of the histogram.
  249. **
  250. ** The function divides the specified histogram by its l2 norm.
  251. **/
  252. VL_INLINE float
  253. _vl_dsift_normalize_histogram (float * begin, float * end)
  254. {
  255. float * iter ;
  256. float norm = 0.0F ;
  257. for (iter = begin ; iter < end ; ++ iter) {
  258. norm += (*iter) * (*iter) ;
  259. }
  260. norm = vl_fast_sqrt_f (norm) + VL_EPSILON_F ;
  261. for (iter = begin; iter < end ; ++ iter) {
  262. *iter /= norm ;
  263. }
  264. return norm ;
  265. }
  266. /** ------------------------------------------------------------------
  267. ** @internal @brief Free internal buffers
  268. ** @param self DSIFT filter.
  269. **/
  270. static void
  271. _vl_dsift_free_buffers (VlDsiftFilter* self)
  272. {
  273. if (self->frames) {
  274. vl_free(self->frames) ;
  275. self->frames = NULL ;
  276. }
  277. if (self->descrs) {
  278. vl_free(self->descrs) ;
  279. self->descrs = NULL ;
  280. }
  281. if (self->grads) {
  282. int t ;
  283. for (t = 0 ; t < self->numGradAlloc ; ++t)
  284. if (self->grads[t]) vl_free(self->grads[t]) ;
  285. vl_free(self->grads) ;
  286. self->grads = NULL ;
  287. }
  288. self->numFrameAlloc = 0 ;
  289. self->numBinAlloc = 0 ;
  290. self->numGradAlloc = 0 ;
  291. }
  292. /** ------------------------------------------------------------------
  293. ** @internal @brief Updates internal buffers to current geometry
  294. **/
  295. VL_EXPORT void
  296. _vl_dsift_update_buffers (VlDsiftFilter * self)
  297. {
  298. int x1 = self->boundMinX ;
  299. int x2 = self->boundMaxX ;
  300. int y1 = self->boundMinY ;
  301. int y2 = self->boundMaxY ;
  302. int rangeX = x2 - x1 - (self->geom.numBinX - 1) * self->geom.binSizeX ;
  303. int rangeY = y2 - y1 - (self->geom.numBinY - 1) * self->geom.binSizeY ;
  304. int numFramesX = (rangeX >= 0) ? rangeX / self->stepX + 1 : 0 ;
  305. int numFramesY = (rangeY >= 0) ? rangeY / self->stepY + 1 : 0 ;
  306. self->numFrames = numFramesX * numFramesY ;
  307. self->descrSize = self->geom.numBinT *
  308. self->geom.numBinX *
  309. self->geom.numBinY ;
  310. }
  311. /** ------------------------------------------------------------------
  312. ** @internal @brief Allocate internal buffers
  313. ** @param self DSIFT filter.
  314. **
  315. ** The function (re)allocates the internal buffers in accordance with
  316. ** the current image and descriptor geometry.
  317. **/
  318. static void
  319. _vl_dsift_alloc_buffers (VlDsiftFilter* self)
  320. {
  321. _vl_dsift_update_buffers (self) ;
  322. {
  323. int numFrameAlloc = vl_dsift_get_keypoint_num (self) ;
  324. int numBinAlloc = vl_dsift_get_descriptor_size (self) ;
  325. int numGradAlloc = self->geom.numBinT ;
  326. /* see if we need to update the buffers */
  327. if (numBinAlloc != self->numBinAlloc ||
  328. numGradAlloc != self->numGradAlloc ||
  329. numFrameAlloc != self->numFrameAlloc) {
  330. int t ;
  331. _vl_dsift_free_buffers(self) ;
  332. self->frames = (VlDsiftKeypoint*)vl_malloc(sizeof(VlDsiftKeypoint) * numFrameAlloc) ;
  333. self->descrs = (float*) vl_malloc(sizeof(float) * numBinAlloc * numFrameAlloc) ;
  334. self->grads = (float**) vl_malloc(sizeof(float*) * numGradAlloc) ;
  335. for (t = 0 ; t < numGradAlloc ; ++t) {
  336. self->grads[t] =(float*)
  337. vl_malloc(sizeof(float) * self->imWidth * self->imHeight) ;
  338. }
  339. self->numBinAlloc = numBinAlloc ;
  340. self->numGradAlloc = numGradAlloc ;
  341. self->numFrameAlloc = numFrameAlloc ;
  342. }
  343. }
  344. }
  345. /** ------------------------------------------------------------------
  346. ** @brief Create a new DSIFT filter
  347. **
  348. ** @param imWidth width of the image.
  349. ** @param imHeight height of the image
  350. **
  351. ** @return new filter.
  352. **/
  353. VL_EXPORT VlDsiftFilter *
  354. vl_dsift_new (int imWidth, int imHeight)
  355. {
  356. VlDsiftFilter * self = (VlDsiftFilter*) vl_malloc (sizeof(VlDsiftFilter)) ;
  357. self->imWidth = imWidth ;
  358. self->imHeight = imHeight ;
  359. self->stepX = 5 ;
  360. self->stepY = 5 ;
  361. self->boundMinX = 0 ;
  362. self->boundMinY = 0 ;
  363. self->boundMaxX = imWidth - 1 ;
  364. self->boundMaxY = imHeight - 1 ;
  365. self->geom.numBinX = 4 ;
  366. self->geom.numBinY = 4 ;
  367. self->geom.numBinT = 8 ;
  368. self->geom.binSizeX = 5 ;
  369. self->geom.binSizeY = 5 ;
  370. self->useFlatWindow = VL_FALSE ;
  371. self->windowSize = 2.0 ;
  372. self->convTmp1 = (float*) vl_malloc(sizeof(float) * self->imWidth * self->imHeight) ;
  373. self->convTmp2 = (float*) vl_malloc(sizeof(float) * self->imWidth * self->imHeight) ;
  374. self->numBinAlloc = 0 ;
  375. self->numFrameAlloc = 0 ;
  376. self->numGradAlloc = 0 ;
  377. self->descrSize = 0 ;
  378. self->numFrames = 0 ;
  379. self->grads = NULL ;
  380. self->frames = NULL ;
  381. self->descrs = NULL ;
  382. _vl_dsift_update_buffers(self) ;
  383. return self ;
  384. }
  385. /** ------------------------------------------------------------------
  386. ** @brief Create a new DSIFT filter (basic interface)
  387. ** @param imWidth width of the image.
  388. ** @param imHeight height of the image.
  389. ** @param step sampling step.
  390. ** @param binSize bin size.
  391. ** @return new filter.
  392. **
  393. ** The descriptor geometry matches the standard SIFT descriptor.
  394. **/
  395. VL_EXPORT VlDsiftFilter *
  396. vl_dsift_new_basic (int imWidth, int imHeight, int step, int binSize)
  397. {
  398. VlDsiftFilter* self = vl_dsift_new(imWidth, imHeight) ;
  399. VlDsiftDescriptorGeometry geom = *vl_dsift_get_geometry(self) ;
  400. geom.binSizeX = binSize ;
  401. geom.binSizeY = binSize ;
  402. vl_dsift_set_geometry(self, &geom) ;
  403. vl_dsift_set_steps(self, step, step) ;
  404. return self ;
  405. }
  406. /** ------------------------------------------------------------------
  407. ** @brief Delete DSIFT filter
  408. ** @param self DSIFT filter.
  409. **/
  410. VL_EXPORT void
  411. vl_dsift_delete (VlDsiftFilter * self)
  412. {
  413. _vl_dsift_free_buffers (self) ;
  414. if (self->convTmp2) vl_free (self->convTmp2) ;
  415. if (self->convTmp1) vl_free (self->convTmp1) ;
  416. vl_free (self) ;
  417. }
  418. /** ------------------------------------------------------------------
  419. ** @internal @brief Process with Gaussian window
  420. ** @param self DSIFT filter.
  421. **/
  422. VL_INLINE void
  423. _vl_dsift_with_gaussian_window (VlDsiftFilter * self)
  424. {
  425. int binx, biny, bint ;
  426. int framex, framey ;
  427. float *xker, *yker ;
  428. int Wx = self->geom.binSizeX - 1 ;
  429. int Wy = self->geom.binSizeY - 1 ;
  430. for (biny = 0 ; biny < self->geom.numBinY ; ++biny) {
  431. yker = _vl_dsift_new_kernel (self->geom.binSizeY,
  432. self->geom.numBinY,
  433. biny,
  434. self->windowSize) ;
  435. for (binx = 0 ; binx < self->geom.numBinX ; ++binx) {
  436. xker = _vl_dsift_new_kernel(self->geom.binSizeX,
  437. self->geom.numBinX,
  438. binx,
  439. self->windowSize) ;
  440. for (bint = 0 ; bint < self->geom.numBinT ; ++bint) {
  441. vl_imconvcol_vf (self->convTmp1, self->imHeight,
  442. self->grads[bint], self->imWidth, self->imHeight,
  443. self->imWidth,
  444. yker, -Wy, +Wy, 1,
  445. VL_PAD_BY_CONTINUITY|VL_TRANSPOSE) ;
  446. vl_imconvcol_vf (self->convTmp2, self->imWidth,
  447. self->convTmp1, self->imHeight, self->imWidth,
  448. self->imHeight,
  449. xker, -Wx, +Wx, 1,
  450. VL_PAD_BY_CONTINUITY|VL_TRANSPOSE) ;
  451. {
  452. float *dst = self->descrs
  453. + bint
  454. + binx * self->geom.numBinT
  455. + biny * (self->geom.numBinX * self->geom.numBinT) ;
  456. float *src = self->convTmp2 ;
  457. int frameSizeX = self->geom.binSizeX * (self->geom.numBinX - 1) + 1 ;
  458. int frameSizeY = self->geom.binSizeY * (self->geom.numBinY - 1) + 1 ;
  459. int descrSize = vl_dsift_get_descriptor_size (self) ;
  460. for (framey = self->boundMinY ;
  461. framey <= self->boundMaxY - frameSizeY + 1 ;
  462. framey += self->stepY) {
  463. for (framex = self->boundMinX ;
  464. framex <= self->boundMaxX - frameSizeX + 1 ;
  465. framex += self->stepX) {
  466. *dst = src [(framex + binx * self->geom.binSizeX) * 1 +
  467. (framey + biny * self->geom.binSizeY) * self->imWidth] ;
  468. dst += descrSize ;
  469. } /* framex */
  470. } /* framey */
  471. }
  472. } /* for bint */
  473. vl_free (xker) ;
  474. } /* for binx */
  475. vl_free (yker) ;
  476. } /* for biny */
  477. }
  478. /** ------------------------------------------------------------------
  479. ** @internal @brief Process with flat window.
  480. ** @param self DSIFT filter object.
  481. **/
  482. VL_INLINE void
  483. _vl_dsift_with_flat_window (VlDsiftFilter* self)
  484. {
  485. int binx, biny, bint ;
  486. int framex, framey ;
  487. /* for each orientation bin */
  488. for (bint = 0 ; bint < self->geom.numBinT ; ++bint) {
  489. vl_imconvcoltri_f (self->convTmp1, self->imHeight,
  490. self->grads [bint], self->imWidth, self->imHeight,
  491. self->imWidth,
  492. self->geom.binSizeY, /* filt size */
  493. 1, /* subsampling step */
  494. VL_PAD_BY_CONTINUITY|VL_TRANSPOSE) ;
  495. vl_imconvcoltri_f (self->convTmp2, self->imWidth,
  496. self->convTmp1, self->imHeight, self->imWidth,
  497. self->imHeight,
  498. self->geom.binSizeX,
  499. 1,
  500. VL_PAD_BY_CONTINUITY|VL_TRANSPOSE) ;
  501. for (biny = 0 ; biny < self->geom.numBinY ; ++biny) {
  502. /*
  503. This fast version of DSIFT does not use a proper Gaussian
  504. weighting scheme for the gradiens that are accumulated on the
  505. spatial bins. Instead each spatial bins is accumulated based on
  506. the triangular kernel only, equivalent to bilinear interpolation
  507. plus a flat, rather than Gaussian, window. Eventually, however,
  508. the magnitude of the spatial bins in the SIFT descriptor is
  509. reweighted by the average of the Gaussian window on each bin.
  510. */
  511. float wy = _vl_dsift_get_bin_window_mean
  512. (self->geom.binSizeY, self->geom.numBinY, biny,
  513. self->windowSize) ;
  514. /* The convolution functions vl_imconvcoltri_* convolve by a
  515. * triangular kernel with unit integral. Instead for SIFT the
  516. * triangular kernel should have unit height. This is
  517. * compensated for by multiplying by the bin size:
  518. */
  519. wy *= self->geom.binSizeY ;
  520. for (binx = 0 ; binx < self->geom.numBinX ; ++binx) {
  521. float w ;
  522. float wx = _vl_dsift_get_bin_window_mean (self->geom.binSizeX,
  523. self->geom.numBinX,
  524. binx,
  525. self->windowSize) ;
  526. float *dst = self->descrs
  527. + bint
  528. + binx * self->geom.numBinT
  529. + biny * (self->geom.numBinX * self->geom.numBinT) ;
  530. float *src = self->convTmp2 ;
  531. int frameSizeX = self->geom.binSizeX * (self->geom.numBinX - 1) + 1 ;
  532. int frameSizeY = self->geom.binSizeY * (self->geom.numBinY - 1) + 1 ;
  533. int descrSize = vl_dsift_get_descriptor_size (self) ;
  534. wx *= self->geom.binSizeX ;
  535. w = wx * wy ;
  536. for (framey = self->boundMinY ;
  537. framey <= self->boundMaxY - frameSizeY + 1 ;
  538. framey += self->stepY) {
  539. for (framex = self->boundMinX ;
  540. framex <= self->boundMaxX - frameSizeX + 1 ;
  541. framex += self->stepX) {
  542. *dst = w * src [(framex + binx * self->geom.binSizeX) * 1 +
  543. (framey + biny * self->geom.binSizeY) * self->imWidth] ;
  544. dst += descrSize ;
  545. } /* framex */
  546. } /* framey */
  547. } /* binx */
  548. } /* biny */
  549. } /* bint */
  550. }
  551. /** ------------------------------------------------------------------
  552. ** @brief Compute keypoints and descriptors
  553. **
  554. ** @param self DSIFT filter.
  555. ** @param im image data.
  556. **/
  557. void vl_dsift_process (VlDsiftFilter* self, float const* im)
  558. {
  559. int t, x, y ;
  560. /* update buffers */
  561. _vl_dsift_alloc_buffers (self) ;
  562. /* clear integral images */
  563. for (t = 0 ; t < self->geom.numBinT ; ++t)
  564. memset (self->grads[t], 0,
  565. sizeof(float) * self->imWidth * self->imHeight) ;
  566. #undef at
  567. #define at(x,y) (im[(y)*self->imWidth+(x)])
  568. /* Compute gradients, their norm, and their angle */
  569. for (y = 0 ; y < self->imHeight ; ++ y) {
  570. for (x = 0 ; x < self->imWidth ; ++ x) {
  571. float gx, gy ;
  572. float angle, mod, nt, rbint ;
  573. int bint ;
  574. /* y derivative */
  575. if (y == 0) {
  576. gy = at(x,y+1) - at(x,y) ;
  577. } else if (y == self->imHeight - 1) {
  578. gy = at(x,y) - at(x,y-1) ;
  579. } else {
  580. gy = 0.5F * (at(x,y+1) - at(x,y-1)) ;
  581. }
  582. /* x derivative */
  583. if (x == 0) {
  584. gx = at(x+1,y) - at(x,y) ;
  585. } else if (x == self->imWidth - 1) {
  586. gx = at(x,y) - at(x-1,y) ;
  587. } else {
  588. gx = 0.5F * (at(x+1,y) - at(x-1,y)) ;
  589. }
  590. /* angle and modulus */
  591. angle = vl_fast_atan2_f (gy,gx) ;
  592. mod = vl_fast_sqrt_f (gx*gx + gy*gy) ;
  593. /* quantize angle */
  594. nt = vl_mod_2pi_f (angle) * (self->geom.numBinT / (2*VL_PI)) ;
  595. bint = (int) vl_floor_f (nt) ;
  596. rbint = nt - bint ;
  597. /* write it back */
  598. self->grads [(bint ) % self->geom.numBinT][x + y * self->imWidth] = (1 - rbint) * mod ;
  599. self->grads [(bint + 1) % self->geom.numBinT][x + y * self->imWidth] = ( rbint) * mod ;
  600. }
  601. }
  602. if (self->useFlatWindow) {
  603. _vl_dsift_with_flat_window(self) ;
  604. } else {
  605. _vl_dsift_with_gaussian_window(self) ;
  606. }
  607. {
  608. VlDsiftKeypoint* frameIter = self->frames ;
  609. float * descrIter = self->descrs ;
  610. int framex, framey, bint ;
  611. int frameSizeX = self->geom.binSizeX * (self->geom.numBinX - 1) + 1 ;
  612. int frameSizeY = self->geom.binSizeY * (self->geom.numBinY - 1) + 1 ;
  613. int descrSize = vl_dsift_get_descriptor_size (self) ;
  614. float deltaCenterX = 0.5F * self->geom.binSizeX * (self->geom.numBinX - 1) ;
  615. float deltaCenterY = 0.5F * self->geom.binSizeY * (self->geom.numBinY - 1) ;
  616. float normConstant = frameSizeX * frameSizeY ;
  617. for (framey = self->boundMinY ;
  618. framey <= self->boundMaxY - frameSizeY + 1 ;
  619. framey += self->stepY) {
  620. for (framex = self->boundMinX ;
  621. framex <= self->boundMaxX - frameSizeX + 1 ;
  622. framex += self->stepX) {
  623. frameIter->x = framex + deltaCenterX ;
  624. frameIter->y = framey + deltaCenterY ;
  625. /* mass */
  626. {
  627. float mass = 0 ;
  628. for (bint = 0 ; bint < descrSize ; ++ bint)
  629. mass += descrIter[bint] ;
  630. mass /= normConstant ;
  631. frameIter->norm = mass ;
  632. }
  633. /* L2 normalize */
  634. _vl_dsift_normalize_histogram (descrIter, descrIter + descrSize) ;
  635. /* clamp */
  636. for(bint = 0 ; bint < descrSize ; ++ bint)
  637. if (descrIter[bint] > 0.2F) descrIter[bint] = 0.2F ;
  638. /* L2 normalize */
  639. _vl_dsift_normalize_histogram (descrIter, descrIter + descrSize) ;
  640. frameIter ++ ;
  641. descrIter += descrSize ;
  642. } /* for framex */
  643. } /* for framey */
  644. }
  645. }