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