| 1 |
/** @file lbp.c |
| 2 |
** @brief Local Binary Patterns (LBP) - Definition |
| 3 |
** @author Andrea Vedaldi |
| 4 |
**/ |
| 5 |
|
| 6 |
/* |
| 7 |
Copyright (C) 2013 Andrea Vedaldi. |
| 8 |
Copyright (C) 2007-12 Andrea Vedaldi and Brian Fulkerson. |
| 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 lbp Local Binary Patterns (LBP) descriptor |
| 18 |
@author Andrea Vedaldi |
| 19 |
<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --> |
| 20 |
|
| 21 |
@ref lbp.h implements the Local Binary Pattern (LBP) feature |
| 22 |
descriptor. The LBP descriptor @cite{ojala10multiresolution} is a |
| 23 |
histogram of quantized LBPs pooled in a local image neighborhood. @ref |
| 24 |
lbp-starting demonstrates how to use the C API to compute the LBP |
| 25 |
descriptors of an image. For further details refer to: |
| 26 |
|
| 27 |
- @subpage lbp-fundamentals - LBP definition and parameters. |
| 28 |
|
| 29 |
<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --> |
| 30 |
@section lbp-starting Getting started with LBP |
| 31 |
<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --> |
| 32 |
|
| 33 |
To compute the LBP descriptor of an image, start by creating a ::VlLbp |
| 34 |
object instance by specifying the type of LBP quantization. Given the |
| 35 |
configure LBP object, then call ::vl_lbp_process to process a |
| 36 |
grayscale image and obtain the corresponding LBP descriptors. This |
| 37 |
function expects as input a buffer large enough to contain the |
| 38 |
computed features. If the image has size @c width x @c height, there |
| 39 |
are exactly @c floor(width/cellSize) x @c floor(height/cellSize) |
| 40 |
cells, each of which has a histogram of LBPs of size @c dimension (as |
| 41 |
returned by ::vl_lbp_get_dimension). Thus the required buffer has size |
| 42 |
@c floor(width/cellSize) x @c floor(height/cellSize) x @c dimension. |
| 43 |
|
| 44 |
::VlLbp supports computing transposed LPBs as well. A transposed LBP |
| 45 |
is the LBP obtained by transposing the input image (regarded as a |
| 46 |
matrix). This functionality can be useful to compute the features when |
| 47 |
the input image is stored in column major format (e.g. MATLAB) rather |
| 48 |
than row major. |
| 49 |
**/ |
| 50 |
|
| 51 |
/** |
| 52 |
<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --> |
| 53 |
@page lbp-fundamentals Local Binary Patterns fundamentals |
| 54 |
@tableofcontents |
| 55 |
<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --> |
| 56 |
|
| 57 |
A *Locally Binary Pattern* (LBP) is a local descriptor that captures |
| 58 |
the appearance of an image in a small neighborhood around a pixel. An |
| 59 |
LBP is a string of bits, with one bit for each of the pixels in the |
| 60 |
neighborhood. Each bit is turned on or off depending on whether the |
| 61 |
intensity of the corresponding pixel is greater than the intensity of |
| 62 |
the central pixel. LBP are seldom used directly, however. Instead, the |
| 63 |
binary string thus produced are further quantized (@ref |
| 64 |
lbp-quantization) and pooled in local histograms (@ref |
| 65 |
lbp-histograms). |
| 66 |
|
| 67 |
While many variants are possible, ::VlLbp implements only the case of |
| 68 |
3 × 3 pixel neighborhoods (this setting was found to be optimal |
| 69 |
in several applications). In particular, the LBP centered on pixel |
| 70 |
$(x,y)$ is a string of eight bits. Each bit is equal to one if the |
| 71 |
corresponding pixel is brighter than the central one. Pixels are |
| 72 |
scanned starting from the one to the right in anti-clockwise order. |
| 73 |
For example the first bit is one if, and only if, $I(x+1,y) > |
| 74 |
I(x,y)$, and the second bit is one if, and only if, $I(x+1,y-1) > |
| 75 |
I(x,y)$. |
| 76 |
|
| 77 |
<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --> |
| 78 |
@section lbp-quantization Quantized LBP |
| 79 |
<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --> |
| 80 |
|
| 81 |
For a 3 × 3 neighborhood, an LBP is a string of eight bits and |
| 82 |
so there are 256 possible LBPs. These are usually too many for a |
| 83 |
reliable statistics (histogram) to be computed. Therefore the 256 |
| 84 |
patterns are further quantized into a smaller number of patterns |
| 85 |
according to one of the following rules: |
| 86 |
|
| 87 |
- <b>Uniform</b> (::VlLbpUniform) There is one quantized pattern for |
| 88 |
each LBP that has exactly a transitions from 0 to 1 and one from 1 |
| 89 |
to 0 when scanned in anti-clockwise order, plus one quantized |
| 90 |
pattern comprising the two uniform LBPs, and one quantized pattern |
| 91 |
comprising all the other LBPs. This yields a total of 58 quantized |
| 92 |
patterns. |
| 93 |
|
| 94 |
@image html lbp.png "LBP quantized patterns." |
| 95 |
|
| 96 |
The number of quantized LBPs, which depends on the quantization type, |
| 97 |
can be obtained by ::vl_lbp_get_dimension. |
| 98 |
|
| 99 |
<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --> |
| 100 |
@section lbp-histograms Histograms of LBPs |
| 101 |
<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --> |
| 102 |
|
| 103 |
The quantized LBP patterns are further grouped into local |
| 104 |
histograms. The image is divided into a number of cells of a |
| 105 |
prescribed size (as specified by the parameter @c cellSize passed to |
| 106 |
::vl_lbp_process as described in @ref lbp-starting). Then the |
| 107 |
quantized LBPs are aggregated into histogram by using bilinear |
| 108 |
interpolation along the two spatial dimensions (similar to HOG and |
| 109 |
SIFT). |
| 110 |
**/ |
| 111 |
|
| 112 |
#include "lbp.h" |
| 113 |
#include "mathop.h" |
| 114 |
#include "string.h" |
| 115 |
|
| 116 |
/* ---------------------------------------------------------------- */ |
| 117 |
/* Initialization helpers */ |
| 118 |
/* ---------------------------------------------------------------- */ |
| 119 |
|
| 120 |
/* |
| 121 |
This function creates the LBP quantization table for the uniform LBP |
| 122 |
patterns. The purpose of this lookup table is to map a 8-bit LBP |
| 123 |
strings to one of 58 uniform pattern codes. |
| 124 |
|
| 125 |
Pixels in the 8-neighbourhoods are read in counterclockwise order |
| 126 |
starting from the east direction, as follows: |
| 127 |
|
| 128 |
NW(5) N(6) NE(7) |
| 129 |
W(4) E(0) -> b0 b1 b2 b3 b4 b5 b6 b7 |
| 130 |
SW(3) S(2) SE(1) |
| 131 |
|
| 132 |
There are 256 such strings, indexing the lookup table. The table |
| 133 |
contains the corresponding code, effectively quantizing the 256 |
| 134 |
patterns into 58. There is one bin for constant patterns (all zeros |
| 135 |
or ones), 8*7 for the uniform ones, and one for all other. |
| 136 |
|
| 137 |
A uniform pattern is a circular sequence of bit b0b1...b7 such that |
| 138 |
there is exactly one switch from 0 to 1 and one from 1 to 0. These |
| 139 |
uniform patterns are enumerated as follows. The slowest varying index |
| 140 |
i (0...7) points to the first bit that is on and the slowest varying |
| 141 |
index j (1...7) to the length of the run of bits equal to one, |
| 142 |
resulting in the sequence: |
| 143 |
|
| 144 |
0: 1000 0000 |
| 145 |
1: 1100 0000 |
| 146 |
... |
| 147 |
7: 1111 1110 |
| 148 |
8: 0100 0000 |
| 149 |
9: 0110 0000 |
| 150 |
... |
| 151 |
56: 1111 1101 |
| 152 |
|
| 153 |
The function also accounts for when the image is stored in transposed |
| 154 |
format. The sampling function is unchanged, so that the first bit to |
| 155 |
be read is not the one to the east, but the one to the south, and |
| 156 |
overall the following sequence is read: |
| 157 |
|
| 158 |
NW(5) W(4) SW(3) |
| 159 |
N(6) S(2) -> b2 b1 b0 b7 b6 b5 b4 b3 |
| 160 |
NE(7) E(0) SE(1) |
| 161 |
|
| 162 |
In enumerating the uniform patterns, the index j is unchanged as it |
| 163 |
encodes the runlenght. On the contrary, the index i changes to |
| 164 |
account for the transposition and for the fact that the beginning and |
| 165 |
ending of the run are swapped. With modular arithmetic, the i must be |
| 166 |
transformed as |
| 167 |
|
| 168 |
ip = - i + 2 - (j - 1) |
| 169 |
*/ |
| 170 |
|
| 171 |
static void |
| 172 |
_vl_lbp_init_uniform(VlLbp * self) |
| 173 |
{ |
| 174 |
int i, j ; |
| 175 |
|
| 176 |
/* overall number of quantized LBPs */ |
| 177 |
self->dimension = 58 ; |
| 178 |
|
| 179 |
/* all but selected patterns map to bin 57 (the first bin has index 0) */ |
| 180 |
for (i = 0 ; i < 256 ; ++i) { |
| 181 |
self->mapping[i] = 57 ; |
| 182 |
} |
| 183 |
|
| 184 |
/* the uniform (all zeros or ones) patterns map to bin 56 */ |
| 185 |
self->mapping[0x00] = 56 ; |
| 186 |
self->mapping[0xff] = 56 ; |
| 187 |
|
| 188 |
/* 56 uniform patterns */ |
| 189 |
for (i = 0 ; i < 8 ; ++i) { |
| 190 |
for (j = 1 ; j <= 7 ; ++j) { |
| 191 |
int ip ; |
| 192 |
int unsigned string ; |
| 193 |
if (self->transposed) { |
| 194 |
ip = (- i + 2 - (j - 1) + 16) % 8 ; |
| 195 |
} else { |
| 196 |
ip = i ; |
| 197 |
} |
| 198 |
|
| 199 |
/* string starting with j ones */ |
| 200 |
string = (1 << j) - 1 ; |
| 201 |
string <<= ip ; |
| 202 |
string = (string | (string >> 8)) & 0xff ; |
| 203 |
|
| 204 |
self->mapping[string] = i * 7 + (j-1) ; |
| 205 |
} |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
/* ---------------------------------------------------------------- */ |
| 210 |
|
| 211 |
/** @brief Create a new LBP object |
| 212 |
** @param type type of LBP features. |
| 213 |
** @param transposed if @c true, then transpose each LBP pattern. |
| 214 |
** @return new VlLbp object instance. |
| 215 |
**/ |
| 216 |
|
| 217 |
VlLbp * |
| 218 |
vl_lbp_new(VlLbpMappingType type, vl_bool transposed) |
| 219 |
{ |
| 220 |
VlLbp * self = vl_malloc(sizeof(VlLbp)) ; |
| 221 |
if (self == NULL) { |
| 222 |
vl_set_last_error(VL_ERR_ALLOC, NULL) ; |
| 223 |
return NULL ; |
| 224 |
} |
| 225 |
self->transposed = transposed ; |
| 226 |
switch (type) { |
| 227 |
case VlLbpUniform: _vl_lbp_init_uniform(self) ; break ; |
| 228 |
default: exit(1) ; |
| 229 |
} |
| 230 |
return self ; |
| 231 |
} |
| 232 |
|
| 233 |
/** @brief Delete VlLbp object |
| 234 |
** @param self object to delete. |
| 235 |
**/ |
| 236 |
|
| 237 |
void |
| 238 |
vl_lbp_delete(VlLbp * self) { |
| 239 |
vl_free(self) ; |
| 240 |
} |
| 241 |
|
| 242 |
/** @brief Get the dimension of the LBP histograms |
| 243 |
** @return dimension of the LBP histograms. |
| 244 |
** The dimension depends on the type of quantization used. |
| 245 |
** @see ::vl_lbp_new(). |
| 246 |
**/ |
| 247 |
|
| 248 |
VL_EXPORT vl_size vl_lbp_get_dimension(VlLbp * self) |
| 249 |
{ |
| 250 |
return self->dimension ; |
| 251 |
} |
| 252 |
|
| 253 |
/* ---------------------------------------------------------------- */ |
| 254 |
|
| 255 |
/** @brief Extract LBP features |
| 256 |
** @param self LBP object. |
| 257 |
** @param features buffer to write the features to. |
| 258 |
** @param image image. |
| 259 |
** @param width image width. |
| 260 |
** @param height image height. |
| 261 |
** @param cellSize size of the LBP cells. |
| 262 |
** |
| 263 |
** @a features is a @c numColumns x @c numRows x @c dimension where |
| 264 |
** @c dimension is the dimension of a LBP feature obtained from ::vl_lbp_get_dimension, |
| 265 |
** @c numColumns is equal to @c floor(width / cellSize), and similarly |
| 266 |
** for @c numRows. |
| 267 |
**/ |
| 268 |
|
| 269 |
VL_EXPORT void |
| 270 |
vl_lbp_process (VlLbp * self, |
| 271 |
float * features, |
| 272 |
float * image, vl_size width, vl_size height, |
| 273 |
vl_size cellSize) |
| 274 |
{ |
| 275 |
vl_size cwidth = width / cellSize; |
| 276 |
vl_size cheight = height / cellSize ; |
| 277 |
vl_size cstride = cwidth * cheight ; |
| 278 |
vl_size cdimension = vl_lbp_get_dimension(self) ; |
| 279 |
vl_index x,y,cx,cy,k,bin ; |
| 280 |
|
| 281 |
#define at(u,v) (*(image + width * (v) + (u))) |
| 282 |
#define to(u,v,w) (*(features + cstride * (w) + cwidth * (v) + (u))) |
| 283 |
|
| 284 |
/* clear the output buffer */ |
| 285 |
memset(features, 0, sizeof(float)*cdimension*cstride) ; |
| 286 |
|
| 287 |
/* accumulate pixel-level measurements into cells */ |
| 288 |
for (y = 1 ; y < (signed)height - 1 ; ++y) { |
| 289 |
float wy1 = (y + 0.5f) / (float)cellSize - 0.5f ; |
| 290 |
int cy1 = (int) vl_floor_f(wy1) ; |
| 291 |
int cy2 = cy1 + 1 ; |
| 292 |
float wy2 = wy1 - (float)cy1 ; |
| 293 |
wy1 = 1.0f - wy2 ; |
| 294 |
if (cy1 >= (signed)cheight) continue ; |
| 295 |
|
| 296 |
for (x = 1 ; x < (signed)width - 1; ++x) { |
| 297 |
float wx1 = (x + 0.5f) / (float)cellSize - 0.5f ; |
| 298 |
int cx1 = (int) vl_floor_f(wx1) ; |
| 299 |
int cx2 = cx1 + 1 ; |
| 300 |
float wx2 = wx1 - (float)cx1 ; |
| 301 |
wx1 = 1.0f - wx2 ; |
| 302 |
if (cx1 >= (signed)cwidth) continue ; |
| 303 |
|
| 304 |
{ |
| 305 |
int unsigned bitString = 0 ; |
| 306 |
float center = at(x,y) ; |
| 307 |
if(at(x+1,y+0) > center) bitString |= 0x1 << 0; /* E */ |
| 308 |
if(at(x+1,y+1) > center) bitString |= 0x1 << 1; /* SE */ |
| 309 |
if(at(x+0,y+1) > center) bitString |= 0x1 << 2; /* S */ |
| 310 |
if(at(x-1,y+1) > center) bitString |= 0x1 << 3; /* SW */ |
| 311 |
if(at(x-1,y+0) > center) bitString |= 0x1 << 4; /* W */ |
| 312 |
if(at(x-1,y-1) > center) bitString |= 0x1 << 5; /* NW */ |
| 313 |
if(at(x+0,y-1) > center) bitString |= 0x1 << 6; /* N */ |
| 314 |
if(at(x+1,y-1) > center) bitString |= 0x1 << 7; /* NE */ |
| 315 |
bin = self->mapping[bitString] ; |
| 316 |
} |
| 317 |
|
| 318 |
if ((cx1 >= 0) & (cy1 >=0)) { |
| 319 |
to(cx1,cy1,bin) += wx1 * wy1; |
| 320 |
} |
| 321 |
if ((cx2 < (signed)cwidth) & (cy1 >=0)) { |
| 322 |
to(cx2,cy1,bin) += wx2 * wy1 ; |
| 323 |
} |
| 324 |
if ((cx1 >= 0) & (cy2 < (signed)cheight)) { |
| 325 |
to(cx1,cy2,bin) += wx1 * wy2 ; |
| 326 |
} |
| 327 |
if ((cx2 < (signed)cwidth) & (cy2 < (signed)cheight)) { |
| 328 |
to(cx2,cy2,bin) += wx2 * wy2 ; |
| 329 |
} |
| 330 |
} /* x */ |
| 331 |
} /* y */ |
| 332 |
|
| 333 |
/* normalize cells */ |
| 334 |
for (cy = 0 ; cy < (signed)cheight ; ++cy) { |
| 335 |
for (cx = 0 ; cx < (signed)cwidth ; ++ cx) { |
| 336 |
float norm = 0 ; |
| 337 |
for (k = 0 ; k < (signed)cdimension ; ++k) { |
| 338 |
norm += features[k * cstride] ; |
| 339 |
} |
| 340 |
norm = sqrtf(norm) + 1e-10f; ; |
| 341 |
for (k = 0 ; k < (signed)cdimension ; ++k) { |
| 342 |
features[k * cstride] = sqrtf(features[k * cstride]) / norm ; |
| 343 |
} |
| 344 |
features += 1 ; |
| 345 |
} |
| 346 |
} /* next cell to normalize */ |
| 347 |
} |