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.

641 lines
21 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /** @file liop.c
  2. ** @brief Local Intensity Order Pattern (LIOP) descriptor - Definition
  3. ** @author Hana Sarbortova
  4. ** @author Andrea Vedaldi
  5. **/
  6. /*
  7. Copyright (C) 2013 Hana Sarbortova and Andrea Vedaldi.
  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. /**
  13. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  14. @page liop Local Intensity Order Pattern (LIOP) descriptor
  15. @author Hana Sarbortova
  16. @author Andrea Vedaldi
  17. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  18. @ref liop.h implements *Local Intensity Order Pattern descriptor*
  19. (LIOP) of @cite{wang11local}. LIOP is a local image descriptor,
  20. similarly to the @ref sift "SIFT descriptor".
  21. @ref liop-starting demonstrates how to use the C API to compute the
  22. LIOP descriptor of a patch. For further details refer to:
  23. - @subpage liop-fundamentals - LIOP definition and parameters.
  24. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  25. @section liop-starting Getting started with LIOP
  26. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  27. The following code fragment demonstrates how tow to use @ref liop.h in
  28. a C program in order to compute the LIOP descriptor of an image patch.
  29. @code
  30. #include <vl/liop.h>
  31. // Create a new object instance (these numbers corresponds to parameter
  32. // values proposed by authors of the paper, except for 41)
  33. vl_size sideLength = 41 ;
  34. VlLiopDesc * liop = vl_liopdesc_new_basic (sideLength);
  35. // allocate the descriptor array
  36. vl_size dimension = vl_liopdesc_get_dimension(liop) ;
  37. float * desc = vl_malloc(sizeof(float) * dimension) ;
  38. // compute descriptor from a patch (an array of length sideLegnth *
  39. // sideLength)
  40. vl_liopdesc_process(liop, desc, patch) ;
  41. // delete the object
  42. vl_liopdesc_delete(liop) ;
  43. @endcode
  44. The image patch must be of odd side length and in single
  45. precision. There are several parameters affecting the LIOP
  46. descriptor. An example is the @ref liop-weighing "threshold" used to
  47. discard low-contrast oder pattern in the computation of the
  48. statistics. This is changed by using ::vl_liopdesc_set_intensity_threshold.
  49. **/
  50. /**
  51. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  52. @page liop-fundamentals LIOP fundamentals
  53. @tableofcontents
  54. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  55. The *Local Invariant Order Pattern* (LIOP) descriptor
  56. @cite{wang11local} is a local image descriptor based on the concept of
  57. *local order pattern*. An order pattern is simply the order obtained
  58. by sorting selected image samples by increasing intensity. Consider in
  59. particular a pixel $\bx$ and $n$ neighbors
  60. $\bx_1,\bx_2,\dots,\bx_n$. The local order pattern at $\bx$ is the
  61. permutation $\sigma$ that sorts the neighbours by increasing intensity
  62. $I(\bx_{\sigma(1)}) \leq I(\bx_{\sigma(2)}) \leq \dots \leq
  63. I(\bx_{\sigma(2)})$.
  64. An advantage of order patterns is that they are invariant to monotonic
  65. changes of the image intensity. However, an order pattern describes
  66. only a small portion of a patch and is not very distinctive. LIOP
  67. assembles local order patterns computed at all image locations to
  68. obtain a descriptor that at the same time distinctive and invariant to
  69. monotonic intensity changes as well as image rotations.
  70. In order to make order patterns rotation invariant, the neighborhood
  71. of samples around $\bx$ is taken in a rotation-covariant manner. In
  72. particular, the points $\bx_1,\dots,\bx_n$ are sampled anticlockwise
  73. on a circle of radius $r$ around $\bx$, as shown in the following
  74. figure:
  75. @image html liop.png "LIOP descriptor layout: square input patch (shaded area), circular measurement region (white area), local neighborhood of a point (blue)."
  76. Since the sample points do not necessarily have integer coordinates,
  77. $I(\bx_i)$ is computed using bilinear interpolation.
  78. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  79. @section liop-spatial-binning Intensity rank spatial binning
  80. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  81. Once local order patterns are computed for all pixels $\bx$ in the
  82. image, they can be pooled into a histogram to form an image
  83. descriptor. Pooling discards spatial information resulting in a
  84. warp-invariant statistics. In practice, there are two restriction on
  85. which pixels can be used for this purpose:
  86. - A margin of $r$ pixels from the image boundary must be maintained so
  87. that neighborhoods fall within the image boundaries.
  88. - Rotation invariance requires the pooling regions to be rotation
  89. co-variant. A way to do so is to make the shape of the pooling
  90. region rotation invariant.
  91. For this reason, the histogram pooling region is restricted to the
  92. circular region shown with a light color in the figure above.
  93. In order to increase distinctiveness of the descriptor, LIOP pools
  94. multiple histograms from a number of regions $R_1,\dots,R_m$ (spatial
  95. pooling). These regions are selected in an illumination-invariant and
  96. rotation-covariant manner by looking at level sets:
  97. \[
  98. R_t = \{\bx :\tau_{t} \leq I(\bx) < \tau_{t+1} \}.
  99. \]
  100. In order to be invariant to monotonic changes of the intensity, the
  101. thresholds $\tau_t$ are selected so that all regions contain the same
  102. number of pixels. This can be done efficiently by sorting pixels by
  103. increasing intensity and then partitioning the resulting list into $m$
  104. equal parts (when $m$ does not divide the number of pixels exactly,
  105. the remaining pixels are incorporated into the last partition).
  106. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  107. @section liop-weighing Weighted pooling
  108. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  109. In order to compute a histogram of order pattern occurrences, one
  110. needs to map permutations to histogram bins. This is obtained by
  111. sorting permutation in lexycogrpahical order. For example, for $n=4$
  112. neighbors one has the following $n!=24$ permutations:
  113. Permutation | Lexycographical rank
  114. --------------|----------------------
  115. 1 2 3 4 | 1
  116. 1 2 4 3 | 2
  117. 1 3 2 4 | 3
  118. 1 3 4 2 | 4
  119. ... | ...
  120. 4 3 1 2 | 23
  121. 4 3 2 1 | 24
  122. In the following, $q(\bx) \in [1, n!]$ will denote the index of the
  123. local order pattern $\sigma$ centered at pixel $\bx$.
  124. The local order patterns $q(\bx)$ in a region $R_t$ are then pooled to
  125. form a histogram of size $!n$. In this process, patterns are weighted
  126. based on their stability. The latter is assumed to be proportional to
  127. the number of pairs of pixels in the neighborhood that have a
  128. sufficiently large intensity difference:
  129. @f[
  130. w(\bx) = \sum_{i=1}^n \sum_{j=1}^n [ |I(\bx_{i}) - I(\bx_{j})| > \Theta) ]
  131. @f]
  132. where $[\cdot]$ is the indicator function.
  133. In VLFeat LIOP implementation, the threshold $\Theta$ is either set as
  134. an absolute value, or as a faction of the difference between the
  135. maximum and minimum intensity in the image (restricted to the pixels
  136. in the light area in the figure above).
  137. Overall, LIOP consists of $m$ histograms of size $n!$ obtained as
  138. \[
  139. h_{qt} = \sum_{\bx : q(\bx) = q \ \wedge\ \bx \in R_t} w(\bx).
  140. \]
  141. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  142. @section liop-normalization Normalization
  143. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  144. After computing the weighted counts $h_{qt}$, the LIOP descriptor is
  145. obtained by stacking the values $\{h_{qt}\}$ into a vector
  146. $\mathbf{h}$ and then normalising it:
  147. \[
  148. \Phi = \frac{\mathbf{h}}{\|\mathbf{h}\|_2}
  149. \]
  150. The dimensionality is therefore $m n!$, where $m$ is the @c
  151. numSpatialBins number of spatial bins and $n$ is the @c numNeighbours
  152. number of neighbours (see ::vl_liopdesc_new). By default, this
  153. descriptor is stored in @c single format. It can be stored as a
  154. sequence of bytes by premultiplying the values by the constant 255 and
  155. then rounding:
  156. \[
  157. \operatorname{round}\left[ 255\, \times \Phi\right].
  158. \]
  159. */
  160. #include "liop.h"
  161. #include "mathop.h"
  162. #include "imopv.h"
  163. #include <string.h>
  164. #define DEFAULT_INTENSITY_THRESHOLD -(5.0/255)
  165. #define DEFAULT_RADIUS 6.0
  166. #define DEFAULT_NUM_SPATIAL_BINS 6
  167. #define DEFAULT_NUM_NEIGHBOURS 4
  168. /* ---------------------------------------------------------------- */
  169. /* Helper functions */
  170. /* ---------------------------------------------------------------- */
  171. static
  172. vl_int factorial(vl_int num)
  173. {
  174. vl_int result = 1;
  175. while(num > 1){
  176. result = num*result;
  177. num--;
  178. }
  179. return result ;
  180. }
  181. /** @internal @brief Compute permutation index.
  182. ** @param permutation array containing all values from 0 to (size - 1) (input/output).
  183. ** @param size size of the permutation array.
  184. ** @return permutation index.
  185. **
  186. ** Compute the position of @a permutation in the lexycographcial
  187. ** sorting of permutations of the given @a size.
  188. **
  189. ** For example, in the lexicographical ordering, permutations of four elements
  190. ** are listed as [1 2 3 4], [1 2 4 3], [1 3 2 4], [1 3 4 2], [1 4 2 3],
  191. ** [1 4 3 2], [2 1 3 4], ..., [4 3 2 1].
  192. **
  193. ** The index can be computed as follows. First pick the first digit
  194. ** perm[1]. This is either 1,2,...,n. For each
  195. ** choice of the first digits, there are (n-1)! other permutations, separated
  196. ** therefore by (n-1)! elements in lexicographical order.
  197. **
  198. ** Process then the second digit perm[2]. This can be though as finding
  199. ** the lexycotraphical index of perm[2], ..., perm[n], a permutation of
  200. ** n-1 elements. This can be explicitly obtained by taking out 1 from
  201. ** all elements perm[i] > perm[1]. */
  202. VL_INLINE vl_index get_permutation_index(vl_uindex *permutation, vl_size size){
  203. vl_index index = 0 ;
  204. vl_index i ;
  205. vl_index j ;
  206. for (i = 0 ; i < (signed)size ; ++i) {
  207. index = index * ((signed)size - i) + permutation[i] ;
  208. for (j = i + 1 ; j < (signed)size ; ++j) {
  209. if (permutation[j] > permutation[i]) { permutation[j] -- ; }
  210. }
  211. }
  212. return index ;
  213. }
  214. /* instantiate two quick sort algorithms */
  215. VL_INLINE float patch_cmp (VlLiopDesc * liop, vl_index i, vl_index j)
  216. {
  217. vl_index ii = liop->patchPermutation[i] ;
  218. vl_index jj = liop->patchPermutation[j] ;
  219. return liop->patchIntensities[ii] - liop->patchIntensities[jj] ;
  220. }
  221. VL_INLINE void patch_swap (VlLiopDesc * liop, vl_index i, vl_index j)
  222. {
  223. vl_index tmp = liop->patchPermutation[i] ;
  224. liop->patchPermutation[i] = liop->patchPermutation[j] ;
  225. liop->patchPermutation[j] = tmp ;
  226. }
  227. #define VL_QSORT_prefix patch
  228. #define VL_QSORT_array VlLiopDesc*
  229. #define VL_QSORT_cmp patch_cmp
  230. #define VL_QSORT_swap patch_swap
  231. #include "qsort-def.h"
  232. VL_INLINE float neigh_cmp (VlLiopDesc * liop, vl_index i, vl_index j)
  233. {
  234. vl_index ii = liop->neighPermutation[i] ;
  235. vl_index jj = liop->neighPermutation[j] ;
  236. return liop->neighIntensities[ii] - liop->neighIntensities[jj] ;
  237. }
  238. VL_INLINE void neigh_swap (VlLiopDesc * liop, vl_index i, vl_index j)
  239. {
  240. vl_index tmp = liop->neighPermutation[i] ;
  241. liop->neighPermutation[i] = liop->neighPermutation[j] ;
  242. liop->neighPermutation[j] = tmp ;
  243. }
  244. #define VL_QSORT_prefix neigh
  245. #define VL_QSORT_array VlLiopDesc*
  246. #define VL_QSORT_cmp neigh_cmp
  247. #define VL_QSORT_swap neigh_swap
  248. #include "qsort-def.h"
  249. /* ---------------------------------------------------------------- */
  250. /* Construct and destroy */
  251. /* ---------------------------------------------------------------- */
  252. /** @brief Create a new LIOP object instance.
  253. ** @param numNeighbours number of neighbours.
  254. ** @param numSpatialBins number of bins.
  255. ** @param radius radius of the cirucal sample neighbourhoods.
  256. ** @param sideLength width of the input image patch (the patch is square).
  257. ** @return new object instance.
  258. **
  259. ** The value of @a radius should be at least less than half the @a
  260. ** sideLength of the patch.
  261. **/
  262. VlLiopDesc *
  263. vl_liopdesc_new (vl_int numNeighbours, vl_int numSpatialBins,
  264. float radius, vl_size sideLength)
  265. {
  266. vl_index i, t ;
  267. VlLiopDesc * self = (VlLiopDesc*)vl_calloc(sizeof(VlLiopDesc), 1);
  268. assert(radius <= sideLength/2) ;
  269. self->numNeighbours = numNeighbours ;
  270. self->numSpatialBins = numSpatialBins ;
  271. self->neighRadius = radius ;
  272. self->intensityThreshold = DEFAULT_INTENSITY_THRESHOLD ;
  273. self->dimension = factorial(numNeighbours) * numSpatialBins ;
  274. /*
  275. Precompute a list of pixels within a circular patch inside
  276. the square image. Leave a suitable marging for sampling around
  277. these pixels.
  278. */
  279. self->patchSize = 0 ;
  280. self->patchPixels = (vl_uindex*)vl_malloc(sizeof(vl_uindex)*sideLength*sideLength) ;
  281. self->patchSideLength = sideLength ;
  282. {
  283. vl_index x, y ;
  284. vl_index center = (sideLength - 1) / 2 ;
  285. double t = center - radius + 0.6 ;
  286. vl_index t2 = (vl_index) (t * t) ;
  287. for (y = 0 ; y < (signed)sideLength ; ++y) {
  288. for (x = 0 ; x < (signed)sideLength ; ++x) {
  289. vl_index dx = x - center ;
  290. vl_index dy = y - center ;
  291. if (x == 0 && y == 0) continue ;
  292. if (dx*dx + dy*dy <= t2) {
  293. self->patchPixels[self->patchSize++] = x + y * sideLength ;
  294. }
  295. }
  296. }
  297. }
  298. self->patchIntensities = (float*)vl_malloc(sizeof(float)*self->patchSize) ;
  299. self->patchPermutation = (vl_uindex*)vl_malloc(sizeof(vl_uindex)*self->patchSize) ;
  300. /*
  301. Precompute the samples in the circular neighbourhood of each
  302. measurement point.
  303. */
  304. self->neighPermutation = (vl_uindex*)vl_malloc(sizeof(vl_uindex) * self->numNeighbours) ;
  305. self->neighIntensities = (float*)vl_malloc(sizeof(float) * self->numNeighbours) ;
  306. self->neighSamplesX = (double*)vl_calloc(sizeof(double), self->numNeighbours * self->patchSize) ;
  307. self->neighSamplesY = (double*)vl_calloc(sizeof(double), self->numNeighbours * self->patchSize) ;
  308. for (i = 0 ; i < (signed)self->patchSize ; ++i) {
  309. vl_index pixel ;
  310. double x, y ;
  311. double dangle = 2*VL_PI / (double)self->numNeighbours ;
  312. double angle0 ;
  313. vl_index center = (sideLength - 1) / 2 ;
  314. pixel = self->patchPixels[i] ;
  315. x = (pixel % (signed)self->patchSideLength) - center ;
  316. y = (pixel / (signed)self->patchSideLength) - center ;
  317. angle0 = atan2(y,x) ;
  318. for (t = 0 ; t < (signed)self->numNeighbours ; ++t) {
  319. double x1 = x + radius * cos(angle0 + dangle * t) + center ;
  320. double y1 = y + radius * sin(angle0 + dangle * t) + center ;
  321. self->neighSamplesX[t + (signed)self->numNeighbours * i] = x1 ;
  322. self->neighSamplesY[t + (signed)self->numNeighbours * i] = y1 ;
  323. }
  324. }
  325. return self ;
  326. }
  327. /** @brief Create a new object with default parameters
  328. ** @param sideLength size of the patches to be processed.
  329. ** @return new object.
  330. **
  331. ** @see ::vl_liopdesc_new. */
  332. VlLiopDesc * vl_liopdesc_new_basic (vl_size sideLength)
  333. {
  334. return vl_liopdesc_new(DEFAULT_NUM_NEIGHBOURS,
  335. DEFAULT_NUM_SPATIAL_BINS,
  336. DEFAULT_RADIUS,
  337. sideLength) ;
  338. }
  339. /** @brief Delete object instance.
  340. ** @param self object instance. */
  341. void
  342. vl_liopdesc_delete (VlLiopDesc * self)
  343. {
  344. vl_free (self->patchPixels) ;
  345. vl_free (self->patchIntensities) ;
  346. vl_free (self->patchPermutation) ;
  347. vl_free (self->neighPermutation) ;
  348. vl_free (self->neighIntensities) ;
  349. vl_free (self->neighSamplesX) ;
  350. vl_free (self->neighSamplesY) ;
  351. vl_free (self) ;
  352. }
  353. /* ---------------------------------------------------------------- */
  354. /* Compute LIOP descriptor */
  355. /* ---------------------------------------------------------------- */
  356. /** @brief Compute liop descriptor for a patch
  357. ** @param self object instance
  358. ** @param desc descriptor to be computed (output).
  359. ** @param patch patch to process
  360. **
  361. ** Use ::vl_liopdesc_get_dimension to get the size of the descriptor
  362. ** @a desc. */
  363. void
  364. vl_liopdesc_process (VlLiopDesc * self, float * desc, float const * patch)
  365. {
  366. vl_index i,t ;
  367. vl_index offset,numPermutations ;
  368. vl_index spatialBinArea, spatialBinEnd, spatialBinIndex ;
  369. float threshold ;
  370. memset(desc, 0, sizeof(float) * self->dimension) ;
  371. /*
  372. * Sort pixels in the patch by increasing intensity.
  373. */
  374. for (i = 0 ; i < (signed)self->patchSize ; ++i) {
  375. vl_index pixel = self->patchPixels[i] ;
  376. self->patchIntensities[i] = patch[pixel] ;
  377. self->patchPermutation[i] = i ;
  378. }
  379. patch_sort(self, self->patchSize) ;
  380. /*
  381. * Tune the threshold if needed.
  382. */
  383. if (self->intensityThreshold < 0) {
  384. i = self->patchPermutation[0] ;
  385. t = self->patchPermutation[self->patchSize-1] ;
  386. threshold = - self->intensityThreshold
  387. * (self->patchIntensities[t] - self->patchIntensities[i]);
  388. } else {
  389. threshold = self->intensityThreshold ;
  390. }
  391. /*
  392. * Process pixels in order of increasing intenisity, dividing them into
  393. * spatial bins on the fly.
  394. */
  395. numPermutations = factorial(self->numNeighbours) ;
  396. spatialBinArea = self->patchSize / self->numSpatialBins ;
  397. spatialBinEnd = spatialBinArea ;
  398. spatialBinIndex = 0 ;
  399. offset = 0 ;
  400. for (i = 0 ; i < (signed)self->patchSize ; ++i) {
  401. vl_index permIndex ;
  402. double *sx, *sy ;
  403. /* advance to the next spatial bin if needed */
  404. if (i >= (signed)spatialBinEnd && spatialBinIndex < (signed)self->numSpatialBins - 1) {
  405. spatialBinEnd += spatialBinArea ;
  406. spatialBinIndex ++ ;
  407. offset += numPermutations ;
  408. }
  409. /* get intensities of neighbours of the current patch element and sort them */
  410. sx = self->neighSamplesX + self->numNeighbours * self->patchPermutation[i] ;
  411. sy = self->neighSamplesY + self->numNeighbours * self->patchPermutation[i] ;
  412. for (t = 0 ; t < self->numNeighbours ; ++t) {
  413. double x = *sx++ ;
  414. double y = *sy++ ;
  415. /* bilinear interpolation */
  416. vl_index ix = vl_floor_d(x) ;
  417. vl_index iy = vl_floor_d(y) ;
  418. double wx = x - ix ;
  419. double wy = y - iy ;
  420. double a = 0, b = 0, c = 0, d = 0 ;
  421. int L = (int) self->patchSideLength ;
  422. if (ix >= 0 && iy >= 0 ) { a = patch[ix + iy * L] ; }
  423. if (ix < L-1 && iy >= 0 ) { b = patch[ix+1 + iy * L] ; }
  424. if (ix >= 0 && iy < L-1) { c = patch[ix + (iy+1) * L] ; }
  425. if (ix < L-1 && iy < L-1) { d = patch[ix+1 + (iy+1) * L] ; }
  426. self->neighPermutation[t] = t;
  427. self->neighIntensities[t] = (1 - wy) * (a + (b - a) * wx) + wy * (c + (d - c) * wx) ;
  428. }
  429. neigh_sort (self, self->numNeighbours) ;
  430. /* get permutation index */
  431. permIndex = get_permutation_index(self->neighPermutation, self->numNeighbours);
  432. /*
  433. * Compute weight according to difference in intensity values and
  434. * accumulate.
  435. */
  436. {
  437. int k, t ;
  438. float weight = 0 ;
  439. for(k = 0; k < self->numNeighbours ; ++k) {
  440. for(t = k + 1; t < self->numNeighbours; ++t){
  441. double a = self->neighIntensities[k] ;
  442. double b = self->neighIntensities[t] ;
  443. weight += (a > b + threshold || b > a + threshold) ;
  444. }
  445. }
  446. desc[permIndex + offset] += weight ;
  447. }
  448. }
  449. /* normalization */
  450. {
  451. float norm = 0;
  452. for(i = 0; i < (signed)self->dimension; i++) {
  453. norm += desc[i]*desc[i];
  454. }
  455. norm = VL_MAX(sqrt(norm), 1e-12) ;
  456. for(i = 0; i < (signed)self->dimension; i++){
  457. desc[i] /= norm ;
  458. }
  459. }
  460. }
  461. /* ---------------------------------------------------------------- */
  462. /* Getters and setters */
  463. /* ---------------------------------------------------------------- */
  464. /** @brief Get the dimension of a LIOP descriptor.
  465. ** @param self object.
  466. ** @return dimension. */
  467. vl_size
  468. vl_liopdesc_get_dimension (VlLiopDesc const * self)
  469. {
  470. return self->dimension ;
  471. }
  472. /** @brief Get the number of neighbours.
  473. ** @param self object.
  474. ** @return number of neighbours.
  475. **/
  476. vl_size
  477. vl_liopdesc_get_num_neighbours (VlLiopDesc const * self)
  478. {
  479. assert(self) ;
  480. return self->numNeighbours ;
  481. }
  482. /** @brief Get the intensity threshold
  483. ** @param self object.
  484. ** @return intensity threshold.
  485. ** @see liop-weighing
  486. **/
  487. float
  488. vl_liopdesc_get_intensity_threshold (VlLiopDesc const * self)
  489. {
  490. assert(self) ;
  491. return self->intensityThreshold ;
  492. }
  493. /** @brief Set the intensity threshold
  494. ** @param self object.
  495. ** @param x intensity threshold.
  496. **
  497. ** If non-negative, the threshold as is is used when comparing
  498. ** intensities. If negative, the absolute value of the specified
  499. ** number is multipled by the maximum intensity difference inside a
  500. ** patch to obtain the threshold.
  501. **
  502. ** @see liop-weighing
  503. **/
  504. void
  505. vl_liopdesc_set_intensity_threshold (VlLiopDesc * self, float x)
  506. {
  507. assert(self) ;
  508. self->intensityThreshold = x ;
  509. }
  510. /** @brief Get the neighbourhood radius.
  511. ** @param self object.
  512. ** @return neighbourhood radius.
  513. **/
  514. double
  515. vl_liopdesc_get_neighbourhood_radius (VlLiopDesc const * self)
  516. {
  517. assert(self) ;
  518. return self->neighRadius ;
  519. }
  520. /** @brief Get the number of spatial bins.
  521. ** @param self object.
  522. ** @return number of spatial bins.
  523. **/
  524. vl_size
  525. vl_liopdesc_get_num_spatial_bins (VlLiopDesc const * self)
  526. {
  527. assert(self) ;
  528. return self->numSpatialBins ;
  529. }