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.

2197 lines
70 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /** @file sift.c
  2. ** @brief SIFT - Definition
  3. ** @author Andrea Vedaldi
  4. **/
  5. /*
  6. Copyright (C) 2007-12 Andrea Vedaldi and Brian Fulkerson.
  7. All rights reserved.
  8. This file is part of the VLFeat library and is made available under
  9. the terms of the BSD license (see the COPYING file).
  10. */
  11. /**
  12. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  13. @page sift Scale Invariant Feature Transform (SIFT)
  14. @author Andrea Vedaldi
  15. @par "Credits:" May people have contributed with suggestions and bug
  16. reports. Although the following list is certainly incomplete, we would
  17. like to thank: Wei Dong, Loic, Giuseppe, Liu, Erwin, P. Ivanov, and
  18. Q. S. Luo.
  19. @tableofcontents
  20. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  21. @ref sift.h implements a @ref sift-usage "SIFT filter object", a
  22. reusable object to extract SIFT features @cite{lowe99object} from one
  23. or multiple images.
  24. This is the original VLFeat implementation of SIFT, designed to be
  25. compatible with Lowe's original SIFT. See @ref covdet for a different
  26. version of SIFT integrated in the more general covariant feature
  27. detector engine.
  28. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  29. @section sift-intro Overview
  30. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  31. A SIFT feature is a selected image region (also called keypoint) with
  32. an associated descriptor. Keypoints are extracted by the <b>@ref
  33. sift-intro-detector "SIFT detector"</b> and their descriptors are
  34. computed by the <b>@ref sift-intro-descriptor "SIFT descriptor"</b>. It is
  35. also common to use independently the SIFT detector (i.e. computing the
  36. keypoints without descriptors) or the SIFT descriptor (i.e. computing
  37. descriptors of custom keypoints).
  38. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  39. @subsection sift-intro-detector SIFT detector
  40. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  41. A SIFT <em>keypoint</em> is a circular image region with an
  42. orientation. It is described by a geometric <em>frame</em> of four
  43. parameters: the keypoint center coordinates @e x and @e y, its @e
  44. scale (the radius of the region), and its @e orientation (an angle
  45. expressed in radians). The SIFT detector uses as keypoints image
  46. structures which resemble &ldquo;blobs&rdquo;. By searching for blobs
  47. at multiple scales and positions, the SIFT detector is invariant (or,
  48. more accurately, covariant) to translation, rotations, and re scaling
  49. of the image.
  50. The keypoint orientation is also determined from the local image
  51. appearance and is covariant to image rotations. Depending on the
  52. symmetry of the keypoint appearance, determining the orientation can
  53. be ambiguous. In this case, the SIFT detectors returns a list of up to
  54. four possible orientations, constructing up to four frames (differing
  55. only by their orientation) for each detected image blob.
  56. @image html sift-frame.png "SIFT keypoints are circular image regions with an orientation."
  57. There are several parameters that influence the detection of SIFT
  58. keypoints. First, searching keypoints at multiple scales is obtained
  59. by constructing a so-called &ldquo;Gaussian scale space&rdquo;. The
  60. scale space is just a collection of images obtained by progressively
  61. smoothing the input image, which is analogous to gradually reducing
  62. the image resolution. Conventionally, the smoothing level is called
  63. <em>scale</em> of the image. The construction of the scale space is
  64. influenced by the following parameters, set when creating the SIFT
  65. filter object by ::vl_sift_new():
  66. - <b>Number of octaves</b>. Increasing the scale by an octave means
  67. doubling the size of the smoothing kernel, whose effect is roughly
  68. equivalent to halving the image resolution. By default, the scale
  69. space spans as many octaves as possible (i.e. roughly <code>
  70. log2(min(width,height)</code>), which has the effect of searching
  71. keypoints of all possible sizes.
  72. - <b>First octave index</b>. By convention, the octave of index 0
  73. starts with the image full resolution. Specifying an index greater
  74. than 0 starts the scale space at a lower resolution (e.g. 1 halves
  75. the resolution). Similarly, specifying a negative index starts the
  76. scale space at an higher resolution image, and can be useful to
  77. extract very small features (since this is obtained by interpolating
  78. the input image, it does not make much sense to go past -1).
  79. - <b>Number of levels per octave</b>. Each octave is sampled at this
  80. given number of intermediate scales (by default 3). Increasing this
  81. number might in principle return more refined keypoints, but in
  82. practice can make their selection unstable due to noise (see [1]).
  83. Keypoints are further refined by eliminating those that are likely to
  84. be unstable, either because they are selected nearby an image edge,
  85. rather than an image blob, or are found on image structures with low
  86. contrast. Filtering is controlled by the follow:
  87. - <b>Peak threshold.</b> This is the minimum amount of contrast to
  88. accept a keypoint. It is set by configuring the SIFT filter object
  89. by ::vl_sift_set_peak_thresh().
  90. - <b>Edge threshold.</b> This is the edge rejection threshold. It is
  91. set by configuring the SIFT filter object by
  92. ::vl_sift_set_edge_thresh().
  93. <table>
  94. <caption>Summary of the parameters influencing the SIFT detector.</caption>
  95. <tr style="font-weight:bold;">
  96. <td>Parameter</td>
  97. <td>See also</td>
  98. <td>Controlled by</td>
  99. <td>Comment</td>
  100. </tr>
  101. <tr>
  102. <td>number of octaves</td>
  103. <td> @ref sift-intro-detector </td>
  104. <td>::vl_sift_new</td>
  105. <td></td>
  106. </tr>
  107. <tr>
  108. <td>first octave index</td>
  109. <td> @ref sift-intro-detector </td>
  110. <td>::vl_sift_new</td>
  111. <td>set to -1 to extract very small features</td>
  112. </tr>
  113. <tr>
  114. <td>number of scale levels per octave</td>
  115. <td> @ref sift-intro-detector </td>
  116. <td>::vl_sift_new</td>
  117. <td>can affect the number of extracted keypoints</td>
  118. </tr>
  119. <tr>
  120. <td>edge threshold</td>
  121. <td> @ref sift-intro-detector </td>
  122. <td>::vl_sift_set_edge_thresh</td>
  123. <td>decrease to eliminate more keypoints</td>
  124. </tr>
  125. <tr>
  126. <td>peak threshold</td>
  127. <td> @ref sift-intro-detector </td>
  128. <td>::vl_sift_set_peak_thresh</td>
  129. <td>increase to eliminate more keypoints</td>
  130. </tr>
  131. </table>
  132. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  133. @subsection sift-intro-descriptor SIFT Descriptor
  134. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  135. @sa @ref sift-tech-descriptor "Descriptor technical details"
  136. A SIFT descriptor is a 3-D spatial histogram of the image gradients in
  137. characterizing the appearance of a keypoint. The gradient at each
  138. pixel is regarded as a sample of a three-dimensional elementary
  139. feature vector, formed by the pixel location and the gradient
  140. orientation. Samples are weighed by the gradient norm and accumulated
  141. in a 3-D histogram @em h, which (up to normalization and clamping)
  142. forms the SIFT descriptor of the region. An additional Gaussian
  143. weighting function is applied to give less importance to gradients
  144. farther away from the keypoint center. Orientations are quantized into
  145. eight bins and the spatial coordinates into four each, as follows:
  146. @image html sift-descr-easy.png "The SIFT descriptor is a spatial histogram of the image gradient."
  147. SIFT descriptors are computed by either calling
  148. ::vl_sift_calc_keypoint_descriptor or
  149. ::vl_sift_calc_raw_descriptor. They accept as input a keypoint
  150. frame, which specifies the descriptor center, its size, and its
  151. orientation on the image plane. The following parameters influence the
  152. descriptor calculation:
  153. - <b>magnification factor</b>. The descriptor size is determined by
  154. multiplying the keypoint scale by this factor. It is set by
  155. ::vl_sift_set_magnif.
  156. - <b>Gaussian window size</b>. The descriptor support is determined by
  157. a Gaussian window, which discounts gradient contributions farther away
  158. from the descriptor center. The standard deviation of this window is
  159. set by ::vl_sift_set_window_size and expressed in unit of bins.
  160. VLFeat SIFT descriptor uses the following convention. The @em y axis
  161. points downwards and angles are measured clockwise (to be consistent
  162. with the standard image convention). The 3-D histogram (consisting of
  163. @f$ 8 \times 4 \times 4 = 128 @f$ bins) is stacked as a single
  164. 128-dimensional vector, where the fastest varying dimension is the
  165. orientation and the slowest the @em y spatial coordinate. This is
  166. illustrated by the following figure.
  167. @image html sift-conv-vlfeat.png "VLFeat conventions"
  168. @note Keypoints (frames) D. Lowe's SIFT implementation convention is
  169. slightly different: The @em y axis points upwards and the angles are
  170. measured counter-clockwise.
  171. @image html sift-conv.png "D. Lowes' SIFT implementation conventions"
  172. <table>
  173. <caption>Summary of the parameters influencing the SIFT descriptor.</caption>
  174. <tr style="font-weight:bold;">
  175. <td>Parameter</td>
  176. <td>See also</td>
  177. <td>Controlled by</td>
  178. <td>Comment</td>
  179. </tr>
  180. <tr>
  181. <td>magnification factor</td>
  182. <td> @ref sift-intro-descriptor </td>
  183. <td>::vl_sift_set_magnif</td>
  184. <td>increase this value to enlarge the image region described</td>
  185. </tr>
  186. <tr>
  187. <td>Gaussian window size</td>
  188. <td> @ref sift-intro-descriptor </td>
  189. <td>::vl_sift_set_window_size</td>
  190. <td>smaller values let the center of the descriptor count more</td>
  191. </tr>
  192. </table>
  193. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  194. @section sift-intro-extensions Extensions
  195. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  196. <b>Eliminating low-contrast descriptors.</b> Near-uniform patches do
  197. not yield stable keypoints or descriptors. ::vl_sift_set_norm_thresh()
  198. can be used to set a threshold on the average norm of the local
  199. gradient to zero-out descriptors that correspond to very low contrast
  200. regions. By default, the threshold is equal to zero, which means that
  201. no descriptor is zeroed. Normally this option is useful only with
  202. custom keypoints, as detected keypoints are implicitly selected at
  203. high contrast image regions.
  204. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  205. @section sift-usage Using the SIFT filter object
  206. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  207. The code provided in this module can be used in different ways. You
  208. can instantiate and use a <b>SIFT filter</b> to extract both SIFT
  209. keypoints and descriptors from one or multiple images. Alternatively,
  210. you can use one of the low level functions to run only a part of the
  211. SIFT algorithm (for instance, to compute the SIFT descriptors of
  212. custom keypoints).
  213. To use a <b>SIFT filter</b> object:
  214. - Initialize a SIFT filter object with ::vl_sift_new(). The filter can
  215. be reused for multiple images of the same size (e.g. for an entire
  216. video sequence).
  217. - For each octave in the scale space:
  218. - Compute the next octave of the DOG scale space using either
  219. ::vl_sift_process_first_octave() or ::vl_sift_process_next_octave()
  220. (stop processing if ::VL_ERR_EOF is returned).
  221. - Run the SIFT detector with ::vl_sift_detect() to get the keypoints.
  222. - For each keypoint:
  223. - Use ::vl_sift_calc_keypoint_orientations() to get the keypoint orientation(s).
  224. - For each orientation:
  225. - Use ::vl_sift_calc_keypoint_descriptor() to get the keypoint descriptor.
  226. - Delete the SIFT filter by ::vl_sift_delete().
  227. To compute SIFT descriptors of custom keypoints, use
  228. ::vl_sift_calc_raw_descriptor().
  229. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  230. @section sift-tech Technical details
  231. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  232. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  233. @subsection sift-tech-ss Scale space
  234. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  235. In order to search for image blobs at multiple scale, the SIFT
  236. detector construct a scale space, defined as follows. Let
  237. @f$I_0(\mathbf{x})@f$ denote an idealized <em>infinite resolution</em>
  238. image. Consider the <em>Gaussian kernel</em>
  239. @f[
  240. g_{\sigma}(\mathbf{x})
  241. =
  242. \frac{1}{2\pi\sigma^2}
  243. \exp
  244. \left(
  245. -\frac{1}{2}
  246. \frac{\mathbf{x}^\top\mathbf{x}}{\sigma^2}
  247. \right)
  248. @f]
  249. The <b>Gaussian scale space</b> is the collection of smoothed images
  250. @f[
  251. I_\sigma = g_\sigma * I, \quad \sigma \geq 0.
  252. @f]
  253. The image at infinite resolution @f$ I_0 @f$ is useful conceptually,
  254. but is not available to us; instead, the input image @f$ I_{\sigma_n}
  255. @f$ is assumed to be pre-smoothed at a nominal level @f$ \sigma_n =
  256. 0.5 @f$ to account for the finite resolution of the pixels. Thus in
  257. practice the scale space is computed by
  258. @f[
  259. I_\sigma = g_{\sqrt{\sigma^2 - \sigma_n^2}} * I_{\sigma_n},
  260. \quad \sigma \geq \sigma_n.
  261. @f]
  262. Scales are sampled at logarithmic steps given by
  263. @f[
  264. \sigma = \sigma_0 2^{o+s/S},
  265. \quad s = 0,\dots,S-1,
  266. \quad o = o_{\min}, \dots, o_{\min}+O-1,
  267. @f]
  268. where @f$ \sigma_0 = 1.6 @f$ is the <em>base scale</em>, @f$ o_{\min}
  269. @f$ is the <em>first octave index</em>, @em O the <em>number of
  270. octaves</em> and @em S the <em>number of scales per octave</em>.
  271. Blobs are detected as local extrema of the <b>Difference of
  272. Gaussians</b> (DoG) scale space, obtained by subtracting successive
  273. scales of the Gaussian scale space:
  274. @f[
  275. \mathrm{DoG}_{\sigma(o,s)} = I_{\sigma(o,s+1)} - I_{\sigma(o,s)}
  276. @f]
  277. At each next octave, the resolution of the images is halved to save
  278. computations. The images composing the Gaussian and DoG scale space
  279. can then be arranged as in the following figure:
  280. @image html sift-ss.png "GSS and DoG scale space structures."
  281. The black vertical segments represent images of the Gaussian Scale
  282. Space (GSS), arranged by increasing scale @f$\sigma@f$. Notice that
  283. the scale level index @e s varies in a slightly redundant set
  284. @f[
  285. s = -1, \dots, S+2
  286. @f]
  287. This simplifies glueing together different octaves and extracting DoG
  288. maxima (required by the SIFT detector).
  289. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  290. @subsection sift-tech-detector Detector
  291. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  292. The SIFT frames (keypoints) are extracted based on local extrema
  293. (peaks) of the DoG scale space. Numerically, local extrema are
  294. elements whose @f$ 3 \times 3 \times 3 @f$ neighbors (in space and
  295. scale) have all smaller (or larger) value. Once extracted, local
  296. extrema are quadratically interpolated (this is very important
  297. especially at the lower resolution scales in order to have accurate
  298. keypoint localization at the full resolution). Finally, they are
  299. filtered to eliminate low-contrast responses or responses close to
  300. edges and the orientation(s) are assigned, as explained next.
  301. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  302. @subsubsection sift-tech-detector-peak Eliminating low contrast responses
  303. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  304. Peaks which are too short may have been generated by noise and are
  305. discarded. This is done by comparing the absolute value of the DoG
  306. scale space at the peak with the <b>peak threshold</b> @f$t_p@f$ and
  307. discarding the peak its value is below the threshold.
  308. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  309. @subsubsection sift-tech-detector-edge Eliminating edge responses
  310. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  311. Peaks which are too flat are often generated by edges and do not yield
  312. stable features. These peaks are detected and removed as follows.
  313. Given a peak @f$x,y,\sigma@f$, the algorithm evaluates the @em x,@em y
  314. Hessian of of the DoG scale space at the scale @f$\sigma@f$. Then the
  315. following score (similar to the Harris function) is computed:
  316. @f[
  317. \frac{(\mathrm{tr}\,D(x,y,\sigma))^2}{\det D(x,y,\sigma)},
  318. \quad
  319. D =
  320. \left[
  321. \begin{array}{cc}
  322. \frac{\partial^2 \mathrm{DoG}}{\partial x^2} &
  323. \frac{\partial^2 \mathrm{DoG}}{\partial x\partial y} \\
  324. \frac{\partial^2 \mathrm{DoG}}{\partial x\partial y} &
  325. \frac{\partial^2 \mathrm{DoG}}{\partial y^2}
  326. \end{array}
  327. \right].
  328. @f]
  329. This score has a minimum (equal to 4) when both eigenvalues of the
  330. Jacobian are equal (curved peak) and increases as one of the
  331. eigenvalues grows and the other stays small. Peaks are retained if the
  332. score is below the quantity @f$(t_e+1)(t_e+1)/t_e@f$, where @f$t_e@f$
  333. is the <b>edge threshold</b>. Notice that this quantity has a minimum
  334. equal to 4 when @f$t_e=1@f$ and grows thereafter. Therefore the range
  335. of the edge threshold is @f$[1,\infty)@f$.
  336. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  337. @subsection sift-tech-detector-orientation Orientation assignment
  338. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  339. A peak in the DoG scale space fixes 2 parameters of the keypoint: the
  340. position and scale. It remains to choose an orientation. In order to
  341. do this, SIFT computes an histogram of the gradient orientations in a
  342. Gaussian window with a standard deviation which is 1.5 times bigger
  343. than the scale @f$\sigma@f$ of the keypoint.
  344. @image html sift-orient.png
  345. This histogram is then smoothed and the maximum is selected. In
  346. addition to the biggest mode, up to other three modes whose amplitude
  347. is within the 80% of the biggest mode are retained and returned as
  348. additional orientations.
  349. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  350. @subsection sift-tech-descriptor Descriptor
  351. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  352. A SIFT descriptor of a local region (keypoint) is a 3-D spatial
  353. histogram of the image gradients. The gradient at each pixel is
  354. regarded as a sample of a three-dimensional elementary feature vector,
  355. formed by the pixel location and the gradient orientation. Samples are
  356. weighed by the gradient norm and accumulated in a 3-D histogram @em h,
  357. which (up to normalization and clamping) forms the SIFT descriptor of
  358. the region. An additional Gaussian weighting function is applied to
  359. give less importance to gradients farther away from the keypoint
  360. center.
  361. <!-- @image html sift-bins.png -->
  362. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  363. @subsubsection sift-tech-descriptor-can Construction in the canonical frame
  364. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  365. Denote the gradient vector field computed at the scale @f$ \sigma @f$ by
  366. @f[
  367. J(x,y) = \nabla I_\sigma(x,y)
  368. =
  369. \left[\begin{array}{cc}
  370. \frac{\partial I_\sigma}{\partial x} &
  371. \frac{\partial I_\sigma}{\partial y} &
  372. \end{array}\right]
  373. @f]
  374. The descriptor is a 3-D spatial histogram capturing the distribution
  375. of @f$ J(x,y) @f$. It is convenient to describe its construction in
  376. the <em>canonical frame</em>. In this frame, the image and descriptor
  377. axes coincide and each spatial bin has side 1. The histogram has @f$
  378. N_\theta \times N_x \times N_y @f$ bins (usually @f$ 8 \times 4 \times
  379. 4 @f$), as in the following figure:
  380. @image html sift-can.png Canonical SIFT descriptor and spatial binning functions
  381. Bins are indexed by a triplet of indexes <em>t, i, j</em> and their
  382. centers are given by
  383. @f{eqnarray*}
  384. \theta_t &=& \frac{2\pi}{N_\theta} t, \quad t = 0,\dots,N_{\theta}-1, \\
  385. x_i &=& i - \frac{N_x -1}{2}, \quad i = 0,\dots,N_x-1, \\
  386. y_j &=& j - \frac{N_x -1}{2}, \quad j = 0,\dots,N_y-1. \\
  387. @f}
  388. The histogram is computed by using trilinear interpolation, i.e. by
  389. weighing contributions by the <em>binning functions</em>
  390. @f{eqnarray*}
  391. \displaystyle
  392. w(z) &=& \mathrm{max}(0, 1 - |z|),
  393. \\
  394. \displaystyle
  395. w_\mathrm{ang}(z) &=& \sum_{k=-\infty}^{+\infty}
  396. w\left(
  397. \frac{N_\theta}{2\pi} z + N_\theta k
  398. \right).
  399. @f}
  400. The gradient vector field is transformed in a three-dimensional
  401. density map of weighed contributions
  402. @f[
  403. f(\theta, x, y) = |J(x,y)| \delta(\theta - \angle J(x,y))
  404. @f]
  405. The historam is localized in the keypoint support by a Gaussian window
  406. of standard deviation @f$ \sigma_{\mathrm{win}} @f$. The histogram is
  407. then given by
  408. @f{eqnarray*}
  409. h(t,i,j) &=& \int
  410. g_{\sigma_\mathrm{win}}(x,y)
  411. w_\mathrm{ang}(\theta - \theta_t) w(x-x_i) w(y-y_j)
  412. f(\theta,x,y)
  413. d\theta\,dx\,dy
  414. \\
  415. &=& \int
  416. g_{\sigma_\mathrm{win}}(x,y)
  417. w_\mathrm{ang}(\angle J(x,y) - \theta_t) w(x-x_i) w(y-y_j)
  418. |J(x,y)|\,dx\,dy
  419. @f}
  420. In post processing, the histogram is @f$ l^2 @f$ normalized, then
  421. clamped at 0.2, and @f$ l^2 @f$ normalized again.
  422. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  423. @subsubsection sift-tech-descriptor-image Calculation in the image frame
  424. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  425. Invariance to similarity transformation is attained by attaching
  426. descriptors to SIFT keypoints (or other similarity-covariant frames).
  427. Then projecting the image in the canonical descriptor frames has the
  428. effect of undoing the image deformation.
  429. In practice, however, it is convenient to compute the descriptor
  430. directly in the image frame. To do this, denote with a hat quantities
  431. relative to the canonical frame and without a hat quantities relative
  432. to the image frame (so for instance @f$ \hat x @f$ is the @e
  433. x-coordinate in the canonical frame and @f$ x @f$ the x-coordinate in
  434. the image frame). Assume that canonical and image frame are
  435. related by an affinity:
  436. @f[
  437. \mathbf{x} = A \hat{\mathbf{x}} + T,
  438. \qquad
  439. \mathbf{x} =
  440. \begin{bmatrix}{c}
  441. x \\
  442. y
  443. \end{bmatrix},
  444. \quad
  445. \mathbf{x} =
  446. \begin{bmatrix}{c}
  447. \hat x \\
  448. \hat y
  449. \end{bmatrix}.
  450. @f]
  451. @image html sift-image-frame.png
  452. Then all quantities can be computed in the image frame directly. For instance,
  453. the image at infinite resolution in the two frames are related by
  454. @f[
  455. \hat I_0(\hat{\mathbf{x}}) = I_0(\mathbf{x}),
  456. \qquad
  457. \mathbf{x} = A \hat{\mathbf{x}} + T.
  458. @f]
  459. The canonized image at scale @f$ \hat \sigma @f$ is in relation with the scaled image
  460. @f[
  461. \hat I_{\hat{\sigma}}(\hat{\mathbf{x}}) = I_{A\hat{\sigma}}(\mathbf{x}),
  462. \qquad \mathbf{x} = A \hat{\mathbf{x}} + T
  463. @f]
  464. where, by generalizing the previous definitions, we have
  465. @f[
  466. I_{A\hat \sigma}(\mathbf{x}) = (g_{A\hat\sigma} * I_0)(\mathbf{x}),
  467. \quad
  468. g_{A\hat\sigma}(\mathbf{x})
  469. =
  470. \frac{1}{2\pi|A|\hat \sigma^2}
  471. \exp
  472. \left(
  473. -\frac{1}{2}
  474. \frac{\mathbf{x}^\top A^{-\top}A^{-1}\mathbf{x}}{\hat \sigma^2}
  475. \right)
  476. @f]
  477. Deriving shows that the gradient fields are in relation
  478. @f[
  479. \hat J(\hat{\mathbf{x}}) = J(\mathbf{x}) A,
  480. \quad J(\mathbf{x}) = (\nabla I_{A\hat\sigma})(\mathbf{x}),
  481. \qquad \mathbf{x} = A \hat{\mathbf{x}} + T.
  482. @f]
  483. Therefore we can compute the descriptor either in the image or canonical frame as:
  484. @f{eqnarray*}
  485. h(t,i,j)
  486. &=&
  487. \int
  488. g_{\hat \sigma_\mathrm{win}}(\hat{\mathbf{x}})\,
  489. w_\mathrm{ang}(\angle \hat J(\hat{\mathbf{x}}) - \theta_t)\,
  490. w_{ij}(\hat{\mathbf{x}})\,
  491. |\hat J(\hat{\mathbf{x}})|\,
  492. d\hat{\mathbf{x}}
  493. \\
  494. &=& \int
  495. g_{A \hat \sigma_\mathrm{win}}(\mathbf{x} - T)\,
  496. w_\mathrm{ang}(\angle J(\mathbf{x})A - \theta_t)\,
  497. w_{ij}(A^{-1}(\mathbf{x} - T))\,
  498. |J(\mathbf{x})A|\,
  499. d\mathbf{x}.
  500. @f}
  501. where we defined the product of the two spatial binning functions
  502. @f[
  503. w_{ij}(\hat{\mathbf{x}}) = w(\hat x - \hat x_i) w(\hat y - \hat y_j)
  504. @f]
  505. In the actual implementation, this integral is computed by visiting a
  506. rectangular area of the image that fully contains the keypoint grid
  507. (along with half a bin border to fully include the bin windowing
  508. function). Since the descriptor can be rotated, this area is a
  509. rectangle of sides @f$m/2\sqrt{2} (N_x+1,N_y+1)@f$ (see also the
  510. illustration).
  511. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  512. @subsubsection sift-tech-descriptor-std Standard SIFT descriptor
  513. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  514. For a SIFT-detected keypoint of center @f$ T @f$, scale @f$ \sigma @f$
  515. and orientation @f$ \theta @f$, the affine transformation @f$ (A,T)
  516. @f$ reduces to the similarity transformation
  517. @f[
  518. \mathbf{x} = m \sigma R(\theta) \hat{\mathbf{x}} + T
  519. @f]
  520. where @f$ R(\theta) @f$ is a counter-clockwise rotation of @f$ \theta
  521. @f$ radians, @f$ m \mathcal{\sigma} @f$ is the size of a descriptor
  522. bin in pixels, and @e m is the <b>descriptor magnification factor</b>
  523. which expresses how much larger a descriptor bin is compared to
  524. the scale of the keypoint @f$ \sigma @f$
  525. (the default value is @e m = 3). Moreover, the
  526. standard SIFT descriptor computes the image gradient at the scale of
  527. the keypoints, which in the canonical frame is equivalent to a
  528. smoothing of @f$ \hat \sigma = 1/m @f$. Finally, the default
  529. Gaussian window size is set to have standard deviation
  530. @f$ \hat \sigma_\mathrm{win} = 2 @f$. This yields the formula
  531. @f{eqnarray*}
  532. h(t,i,j)
  533. &=&
  534. m \sigma \int
  535. g_{\sigma_\mathrm{win}}(\mathbf{x} - T)\,
  536. w_\mathrm{ang}(\angle J(\mathbf{x}) - \theta - \theta_t)\,
  537. w_{ij}\left(\frac{R(\theta)^\top \mathbf{x} - T}{m\sigma}\right)\,
  538. |J(\mathbf{x})|\,
  539. d\mathbf{x},
  540. \\
  541. \sigma_{\mathrm{win}} &=& m\sigma\hat \sigma_{\mathrm{win}},
  542. \\
  543. J(\mathbf{x})
  544. &=& \nabla (g_{m \sigma \hat \sigma} * I)(\mathbf{x})
  545. = \nabla (g_{\sigma} * I)(\mathbf{x})
  546. = \nabla I_{\sigma} (\mathbf{x}).
  547. @f}
  548. **/
  549. #include "sift.h"
  550. #include "imopv.h"
  551. #include "mathop.h"
  552. #include <assert.h>
  553. #include <stdlib.h>
  554. #include <string.h>
  555. #include <math.h>
  556. #include <stdio.h>
  557. /** @internal @brief Use bilinear interpolation to compute orientations */
  558. #define VL_SIFT_BILINEAR_ORIENTATIONS 1
  559. #define EXPN_SZ 256 /**< ::fast_expn table size @internal */
  560. #define EXPN_MAX 25.0 /**< ::fast_expn table max @internal */
  561. double expn_tab [EXPN_SZ+1] ; /**< ::fast_expn table @internal */
  562. #define NBO 8
  563. #define NBP 4
  564. #define log2(x) (log(x)/VL_LOG_OF_2)
  565. /** ------------------------------------------------------------------
  566. ** @internal
  567. ** @brief Fast @f$exp(-x)@f$ approximation
  568. **
  569. ** @param x argument.
  570. **
  571. ** The argument must be in the range [0, ::EXPN_MAX] .
  572. **
  573. ** @return approximation of @f$exp(-x)@f$.
  574. **/
  575. VL_INLINE double
  576. fast_expn (double x)
  577. {
  578. double a,b,r ;
  579. int i ;
  580. /*assert(0 <= x && x <= EXPN_MAX) ;*/
  581. if (x > EXPN_MAX) return 0.0 ;
  582. x *= EXPN_SZ / EXPN_MAX ;
  583. i = (int)vl_floor_d (x) ;
  584. r = x - i ;
  585. a = expn_tab [i ] ;
  586. b = expn_tab [i + 1] ;
  587. return a + r * (b - a) ;
  588. }
  589. /** ------------------------------------------------------------------
  590. ** @internal
  591. ** @brief Initialize tables for ::fast_expn
  592. **/
  593. VL_INLINE void
  594. fast_expn_init ()
  595. {
  596. int k ;
  597. for(k = 0 ; k < EXPN_SZ + 1 ; ++ k) {
  598. expn_tab [k] = exp (- (double) k * (EXPN_MAX / EXPN_SZ)) ;
  599. }
  600. }
  601. /** ------------------------------------------------------------------
  602. ** @internal
  603. ** @brief Copy image, upsample rows and take transpose
  604. **
  605. ** @param dst output image buffer.
  606. ** @param src input image buffer.
  607. ** @param width input image width.
  608. ** @param height input image height.
  609. **
  610. ** The output image has dimensions @a height by 2 @a width (so the
  611. ** destination buffer must be at least as big as two times the
  612. ** input buffer).
  613. **
  614. ** Upsampling is performed by linear interpolation.
  615. **/
  616. static void
  617. copy_and_upsample_rows
  618. (vl_sift_pix *dst,
  619. vl_sift_pix const *src, int width, int height)
  620. {
  621. int x, y ;
  622. vl_sift_pix a, b ;
  623. for(y = 0 ; y < height ; ++y) {
  624. b = a = *src++ ;
  625. for(x = 0 ; x < width - 1 ; ++x) {
  626. b = *src++ ;
  627. *dst = a ; dst += height ;
  628. *dst = 0.5 * (a + b) ; dst += height ;
  629. a = b ;
  630. }
  631. *dst = b ; dst += height ;
  632. *dst = b ; dst += height ;
  633. dst += 1 - width * 2 * height ;
  634. }
  635. }
  636. /** ------------------------------------------------------------------
  637. ** @internal
  638. ** @brief Smooth an image
  639. ** @param self SIFT filter.
  640. ** @param outputImage output imgae buffer.
  641. ** @param tempImage temporary image buffer.
  642. ** @param inputImage input image buffer.
  643. ** @param width input image width.
  644. ** @param height input image height.
  645. ** @param sigma smoothing.
  646. **/
  647. static void
  648. _vl_sift_smooth (VlSiftFilt * self,
  649. vl_sift_pix * outputImage,
  650. vl_sift_pix * tempImage,
  651. vl_sift_pix const * inputImage,
  652. vl_size width,
  653. vl_size height,
  654. double sigma)
  655. {
  656. /* prepare Gaussian filter */
  657. if (self->gaussFilterSigma != sigma) {
  658. vl_uindex j ;
  659. vl_sift_pix acc = 0 ;
  660. if (self->gaussFilter) vl_free (self->gaussFilter) ;
  661. self->gaussFilterWidth = VL_MAX(ceil(4.0 * sigma), 1) ;
  662. self->gaussFilterSigma = sigma ;
  663. self->gaussFilter = (vl_sift_pix*)vl_malloc (sizeof(vl_sift_pix) * (2 * self->gaussFilterWidth + 1)) ;
  664. for (j = 0 ; j < 2 * self->gaussFilterWidth + 1 ; ++j) {
  665. vl_sift_pix d = ((vl_sift_pix)((signed)j - (signed)self->gaussFilterWidth)) / ((vl_sift_pix)sigma) ;
  666. self->gaussFilter[j] = (vl_sift_pix) exp (- 0.5 * (d*d)) ;
  667. acc += self->gaussFilter[j] ;
  668. }
  669. for (j = 0 ; j < 2 * self->gaussFilterWidth + 1 ; ++j) {
  670. self->gaussFilter[j] /= acc ;
  671. }
  672. }
  673. if (self->gaussFilterWidth == 0) {
  674. memcpy (outputImage, inputImage, sizeof(vl_sift_pix) * width * height) ;
  675. return ;
  676. }
  677. vl_imconvcol_vf (tempImage, height,
  678. inputImage, width, height, width,
  679. self->gaussFilter,
  680. -((int)self->gaussFilterWidth), self->gaussFilterWidth,
  681. 1, VL_PAD_BY_CONTINUITY | VL_TRANSPOSE) ;
  682. vl_imconvcol_vf (outputImage, width,
  683. tempImage, height, width, height,
  684. self->gaussFilter,
  685. -((int)self->gaussFilterWidth), self->gaussFilterWidth,
  686. 1, VL_PAD_BY_CONTINUITY | VL_TRANSPOSE) ;
  687. }
  688. /** ------------------------------------------------------------------
  689. ** @internal
  690. ** @brief Copy and downsample an image
  691. **
  692. ** @param dst output imgae buffer.
  693. ** @param src input image buffer.
  694. ** @param width input image width.
  695. ** @param height input image height.
  696. ** @param d octaves (non negative).
  697. **
  698. ** The function downsamples the image @a d times, reducing it to @c
  699. ** 1/2^d of its original size. The parameters @a width and @a height
  700. ** are the size of the input image. The destination image @a dst is
  701. ** assumed to be <code>floor(width/2^d)</code> pixels wide and
  702. ** <code>floor(height/2^d)</code> pixels high.
  703. **/
  704. static void
  705. copy_and_downsample
  706. (vl_sift_pix *dst,
  707. vl_sift_pix const *src,
  708. int width, int height, int d)
  709. {
  710. int x, y ;
  711. d = 1 << d ; /* d = 2^d */
  712. for(y = 0 ; y < height ; y+=d) {
  713. vl_sift_pix const * srcrowp = src + y * width ;
  714. for(x = 0 ; x < width - (d-1) ; x+=d) {
  715. *dst++ = *srcrowp ;
  716. srcrowp += d ;
  717. }
  718. }
  719. }
  720. /** ------------------------------------------------------------------
  721. ** @brief Create a new SIFT filter
  722. **
  723. ** @param width image width.
  724. ** @param height image height.
  725. ** @param noctaves number of octaves.
  726. ** @param nlevels number of levels per octave.
  727. ** @param o_min first octave index.
  728. **
  729. ** The function allocates and returns a new SIFT filter for the
  730. ** specified image and scale space geometry.
  731. **
  732. ** Setting @a O to a negative value sets the number of octaves to the
  733. ** maximum possible value depending on the size of the image.
  734. **
  735. ** @return the new SIFT filter.
  736. ** @sa ::vl_sift_delete().
  737. **/
  738. VL_EXPORT
  739. VlSiftFilt *
  740. vl_sift_new (int width, int height,
  741. int noctaves, int nlevels,
  742. int o_min)
  743. {
  744. VlSiftFilt *f = (VlSiftFilt*)vl_malloc (sizeof(VlSiftFilt)) ;
  745. int w = VL_SHIFT_LEFT (width, -o_min) ;
  746. int h = VL_SHIFT_LEFT (height, -o_min) ;
  747. int nel = w * h ;
  748. /* negative value O => calculate max. value */
  749. if (noctaves < 0) {
  750. noctaves = VL_MAX (floor (log2 ((double)VL_MIN(width, height))) - o_min - 3, 1) ;
  751. }
  752. f-> width = width ;
  753. f-> height = height ;
  754. f-> O = noctaves ;
  755. f-> S = nlevels ;
  756. f-> o_min = o_min ;
  757. f-> s_min = -1 ;
  758. f-> s_max = nlevels + 1 ;
  759. f-> o_cur = o_min ;
  760. f-> temp = (vl_sift_pix*)vl_malloc (sizeof(vl_sift_pix) * nel ) ;
  761. f-> octave = (vl_sift_pix*)vl_malloc (sizeof(vl_sift_pix) * nel
  762. * (f->s_max - f->s_min + 1) ) ;
  763. f-> dog = (vl_sift_pix*)vl_malloc (sizeof(vl_sift_pix) * nel
  764. * (f->s_max - f->s_min ) ) ;
  765. f-> grad = (vl_sift_pix*)vl_malloc (sizeof(vl_sift_pix) * nel * 2
  766. * (f->s_max - f->s_min ) ) ;
  767. f-> sigman = 0.5 ;
  768. f-> sigmak = pow (2.0, 1.0 / nlevels) ;
  769. f-> sigma0 = 1.6 * f->sigmak ;
  770. f-> dsigma0 = f->sigma0 * sqrt (1.0 - 1.0 / (f->sigmak*f->sigmak)) ;
  771. f-> gaussFilter = NULL ;
  772. f-> gaussFilterSigma = 0 ;
  773. f-> gaussFilterWidth = 0 ;
  774. f-> octave_width = 0 ;
  775. f-> octave_height = 0 ;
  776. f-> keys = 0 ;
  777. f-> nkeys = 0 ;
  778. f-> keys_res = 0 ;
  779. f-> peak_thresh = 0.0 ;
  780. f-> edge_thresh = 10.0 ;
  781. f-> norm_thresh = 0.0 ;
  782. f-> magnif = 3.0 ;
  783. f-> windowSize = NBP / 2 ;
  784. f-> grad_o = o_min - 1 ;
  785. /* initialize fast_expn stuff */
  786. fast_expn_init () ;
  787. return f ;
  788. }
  789. /** -------------------------------------------------------------------
  790. ** @brief Delete SIFT filter
  791. **
  792. ** @param f SIFT filter to delete.
  793. **
  794. ** The function frees the resources allocated by ::vl_sift_new().
  795. **/
  796. VL_EXPORT
  797. void
  798. vl_sift_delete (VlSiftFilt* f)
  799. {
  800. if (f) {
  801. if (f->keys) vl_free (f->keys) ;
  802. if (f->grad) vl_free (f->grad) ;
  803. if (f->dog) vl_free (f->dog) ;
  804. if (f->octave) vl_free (f->octave) ;
  805. if (f->temp) vl_free (f->temp) ;
  806. if (f->gaussFilter) vl_free (f->gaussFilter) ;
  807. vl_free (f) ;
  808. }
  809. }
  810. /** ------------------------------------------------------------------
  811. ** @brief Start processing a new image
  812. **
  813. ** @param f SIFT filter.
  814. ** @param im image data.
  815. **
  816. ** The function starts processing a new image by computing its
  817. ** Gaussian scale space at the lower octave. It also empties the
  818. ** internal keypoint buffer.
  819. **
  820. ** @return error code. The function returns ::VL_ERR_EOF if there are
  821. ** no more octaves to process.
  822. **
  823. ** @sa ::vl_sift_process_next_octave().
  824. **/
  825. VL_EXPORT
  826. int
  827. vl_sift_process_first_octave (VlSiftFilt *f, vl_sift_pix const *im)
  828. {
  829. int o, s, h, w ;
  830. double sa, sb ;
  831. vl_sift_pix *octave ;
  832. /* shortcuts */
  833. vl_sift_pix *temp = f-> temp ;
  834. int width = f-> width ;
  835. int height = f-> height ;
  836. int o_min = f-> o_min ;
  837. int s_min = f-> s_min ;
  838. int s_max = f-> s_max ;
  839. double sigma0 = f-> sigma0 ;
  840. double sigmak = f-> sigmak ;
  841. double sigman = f-> sigman ;
  842. double dsigma0 = f-> dsigma0 ;
  843. /* restart from the first */
  844. f->o_cur = o_min ;
  845. f->nkeys = 0 ;
  846. w = f-> octave_width = VL_SHIFT_LEFT(f->width, - f->o_cur) ;
  847. h = f-> octave_height = VL_SHIFT_LEFT(f->height, - f->o_cur) ;
  848. /* is there at least one octave? */
  849. if (f->O == 0)
  850. return VL_ERR_EOF ;
  851. /* ------------------------------------------------------------------
  852. * Compute the first sublevel of the first octave
  853. * --------------------------------------------------------------- */
  854. /*
  855. * If the first octave has negative index, we upscale the image; if
  856. * the first octave has positive index, we downscale the image; if
  857. * the first octave has index zero, we just copy the image.
  858. */
  859. octave = vl_sift_get_octave (f, s_min) ;
  860. if (o_min < 0) {
  861. /* double once */
  862. copy_and_upsample_rows (temp, im, width, height) ;
  863. copy_and_upsample_rows (octave, temp, height, 2 * width ) ;
  864. /* double more */
  865. for(o = -1 ; o > o_min ; --o) {
  866. copy_and_upsample_rows (temp, octave,
  867. width << -o, height << -o ) ;
  868. copy_and_upsample_rows (octave, temp,
  869. width << -o, 2 * (height << -o)) ;
  870. }
  871. }
  872. else if (o_min > 0) {
  873. /* downsample */
  874. copy_and_downsample (octave, im, width, height, o_min) ;
  875. }
  876. else {
  877. /* direct copy */
  878. memcpy(octave, im, sizeof(vl_sift_pix) * width * height) ;
  879. }
  880. /*
  881. * Here we adjust the smoothing of the first level of the octave.
  882. * The input image is assumed to have nominal smoothing equal to
  883. * f->simgan.
  884. */
  885. sa = sigma0 * pow (sigmak, s_min) ;
  886. sb = sigman * pow (2.0, - o_min) ;
  887. if (sa > sb) {
  888. double sd = sqrt (sa*sa - sb*sb) ;
  889. _vl_sift_smooth (f, octave, temp, octave, w, h, sd) ;
  890. }
  891. /* -----------------------------------------------------------------
  892. * Compute the first octave
  893. * -------------------------------------------------------------- */
  894. for(s = s_min + 1 ; s <= s_max ; ++s) {
  895. double sd = dsigma0 * pow (sigmak, s) ;
  896. _vl_sift_smooth (f, vl_sift_get_octave(f, s), temp,
  897. vl_sift_get_octave(f, s - 1), w, h, sd) ;
  898. }
  899. return VL_ERR_OK ;
  900. }
  901. /** ------------------------------------------------------------------
  902. ** @brief Process next octave
  903. **
  904. ** @param f SIFT filter.
  905. **
  906. ** The function computes the next octave of the Gaussian scale space.
  907. ** Notice that this clears the record of any feature detected in the
  908. ** previous octave.
  909. **
  910. ** @return error code. The function returns the error
  911. ** ::VL_ERR_EOF when there are no more octaves to process.
  912. **
  913. ** @sa ::vl_sift_process_first_octave().
  914. **/
  915. VL_EXPORT
  916. int
  917. vl_sift_process_next_octave (VlSiftFilt *f)
  918. {
  919. int s, h, w, s_best ;
  920. double sa, sb ;
  921. vl_sift_pix *octave, *pt ;
  922. /* shortcuts */
  923. vl_sift_pix *temp = f-> temp ;
  924. int O = f-> O ;
  925. int S = f-> S ;
  926. int o_min = f-> o_min ;
  927. int s_min = f-> s_min ;
  928. int s_max = f-> s_max ;
  929. double sigma0 = f-> sigma0 ;
  930. double sigmak = f-> sigmak ;
  931. double dsigma0 = f-> dsigma0 ;
  932. /* is there another octave ? */
  933. if (f->o_cur == o_min + O - 1)
  934. return VL_ERR_EOF ;
  935. /* retrieve base */
  936. s_best = VL_MIN(s_min + S, s_max) ;
  937. w = vl_sift_get_octave_width (f) ;
  938. h = vl_sift_get_octave_height (f) ;
  939. pt = vl_sift_get_octave (f, s_best) ;
  940. octave = vl_sift_get_octave (f, s_min) ;
  941. /* next octave */
  942. copy_and_downsample (octave, pt, w, h, 1) ;
  943. f-> o_cur += 1 ;
  944. f-> nkeys = 0 ;
  945. w = f-> octave_width = VL_SHIFT_LEFT(f->width, - f->o_cur) ;
  946. h = f-> octave_height = VL_SHIFT_LEFT(f->height, - f->o_cur) ;
  947. sa = sigma0 * powf (sigmak, s_min ) ;
  948. sb = sigma0 * powf (sigmak, s_best - S) ;
  949. if (sa > sb) {
  950. double sd = sqrt (sa*sa - sb*sb) ;
  951. _vl_sift_smooth (f, octave, temp, octave, w, h, sd) ;
  952. }
  953. /* ------------------------------------------------------------------
  954. * Fill octave
  955. * --------------------------------------------------------------- */
  956. for(s = s_min + 1 ; s <= s_max ; ++s) {
  957. double sd = dsigma0 * pow (sigmak, s) ;
  958. _vl_sift_smooth (f, vl_sift_get_octave(f, s), temp,
  959. vl_sift_get_octave(f, s - 1), w, h, sd) ;
  960. }
  961. return VL_ERR_OK ;
  962. }
  963. /** ------------------------------------------------------------------
  964. ** @brief Detect keypoints
  965. **
  966. ** The function detect keypoints in the current octave filling the
  967. ** internal keypoint buffer. Keypoints can be retrieved by
  968. ** ::vl_sift_get_keypoints().
  969. **
  970. ** @param f SIFT filter.
  971. **/
  972. VL_EXPORT
  973. void
  974. vl_sift_detect (VlSiftFilt * f)
  975. {
  976. vl_sift_pix* dog = f-> dog ;
  977. int s_min = f-> s_min ;
  978. int s_max = f-> s_max ;
  979. int w = f-> octave_width ;
  980. int h = f-> octave_height ;
  981. double te = f-> edge_thresh ;
  982. double tp = f-> peak_thresh ;
  983. int const xo = 1 ; /* x-stride */
  984. int const yo = w ; /* y-stride */
  985. int const so = w * h ; /* s-stride */
  986. double xper = pow (2.0, f->o_cur) ;
  987. int x, y, s, i, ii, jj ;
  988. vl_sift_pix *pt, v ;
  989. VlSiftKeypoint *k ;
  990. /* clear current list */
  991. f-> nkeys = 0 ;
  992. /* compute difference of gaussian (DoG) */
  993. pt = f-> dog ;
  994. for (s = s_min ; s <= s_max - 1 ; ++s) {
  995. vl_sift_pix* src_a = vl_sift_get_octave (f, s ) ;
  996. vl_sift_pix* src_b = vl_sift_get_octave (f, s + 1) ;
  997. vl_sift_pix* end_a = src_a + w * h ;
  998. while (src_a != end_a) {
  999. *pt++ = *src_b++ - *src_a++ ;
  1000. }
  1001. }
  1002. /* -----------------------------------------------------------------
  1003. * Find local maxima of DoG
  1004. * -------------------------------------------------------------- */
  1005. /* start from dog [1,1,s_min+1] */
  1006. pt = dog + xo + yo + so ;
  1007. for(s = s_min + 1 ; s <= s_max - 2 ; ++s) {
  1008. for(y = 1 ; y < h - 1 ; ++y) {
  1009. for(x = 1 ; x < w - 1 ; ++x) {
  1010. v = *pt ;
  1011. #define CHECK_NEIGHBORS(CMP,SGN) \
  1012. ( v CMP ## = SGN 0.8 * tp && \
  1013. v CMP *(pt + xo) && \
  1014. v CMP *(pt - xo) && \
  1015. v CMP *(pt + so) && \
  1016. v CMP *(pt - so) && \
  1017. v CMP *(pt + yo) && \
  1018. v CMP *(pt - yo) && \
  1019. \
  1020. v CMP *(pt + yo + xo) && \
  1021. v CMP *(pt + yo - xo) && \
  1022. v CMP *(pt - yo + xo) && \
  1023. v CMP *(pt - yo - xo) && \
  1024. \
  1025. v CMP *(pt + xo + so) && \
  1026. v CMP *(pt - xo + so) && \
  1027. v CMP *(pt + yo + so) && \
  1028. v CMP *(pt - yo + so) && \
  1029. v CMP *(pt + yo + xo + so) && \
  1030. v CMP *(pt + yo - xo + so) && \
  1031. v CMP *(pt - yo + xo + so) && \
  1032. v CMP *(pt - yo - xo + so) && \
  1033. \
  1034. v CMP *(pt + xo - so) && \
  1035. v CMP *(pt - xo - so) && \
  1036. v CMP *(pt + yo - so) && \
  1037. v CMP *(pt - yo - so) && \
  1038. v CMP *(pt + yo + xo - so) && \
  1039. v CMP *(pt + yo - xo - so) && \
  1040. v CMP *(pt - yo + xo - so) && \
  1041. v CMP *(pt - yo - xo - so) )
  1042. if (CHECK_NEIGHBORS(>,+) ||
  1043. CHECK_NEIGHBORS(<,-) ) {
  1044. /* make room for more keypoints */
  1045. if (f->nkeys >= f->keys_res) {
  1046. f->keys_res += 500 ;
  1047. if (f->keys) {
  1048. f->keys = (VlSiftKeypoint*)vl_realloc (f->keys,
  1049. f->keys_res *
  1050. sizeof(VlSiftKeypoint)) ;
  1051. } else {
  1052. f->keys = (VlSiftKeypoint*)vl_malloc (f->keys_res *
  1053. sizeof(VlSiftKeypoint)) ;
  1054. }
  1055. }
  1056. k = f->keys + (f->nkeys ++) ;
  1057. k-> ix = x ;
  1058. k-> iy = y ;
  1059. k-> is = s ;
  1060. }
  1061. pt += 1 ;
  1062. }
  1063. pt += 2 ;
  1064. }
  1065. pt += 2 * yo ;
  1066. }
  1067. /* -----------------------------------------------------------------
  1068. * Refine local maxima
  1069. * -------------------------------------------------------------- */
  1070. /* this pointer is used to write the keypoints back */
  1071. k = f->keys ;
  1072. for (i = 0 ; i < f->nkeys ; ++i) {
  1073. int x = f-> keys [i] .ix ;
  1074. int y = f-> keys [i] .iy ;
  1075. int s = f-> keys [i]. is ;
  1076. double Dx=0,Dy=0,Ds=0,Dxx=0,Dyy=0,Dss=0,Dxy=0,Dxs=0,Dys=0 ;
  1077. double A [3*3], b [3] ;
  1078. int dx = 0 ;
  1079. int dy = 0 ;
  1080. int iter, i, j ;
  1081. for (iter = 0 ; iter < 5 ; ++iter) {
  1082. x += dx ;
  1083. y += dy ;
  1084. pt = dog
  1085. + xo * x
  1086. + yo * y
  1087. + so * (s - s_min) ;
  1088. /** @brief Index GSS @internal */
  1089. #define at(dx,dy,ds) (*( pt + (dx)*xo + (dy)*yo + (ds)*so))
  1090. /** @brief Index matrix A @internal */
  1091. #define Aat(i,j) (A[(i)+(j)*3])
  1092. /* compute the gradient */
  1093. Dx = 0.5 * (at(+1,0,0) - at(-1,0,0)) ;
  1094. Dy = 0.5 * (at(0,+1,0) - at(0,-1,0));
  1095. Ds = 0.5 * (at(0,0,+1) - at(0,0,-1)) ;
  1096. /* compute the Hessian */
  1097. Dxx = (at(+1,0,0) + at(-1,0,0) - 2.0 * at(0,0,0)) ;
  1098. Dyy = (at(0,+1,0) + at(0,-1,0) - 2.0 * at(0,0,0)) ;
  1099. Dss = (at(0,0,+1) + at(0,0,-1) - 2.0 * at(0,0,0)) ;
  1100. Dxy = 0.25 * ( at(+1,+1,0) + at(-1,-1,0) - at(-1,+1,0) - at(+1,-1,0) ) ;
  1101. Dxs = 0.25 * ( at(+1,0,+1) + at(-1,0,-1) - at(-1,0,+1) - at(+1,0,-1) ) ;
  1102. Dys = 0.25 * ( at(0,+1,+1) + at(0,-1,-1) - at(0,-1,+1) - at(0,+1,-1) ) ;
  1103. /* solve linear system ....................................... */
  1104. Aat(0,0) = Dxx ;
  1105. Aat(1,1) = Dyy ;
  1106. Aat(2,2) = Dss ;
  1107. Aat(0,1) = Aat(1,0) = Dxy ;
  1108. Aat(0,2) = Aat(2,0) = Dxs ;
  1109. Aat(1,2) = Aat(2,1) = Dys ;
  1110. b[0] = - Dx ;
  1111. b[1] = - Dy ;
  1112. b[2] = - Ds ;
  1113. /* Gauss elimination */
  1114. for(j = 0 ; j < 3 ; ++j) {
  1115. double maxa = 0 ;
  1116. double maxabsa = 0 ;
  1117. int maxi = -1 ;
  1118. double tmp ;
  1119. /* look for the maximally stable pivot */
  1120. for (i = j ; i < 3 ; ++i) {
  1121. double a = Aat (i,j) ;
  1122. double absa = vl_abs_d (a) ;
  1123. if (absa > maxabsa) {
  1124. maxa = a ;
  1125. maxabsa = absa ;
  1126. maxi = i ;
  1127. }
  1128. }
  1129. /* if singular give up */
  1130. if (maxabsa < 1e-10f) {
  1131. b[0] = 0 ;
  1132. b[1] = 0 ;
  1133. b[2] = 0 ;
  1134. break ;
  1135. }
  1136. i = maxi ;
  1137. /* swap j-th row with i-th row and normalize j-th row */
  1138. for(jj = j ; jj < 3 ; ++jj) {
  1139. tmp = Aat(i,jj) ; Aat(i,jj) = Aat(j,jj) ; Aat(j,jj) = tmp ;
  1140. Aat(j,jj) /= maxa ;
  1141. }
  1142. tmp = b[j] ; b[j] = b[i] ; b[i] = tmp ;
  1143. b[j] /= maxa ;
  1144. /* elimination */
  1145. for (ii = j+1 ; ii < 3 ; ++ii) {
  1146. double x = Aat(ii,j) ;
  1147. for (jj = j ; jj < 3 ; ++jj) {
  1148. Aat(ii,jj) -= x * Aat(j,jj) ;
  1149. }
  1150. b[ii] -= x * b[j] ;
  1151. }
  1152. }
  1153. /* backward substitution */
  1154. for (i = 2 ; i > 0 ; --i) {
  1155. double x = b[i] ;
  1156. for (ii = i-1 ; ii >= 0 ; --ii) {
  1157. b[ii] -= x * Aat(ii,i) ;
  1158. }
  1159. }
  1160. /* .......................................................... */
  1161. /* If the translation of the keypoint is big, move the keypoint
  1162. * and re-iterate the computation. Otherwise we are all set.
  1163. */
  1164. dx= ((b[0] > 0.6 && x < w - 2) ? 1 : 0)
  1165. + ((b[0] < -0.6 && x > 1 ) ? -1 : 0) ;
  1166. dy= ((b[1] > 0.6 && y < h - 2) ? 1 : 0)
  1167. + ((b[1] < -0.6 && y > 1 ) ? -1 : 0) ;
  1168. if (dx == 0 && dy == 0) break ;
  1169. }
  1170. /* check threshold and other conditions */
  1171. {
  1172. double val = at(0,0,0)
  1173. + 0.5 * (Dx * b[0] + Dy * b[1] + Ds * b[2]) ;
  1174. double score = (Dxx+Dyy)*(Dxx+Dyy) / (Dxx*Dyy - Dxy*Dxy) ;
  1175. double xn = x + b[0] ;
  1176. double yn = y + b[1] ;
  1177. double sn = s + b[2] ;
  1178. vl_bool good =
  1179. vl_abs_d (val) > tp &&
  1180. score < (te+1)*(te+1)/te &&
  1181. score >= 0 &&
  1182. vl_abs_d (b[0]) < 1.5 &&
  1183. vl_abs_d (b[1]) < 1.5 &&
  1184. vl_abs_d (b[2]) < 1.5 &&
  1185. xn >= 0 &&
  1186. xn <= w - 1 &&
  1187. yn >= 0 &&
  1188. yn <= h - 1 &&
  1189. sn >= s_min &&
  1190. sn <= s_max ;
  1191. if (good) {
  1192. k-> o = f->o_cur ;
  1193. k-> ix = x ;
  1194. k-> iy = y ;
  1195. k-> is = s ;
  1196. k-> s = sn ;
  1197. k-> x = xn * xper ;
  1198. k-> y = yn * xper ;
  1199. k-> sigma = f->sigma0 * pow (2.0, sn/f->S) * xper ;
  1200. ++ k ;
  1201. }
  1202. } /* done checking */
  1203. } /* next keypoint to refine */
  1204. /* update keypoint count */
  1205. f-> nkeys = (int)(k - f->keys) ;
  1206. }
  1207. /** ------------------------------------------------------------------
  1208. ** @internal
  1209. ** @brief Update gradients to current GSS octave
  1210. **
  1211. ** @param f SIFT filter.
  1212. **
  1213. ** The function makes sure that the gradient buffer is up-to-date
  1214. ** with the current GSS data.
  1215. **
  1216. ** @remark The minimum octave size is 2x2xS.
  1217. **/
  1218. static void
  1219. update_gradient (VlSiftFilt *f)
  1220. {
  1221. int s_min = f->s_min ;
  1222. int s_max = f->s_max ;
  1223. int w = vl_sift_get_octave_width (f) ;
  1224. int h = vl_sift_get_octave_height (f) ;
  1225. int const xo = 1 ;
  1226. int const yo = w ;
  1227. int const so = h * w ;
  1228. int y, s ;
  1229. if (f->grad_o == f->o_cur) return ;
  1230. for (s = s_min + 1 ;
  1231. s <= s_max - 2 ; ++ s) {
  1232. vl_sift_pix *src, *end, *grad, gx, gy ;
  1233. #define SAVE_BACK \
  1234. *grad++ = vl_fast_sqrt_f (gx*gx + gy*gy) ; \
  1235. *grad++ = vl_mod_2pi_f (vl_fast_atan2_f (gy, gx) + 2*VL_PI) ; \
  1236. ++src ; \
  1237. src = vl_sift_get_octave (f,s) ;
  1238. grad = f->grad + 2 * so * (s - s_min -1) ;
  1239. /* first pixel of the first row */
  1240. gx = src[+xo] - src[0] ;
  1241. gy = src[+yo] - src[0] ;
  1242. SAVE_BACK ;
  1243. /* middle pixels of the first row */
  1244. end = (src - 1) + w - 1 ;
  1245. while (src < end) {
  1246. gx = 0.5 * (src[+xo] - src[-xo]) ;
  1247. gy = src[+yo] - src[0] ;
  1248. SAVE_BACK ;
  1249. }
  1250. /* last pixel of the first row */
  1251. gx = src[0] - src[-xo] ;
  1252. gy = src[+yo] - src[0] ;
  1253. SAVE_BACK ;
  1254. for (y = 1 ; y < h -1 ; ++y) {
  1255. /* first pixel of the middle rows */
  1256. gx = src[+xo] - src[0] ;
  1257. gy = 0.5 * (src[+yo] - src[-yo]) ;
  1258. SAVE_BACK ;
  1259. /* middle pixels of the middle rows */
  1260. end = (src - 1) + w - 1 ;
  1261. while (src < end) {
  1262. gx = 0.5 * (src[+xo] - src[-xo]) ;
  1263. gy = 0.5 * (src[+yo] - src[-yo]) ;
  1264. SAVE_BACK ;
  1265. }
  1266. /* last pixel of the middle row */
  1267. gx = src[0] - src[-xo] ;
  1268. gy = 0.5 * (src[+yo] - src[-yo]) ;
  1269. SAVE_BACK ;
  1270. }
  1271. /* first pixel of the last row */
  1272. gx = src[+xo] - src[0] ;
  1273. gy = src[ 0] - src[-yo] ;
  1274. SAVE_BACK ;
  1275. /* middle pixels of the last row */
  1276. end = (src - 1) + w - 1 ;
  1277. while (src < end) {
  1278. gx = 0.5 * (src[+xo] - src[-xo]) ;
  1279. gy = src[0] - src[-yo] ;
  1280. SAVE_BACK ;
  1281. }
  1282. /* last pixel of the last row */
  1283. gx = src[0] - src[-xo] ;
  1284. gy = src[0] - src[-yo] ;
  1285. SAVE_BACK ;
  1286. }
  1287. f->grad_o = f->o_cur ;
  1288. }
  1289. /** ------------------------------------------------------------------
  1290. ** @brief Calculate the keypoint orientation(s)
  1291. **
  1292. ** @param f SIFT filter.
  1293. ** @param angles orientations (output).
  1294. ** @param k keypoint.
  1295. **
  1296. ** The function computes the orientation(s) of the keypoint @a k.
  1297. ** The function returns the number of orientations found (up to
  1298. ** four). The orientations themselves are written to the vector @a
  1299. ** angles.
  1300. **
  1301. ** @remark The function requires the keypoint octave @a k->o to be
  1302. ** equal to the filter current octave ::vl_sift_get_octave. If this
  1303. ** is not the case, the function returns zero orientations.
  1304. **
  1305. ** @remark The function requires the keypoint scale level @c k->s to
  1306. ** be in the range @c s_min+1 and @c s_max-2 (where usually @c
  1307. ** s_min=0 and @c s_max=S+2). If this is not the case, the function
  1308. ** returns zero orientations.
  1309. **
  1310. ** @return number of orientations found.
  1311. **/
  1312. VL_EXPORT
  1313. int
  1314. vl_sift_calc_keypoint_orientations (VlSiftFilt *f,
  1315. double angles [4],
  1316. VlSiftKeypoint const *k)
  1317. {
  1318. double const winf = 1.5 ;
  1319. double xper = pow (2.0, f->o_cur) ;
  1320. int w = f-> octave_width ;
  1321. int h = f-> octave_height ;
  1322. int const xo = 2 ; /* x-stride */
  1323. int const yo = 2 * w ; /* y-stride */
  1324. int const so = 2 * w * h ; /* s-stride */
  1325. double x = k-> x / xper ;
  1326. double y = k-> y / xper ;
  1327. double sigma = k-> sigma / xper ;
  1328. int xi = (int) (x + 0.5) ;
  1329. int yi = (int) (y + 0.5) ;
  1330. int si = k-> is ;
  1331. double const sigmaw = winf * sigma ;
  1332. int W = VL_MAX(floor (3.0 * sigmaw), 1) ;
  1333. int nangles= 0 ;
  1334. enum {nbins = 36} ;
  1335. double hist [nbins], maxh ;
  1336. vl_sift_pix const * pt ;
  1337. int xs, ys, iter, i ;
  1338. /* skip if the keypoint octave is not current */
  1339. if(k->o != f->o_cur)
  1340. return 0 ;
  1341. /* skip the keypoint if it is out of bounds */
  1342. if(xi < 0 ||
  1343. xi > w - 1 ||
  1344. yi < 0 ||
  1345. yi > h - 1 ||
  1346. si < f->s_min + 1 ||
  1347. si > f->s_max - 2 ) {
  1348. return 0 ;
  1349. }
  1350. /* make gradient up to date */
  1351. update_gradient (f) ;
  1352. /* clear histogram */
  1353. memset (hist, 0, sizeof(double) * nbins) ;
  1354. /* compute orientation histogram */
  1355. pt = f-> grad + xo*xi + yo*yi + so*(si - f->s_min - 1) ;
  1356. #undef at
  1357. #define at(dx,dy) (*(pt + xo * (dx) + yo * (dy)))
  1358. for(ys = VL_MAX (- W, - yi) ;
  1359. ys <= VL_MIN (+ W, h - 1 - yi) ; ++ys) {
  1360. for(xs = VL_MAX (- W, - xi) ;
  1361. xs <= VL_MIN (+ W, w - 1 - xi) ; ++xs) {
  1362. double dx = (double)(xi + xs) - x;
  1363. double dy = (double)(yi + ys) - y;
  1364. double r2 = dx*dx + dy*dy ;
  1365. double wgt, mod, ang, fbin ;
  1366. /* limit to a circular window */
  1367. if (r2 >= W*W + 0.6) continue ;
  1368. wgt = fast_expn (r2 / (2*sigmaw*sigmaw)) ;
  1369. mod = *(pt + xs*xo + ys*yo ) ;
  1370. ang = *(pt + xs*xo + ys*yo + 1) ;
  1371. fbin = nbins * ang / (2 * VL_PI) ;
  1372. #if defined(VL_SIFT_BILINEAR_ORIENTATIONS)
  1373. {
  1374. int bin = (int) vl_floor_d (fbin - 0.5) ;
  1375. double rbin = fbin - bin - 0.5 ;
  1376. hist [(bin + nbins) % nbins] += (1 - rbin) * mod * wgt ;
  1377. hist [(bin + 1 ) % nbins] += ( rbin) * mod * wgt ;
  1378. }
  1379. #else
  1380. {
  1381. int bin = vl_floor_d (fbin) ;
  1382. bin = vl_floor_d (nbins * ang / (2*VL_PI)) ;
  1383. hist [(bin) % nbins] += mod * wgt ;
  1384. }
  1385. #endif
  1386. } /* for xs */
  1387. } /* for ys */
  1388. /* smooth histogram */
  1389. for (iter = 0; iter < 6; iter ++) {
  1390. double prev = hist [nbins - 1] ;
  1391. double first = hist [0] ;
  1392. int i ;
  1393. for (i = 0; i < nbins - 1; i++) {
  1394. double newh = (prev + hist[i] + hist[(i+1) % nbins]) / 3.0;
  1395. prev = hist[i] ;
  1396. hist[i] = newh ;
  1397. }
  1398. hist[i] = (prev + hist[i] + first) / 3.0 ;
  1399. }
  1400. /* find the histogram maximum */
  1401. maxh = 0 ;
  1402. for (i = 0 ; i < nbins ; ++i)
  1403. maxh = VL_MAX (maxh, hist [i]) ;
  1404. /* find peaks within 80% from max */
  1405. nangles = 0 ;
  1406. for(i = 0 ; i < nbins ; ++i) {
  1407. double h0 = hist [i] ;
  1408. double hm = hist [(i - 1 + nbins) % nbins] ;
  1409. double hp = hist [(i + 1 + nbins) % nbins] ;
  1410. /* is this a peak? */
  1411. if (h0 > 0.8*maxh && h0 > hm && h0 > hp) {
  1412. /* quadratic interpolation */
  1413. double di = - 0.5 * (hp - hm) / (hp + hm - 2 * h0) ;
  1414. double th = 2 * VL_PI * (i + di + 0.5) / nbins ;
  1415. angles [ nangles++ ] = th ;
  1416. if( nangles == 4 )
  1417. goto enough_angles ;
  1418. }
  1419. }
  1420. enough_angles:
  1421. return nangles ;
  1422. }
  1423. /** ------------------------------------------------------------------
  1424. ** @internal
  1425. ** @brief Normalizes in norm L_2 a descriptor
  1426. ** @param begin begin of histogram.
  1427. ** @param end end of histogram.
  1428. **/
  1429. VL_INLINE vl_sift_pix
  1430. normalize_histogram
  1431. (vl_sift_pix *begin, vl_sift_pix *end)
  1432. {
  1433. vl_sift_pix* iter ;
  1434. vl_sift_pix norm = 0.0 ;
  1435. for (iter = begin ; iter != end ; ++ iter)
  1436. norm += (*iter) * (*iter) ;
  1437. norm = vl_fast_sqrt_f (norm) + VL_EPSILON_F ;
  1438. for (iter = begin; iter != end ; ++ iter)
  1439. *iter /= norm ;
  1440. return norm;
  1441. }
  1442. /** ------------------------------------------------------------------
  1443. ** @brief Run the SIFT descriptor on raw data
  1444. **
  1445. ** @param f SIFT filter.
  1446. ** @param grad image gradients.
  1447. ** @param descr SIFT descriptor (output).
  1448. ** @param width image width.
  1449. ** @param height image height.
  1450. ** @param x keypoint x coordinate.
  1451. ** @param y keypoint y coordinate.
  1452. ** @param sigma keypoint scale.
  1453. ** @param angle0 keypoint orientation.
  1454. **
  1455. ** The function runs the SIFT descriptor on raw data. Here @a image
  1456. ** is a 2 x @a width x @a height array (by convention, the memory
  1457. ** layout is a s such the first index is the fastest varying
  1458. ** one). The first @a width x @a height layer of the array contains
  1459. ** the gradient magnitude and the second the gradient angle (in
  1460. ** radians, between 0 and @f$ 2\pi @f$). @a x, @a y and @a sigma give
  1461. ** the keypoint center and scale respectively.
  1462. **
  1463. ** In order to be equivalent to a standard SIFT descriptor the image
  1464. ** gradient must be computed at a smoothing level equal to the scale
  1465. ** of the keypoint. In practice, the actual SIFT algorithm makes the
  1466. ** following additional approximation, which influence the result:
  1467. **
  1468. ** - Scale is discretized in @c S levels.
  1469. ** - The image is downsampled once for each octave (if you do this,
  1470. ** the parameters @a x, @a y and @a sigma must be
  1471. ** scaled too).
  1472. **/
  1473. VL_EXPORT
  1474. void
  1475. vl_sift_calc_raw_descriptor (VlSiftFilt const *f,
  1476. vl_sift_pix const* grad,
  1477. vl_sift_pix *descr,
  1478. int width, int height,
  1479. double x, double y,
  1480. double sigma,
  1481. double angle0)
  1482. {
  1483. double const magnif = f-> magnif ;
  1484. int w = width ;
  1485. int h = height ;
  1486. int const xo = 2 ; /* x-stride */
  1487. int const yo = 2 * w ; /* y-stride */
  1488. int xi = (int) (x + 0.5) ;
  1489. int yi = (int) (y + 0.5) ;
  1490. double const st0 = sin (angle0) ;
  1491. double const ct0 = cos (angle0) ;
  1492. double const SBP = magnif * sigma + VL_EPSILON_D ;
  1493. int const W = floor
  1494. (sqrt(2.0) * SBP * (NBP + 1) / 2.0 + 0.5) ;
  1495. int const binto = 1 ; /* bin theta-stride */
  1496. int const binyo = NBO * NBP ; /* bin y-stride */
  1497. int const binxo = NBO ; /* bin x-stride */
  1498. int bin, dxi, dyi ;
  1499. vl_sift_pix const *pt ;
  1500. vl_sift_pix *dpt ;
  1501. /* check bounds */
  1502. if(xi < 0 ||
  1503. xi >= w ||
  1504. yi < 0 ||
  1505. yi >= h - 1 )
  1506. return ;
  1507. /* clear descriptor */
  1508. memset (descr, 0, sizeof(vl_sift_pix) * NBO*NBP*NBP) ;
  1509. /* Center the scale space and the descriptor on the current keypoint.
  1510. * Note that dpt is pointing to the bin of center (SBP/2,SBP/2,0).
  1511. */
  1512. pt = grad + xi*xo + yi*yo ;
  1513. dpt = descr + (NBP/2) * binyo + (NBP/2) * binxo ;
  1514. #undef atd
  1515. #define atd(dbinx,dbiny,dbint) *(dpt + (dbint)*binto + (dbiny)*binyo + (dbinx)*binxo)
  1516. /*
  1517. * Process pixels in the intersection of the image rectangle
  1518. * (1,1)-(M-1,N-1) and the keypoint bounding box.
  1519. */
  1520. for(dyi = VL_MAX(- W, - yi ) ;
  1521. dyi <= VL_MIN(+ W, h - yi -1) ; ++ dyi) {
  1522. for(dxi = VL_MAX(- W, - xi ) ;
  1523. dxi <= VL_MIN(+ W, w - xi -1) ; ++ dxi) {
  1524. /* retrieve */
  1525. vl_sift_pix mod = *( pt + dxi*xo + dyi*yo + 0 ) ;
  1526. vl_sift_pix angle = *( pt + dxi*xo + dyi*yo + 1 ) ;
  1527. vl_sift_pix theta = vl_mod_2pi_f (angle - angle0) ;
  1528. /* fractional displacement */
  1529. vl_sift_pix dx = xi + dxi - x;
  1530. vl_sift_pix dy = yi + dyi - y;
  1531. /* get the displacement normalized w.r.t. the keypoint
  1532. orientation and extension */
  1533. vl_sift_pix nx = ( ct0 * dx + st0 * dy) / SBP ;
  1534. vl_sift_pix ny = (-st0 * dx + ct0 * dy) / SBP ;
  1535. vl_sift_pix nt = NBO * theta / (2 * VL_PI) ;
  1536. /* Get the Gaussian weight of the sample. The Gaussian window
  1537. * has a standard deviation equal to NBP/2. Note that dx and dy
  1538. * are in the normalized frame, so that -NBP/2 <= dx <=
  1539. * NBP/2. */
  1540. vl_sift_pix const wsigma = f->windowSize ;
  1541. vl_sift_pix win = fast_expn
  1542. ((nx*nx + ny*ny)/(2.0 * wsigma * wsigma)) ;
  1543. /* The sample will be distributed in 8 adjacent bins.
  1544. We start from the ``lower-left'' bin. */
  1545. int binx = (int)vl_floor_f (nx - 0.5) ;
  1546. int biny = (int)vl_floor_f (ny - 0.5) ;
  1547. int bint = (int)vl_floor_f (nt) ;
  1548. vl_sift_pix rbinx = nx - (binx + 0.5) ;
  1549. vl_sift_pix rbiny = ny - (biny + 0.5) ;
  1550. vl_sift_pix rbint = nt - bint ;
  1551. int dbinx ;
  1552. int dbiny ;
  1553. int dbint ;
  1554. /* Distribute the current sample into the 8 adjacent bins*/
  1555. for(dbinx = 0 ; dbinx < 2 ; ++dbinx) {
  1556. for(dbiny = 0 ; dbiny < 2 ; ++dbiny) {
  1557. for(dbint = 0 ; dbint < 2 ; ++dbint) {
  1558. if (binx + dbinx >= - (NBP/2) &&
  1559. binx + dbinx < (NBP/2) &&
  1560. biny + dbiny >= - (NBP/2) &&
  1561. biny + dbiny < (NBP/2) ) {
  1562. vl_sift_pix weight = win
  1563. * mod
  1564. * vl_abs_f (1 - dbinx - rbinx)
  1565. * vl_abs_f (1 - dbiny - rbiny)
  1566. * vl_abs_f (1 - dbint - rbint) ;
  1567. atd(binx+dbinx, biny+dbiny, (bint+dbint) % NBO) += weight ;
  1568. }
  1569. }
  1570. }
  1571. }
  1572. }
  1573. }
  1574. /* Standard SIFT descriptors are normalized, truncated and normalized again */
  1575. if(1) {
  1576. /* normalize L2 norm */
  1577. vl_sift_pix norm = normalize_histogram (descr, descr + NBO*NBP*NBP) ;
  1578. /*
  1579. Set the descriptor to zero if it is lower than our
  1580. norm_threshold. We divide by the number of samples in the
  1581. descriptor region because the Gaussian window used in the
  1582. calculation of the descriptor is not normalized.
  1583. */
  1584. int numSamples =
  1585. (VL_MIN(W, w - xi -1) - VL_MAX(-W, - xi) + 1) *
  1586. (VL_MIN(W, h - yi -1) - VL_MAX(-W, - yi) + 1) ;
  1587. if(f-> norm_thresh && norm < f-> norm_thresh * numSamples) {
  1588. for(bin = 0; bin < NBO*NBP*NBP ; ++ bin)
  1589. descr [bin] = 0;
  1590. }
  1591. else {
  1592. /* truncate at 0.2. */
  1593. for(bin = 0; bin < NBO*NBP*NBP ; ++ bin) {
  1594. if (descr [bin] > 0.2) descr [bin] = 0.2;
  1595. }
  1596. /* normalize again. */
  1597. normalize_histogram (descr, descr + NBO*NBP*NBP) ;
  1598. }
  1599. }
  1600. }
  1601. /** ------------------------------------------------------------------
  1602. ** @brief Compute the descriptor of a keypoint
  1603. **
  1604. ** @param f SIFT filter.
  1605. ** @param descr SIFT descriptor (output)
  1606. ** @param k keypoint.
  1607. ** @param angle0 keypoint direction.
  1608. **
  1609. ** The function computes the SIFT descriptor of the keypoint @a k of
  1610. ** orientation @a angle0. The function fills the buffer @a descr
  1611. ** which must be large enough to hold the descriptor.
  1612. **
  1613. ** The function assumes that the keypoint is on the current octave.
  1614. ** If not, it does not do anything.
  1615. **/
  1616. VL_EXPORT
  1617. void
  1618. vl_sift_calc_keypoint_descriptor (VlSiftFilt *f,
  1619. vl_sift_pix *descr,
  1620. VlSiftKeypoint const* k,
  1621. double angle0)
  1622. {
  1623. /*
  1624. The SIFT descriptor is a three dimensional histogram of the
  1625. position and orientation of the gradient. There are NBP bins for
  1626. each spatial dimension and NBO bins for the orientation dimension,
  1627. for a total of NBP x NBP x NBO bins.
  1628. The support of each spatial bin has an extension of SBP = 3sigma
  1629. pixels, where sigma is the scale of the keypoint. Thus all the
  1630. bins together have a support SBP x NBP pixels wide. Since
  1631. weighting and interpolation of pixel is used, the support extends
  1632. by another half bin. Therefore, the support is a square window of
  1633. SBP x (NBP + 1) pixels. Finally, since the patch can be
  1634. arbitrarily rotated, we need to consider a window 2W += sqrt(2) x
  1635. SBP x (NBP + 1) pixels wide.
  1636. */
  1637. double const magnif = f-> magnif ;
  1638. double xper = pow (2.0, f->o_cur) ;
  1639. int w = f-> octave_width ;
  1640. int h = f-> octave_height ;
  1641. int const xo = 2 ; /* x-stride */
  1642. int const yo = 2 * w ; /* y-stride */
  1643. int const so = 2 * w * h ; /* s-stride */
  1644. double x = k-> x / xper ;
  1645. double y = k-> y / xper ;
  1646. double sigma = k-> sigma / xper ;
  1647. int xi = (int) (x + 0.5) ;
  1648. int yi = (int) (y + 0.5) ;
  1649. int si = k-> is ;
  1650. double const st0 = sin (angle0) ;
  1651. double const ct0 = cos (angle0) ;
  1652. double const SBP = magnif * sigma + VL_EPSILON_D ;
  1653. int const W = floor
  1654. (sqrt(2.0) * SBP * (NBP + 1) / 2.0 + 0.5) ;
  1655. int const binto = 1 ; /* bin theta-stride */
  1656. int const binyo = NBO * NBP ; /* bin y-stride */
  1657. int const binxo = NBO ; /* bin x-stride */
  1658. int bin, dxi, dyi ;
  1659. vl_sift_pix const *pt ;
  1660. vl_sift_pix *dpt ;
  1661. /* check bounds */
  1662. if(k->o != f->o_cur ||
  1663. xi < 0 ||
  1664. xi >= w ||
  1665. yi < 0 ||
  1666. yi >= h - 1 ||
  1667. si < f->s_min + 1 ||
  1668. si > f->s_max - 2 )
  1669. return ;
  1670. /* synchronize gradient buffer */
  1671. update_gradient (f) ;
  1672. /* VL_PRINTF("W = %d ; magnif = %g ; SBP = %g\n", W,magnif,SBP) ; */
  1673. /* clear descriptor */
  1674. memset (descr, 0, sizeof(vl_sift_pix) * NBO*NBP*NBP) ;
  1675. /* Center the scale space and the descriptor on the current keypoint.
  1676. * Note that dpt is pointing to the bin of center (SBP/2,SBP/2,0).
  1677. */
  1678. pt = f->grad + xi*xo + yi*yo + (si - f->s_min - 1)*so ;
  1679. dpt = descr + (NBP/2) * binyo + (NBP/2) * binxo ;
  1680. #undef atd
  1681. #define atd(dbinx,dbiny,dbint) *(dpt + (dbint)*binto + (dbiny)*binyo + (dbinx)*binxo)
  1682. /*
  1683. * Process pixels in the intersection of the image rectangle
  1684. * (1,1)-(M-1,N-1) and the keypoint bounding box.
  1685. */
  1686. for(dyi = VL_MAX (- W, 1 - yi ) ;
  1687. dyi <= VL_MIN (+ W, h - yi - 2) ; ++ dyi) {
  1688. for(dxi = VL_MAX (- W, 1 - xi ) ;
  1689. dxi <= VL_MIN (+ W, w - xi - 2) ; ++ dxi) {
  1690. /* retrieve */
  1691. vl_sift_pix mod = *( pt + dxi*xo + dyi*yo + 0 ) ;
  1692. vl_sift_pix angle = *( pt + dxi*xo + dyi*yo + 1 ) ;
  1693. vl_sift_pix theta = vl_mod_2pi_f (angle - angle0) ;
  1694. /* fractional displacement */
  1695. vl_sift_pix dx = xi + dxi - x;
  1696. vl_sift_pix dy = yi + dyi - y;
  1697. /* get the displacement normalized w.r.t. the keypoint
  1698. orientation and extension */
  1699. vl_sift_pix nx = ( ct0 * dx + st0 * dy) / SBP ;
  1700. vl_sift_pix ny = (-st0 * dx + ct0 * dy) / SBP ;
  1701. vl_sift_pix nt = NBO * theta / (2 * VL_PI) ;
  1702. /* Get the Gaussian weight of the sample. The Gaussian window
  1703. * has a standard deviation equal to NBP/2. Note that dx and dy
  1704. * are in the normalized frame, so that -NBP/2 <= dx <=
  1705. * NBP/2. */
  1706. vl_sift_pix const wsigma = f->windowSize ;
  1707. vl_sift_pix win = fast_expn
  1708. ((nx*nx + ny*ny)/(2.0 * wsigma * wsigma)) ;
  1709. /* The sample will be distributed in 8 adjacent bins.
  1710. We start from the ``lower-left'' bin. */
  1711. int binx = (int)vl_floor_f (nx - 0.5) ;
  1712. int biny = (int)vl_floor_f (ny - 0.5) ;
  1713. int bint = (int)vl_floor_f (nt) ;
  1714. vl_sift_pix rbinx = nx - (binx + 0.5) ;
  1715. vl_sift_pix rbiny = ny - (biny + 0.5) ;
  1716. vl_sift_pix rbint = nt - bint ;
  1717. int dbinx ;
  1718. int dbiny ;
  1719. int dbint ;
  1720. /* Distribute the current sample into the 8 adjacent bins*/
  1721. for(dbinx = 0 ; dbinx < 2 ; ++dbinx) {
  1722. for(dbiny = 0 ; dbiny < 2 ; ++dbiny) {
  1723. for(dbint = 0 ; dbint < 2 ; ++dbint) {
  1724. if (binx + dbinx >= - (NBP/2) &&
  1725. binx + dbinx < (NBP/2) &&
  1726. biny + dbiny >= - (NBP/2) &&
  1727. biny + dbiny < (NBP/2) ) {
  1728. vl_sift_pix weight = win
  1729. * mod
  1730. * vl_abs_f (1 - dbinx - rbinx)
  1731. * vl_abs_f (1 - dbiny - rbiny)
  1732. * vl_abs_f (1 - dbint - rbint) ;
  1733. atd(binx+dbinx, biny+dbiny, (bint+dbint) % NBO) += weight ;
  1734. }
  1735. }
  1736. }
  1737. }
  1738. }
  1739. }
  1740. /* Standard SIFT descriptors are normalized, truncated and normalized again */
  1741. if(1) {
  1742. /* Normalize the histogram to L2 unit length. */
  1743. vl_sift_pix norm = normalize_histogram (descr, descr + NBO*NBP*NBP) ;
  1744. /* Set the descriptor to zero if it is lower than our norm_threshold */
  1745. if(f-> norm_thresh && norm < f-> norm_thresh) {
  1746. for(bin = 0; bin < NBO*NBP*NBP ; ++ bin)
  1747. descr [bin] = 0;
  1748. }
  1749. else {
  1750. /* Truncate at 0.2. */
  1751. for(bin = 0; bin < NBO*NBP*NBP ; ++ bin) {
  1752. if (descr [bin] > 0.2) descr [bin] = 0.2;
  1753. }
  1754. /* Normalize again. */
  1755. normalize_histogram (descr, descr + NBO*NBP*NBP) ;
  1756. }
  1757. }
  1758. }
  1759. /** ------------------------------------------------------------------
  1760. ** @brief Initialize a keypoint from its position and scale
  1761. **
  1762. ** @param f SIFT filter.
  1763. ** @param k SIFT keypoint (output).
  1764. ** @param x x coordinate of the keypoint center.
  1765. ** @param y y coordinate of the keypoint center.
  1766. ** @param sigma keypoint scale.
  1767. **
  1768. ** The function initializes a keypoint structure @a k from
  1769. ** the location @a x
  1770. ** and @a y and the scale @a sigma of the keypoint. The keypoint structure
  1771. ** maps the keypoint to an octave and scale level of the discretized
  1772. ** Gaussian scale space, which is required for instance to compute the
  1773. ** keypoint SIFT descriptor.
  1774. **
  1775. ** @par Algorithm
  1776. **
  1777. ** The formula linking the keypoint scale sigma to the octave and
  1778. ** scale indexes is
  1779. **
  1780. ** @f[ \sigma(o,s) = \sigma_0 2^{o+s/S} @f]
  1781. **
  1782. ** In addition to the scale index @e s (which can be fractional due
  1783. ** to scale interpolation) a keypoint has an integer scale index @e
  1784. ** is too (which is the index of the scale level where it was
  1785. ** detected in the DoG scale space). We have the constraints (@ref
  1786. ** sift-tech-detector see also the "SIFT detector"):
  1787. **
  1788. ** - @e o is integer in the range @f$ [o_\mathrm{min},
  1789. ** o_{\mathrm{min}}+O-1] @f$.
  1790. ** - @e is is integer in the range @f$ [s_\mathrm{min}+1,
  1791. ** s_\mathrm{max}-2] @f$. This depends on how the scale is
  1792. ** determined during detection, and must be so here because
  1793. ** gradients are computed only for this range of scale levels
  1794. ** and are required for the calculation of the SIFT descriptor.
  1795. ** - @f$ |s - is| < 0.5 @f$ for detected keypoints in most cases due
  1796. ** to the interpolation technique used during detection. However
  1797. ** this is not necessary.
  1798. **
  1799. ** Thus octave o represents scales @f$ \{ \sigma(o, s) : s \in
  1800. ** [s_\mathrm{min}+1-.5, s_\mathrm{max}-2+.5] \} @f$. Note that some
  1801. ** scales may be represented more than once. For each scale, we
  1802. ** select the largest possible octave that contains it, i.e.
  1803. **
  1804. ** @f[
  1805. ** o(\sigma)
  1806. ** = \max \{ o \in \mathbb{Z} :
  1807. ** \sigma_0 2^{\frac{s_\mathrm{min}+1-.5}{S}} \leq \sigma \}
  1808. ** = \mathrm{floor}\,\left[
  1809. ** \log_2(\sigma / \sigma_0) - \frac{s_\mathrm{min}+1-.5}{S}\right]
  1810. ** @f]
  1811. **
  1812. ** and then
  1813. **
  1814. ** @f[
  1815. ** s(\sigma) = S \left[\log_2(\sigma / \sigma_0) - o(\sigma)\right],
  1816. ** \quad
  1817. ** is(\sigma) = \mathrm{round}\,(s(\sigma))
  1818. ** @f]
  1819. **
  1820. ** In practice, both @f$ o(\sigma) @f$ and @f$ is(\sigma) @f$ are
  1821. ** clamped to their feasible range as determined by the SIFT filter
  1822. ** parameters.
  1823. **/
  1824. VL_EXPORT
  1825. void
  1826. vl_sift_keypoint_init (VlSiftFilt const *f,
  1827. VlSiftKeypoint *k,
  1828. double x,
  1829. double y,
  1830. double sigma)
  1831. {
  1832. int o, ix, iy, is ;
  1833. double s, phi, xper ;
  1834. phi = log2 ((sigma + VL_EPSILON_D) / f->sigma0) ;
  1835. o = (int)vl_floor_d (phi - ((double) f->s_min + 0.5) / f->S) ;
  1836. o = VL_MIN (o, f->o_min + f->O - 1) ;
  1837. o = VL_MAX (o, f->o_min ) ;
  1838. s = f->S * (phi - o) ;
  1839. is = (int)(s + 0.5) ;
  1840. is = VL_MIN(is, f->s_max - 2) ;
  1841. is = VL_MAX(is, f->s_min + 1) ;
  1842. xper = pow (2.0, o) ;
  1843. ix = (int)(x / xper + 0.5) ;
  1844. iy = (int)(y / xper + 0.5) ;
  1845. k -> o = o ;
  1846. k -> ix = ix ;
  1847. k -> iy = iy ;
  1848. k -> is = is ;
  1849. k -> x = x ;
  1850. k -> y = y ;
  1851. k -> s = s ;
  1852. k->sigma = sigma ;
  1853. }