Local Binary Pattern¶
| Fact | Value |
|---|---|
| Also called | LBP, Local binary patterns |
| Status | Implemented and tested |
| Family | Texture, corners, blobs and features |
| Engine | Science engine |
| Type | Nonlinear, spatial, local binary codes |
| Output | Image |
| Operator shown as | algorithm (exact) |
| Support radius | ceil(radius) + 1 |
| Code | engine/src/pixelnexus_engine/filters/texture.py, lbp |
| Operator code | engine/src/pixelnexus_engine/filters/texture.py, lbp_representation |
| Packages | scikit-image |
Overview¶
A local binary pattern describes the texture around each pixel with a small code. P points on a circle around the pixel each give one bit: 1 when the point is at least as bright as the center. Ojala, Pietikainen and Maenpaa showed that the mix of these codes describes a texture well. The codes only depend on which neighbors are brighter, so the brightness and the contrast of the image do not change them. The filter shows the codes as a gray image, or counts them in a histogram table for the Features panel. It runs in the PixelNexus engine, with scikit-image.
How it works¶
- Take the 8-bit luma.
- Sample P points on a circle of radius R around each pixel.
- Set a bit for each point that is at least as bright as the center.
- Turn the bits into a code with the chosen method.
- Scale the codes to an image, or count them in a histogram.
Mathematical foundation¶
The formula shows the default parameter values.
where
- \(g_c\): The center pixel
- \(g_p\): The sample points on the circle
- \(P, R\): The number of points and the radius
The center pixel is g_c, and g_p are P points on a circle of radius R around it. They start at the right and go counterclockwise. Points between pixels are interpolated. Each point gives a bit: 1 when it is at least as bright as the center. Only the signs of the differences count. So adding a constant to the image or stretching its contrast leaves the codes as they are. The rotation invariant method turns each code to its smallest rotation. Uniform codes have at most two changes between 0 and 1 around the circle. Ojala and coworkers found that they cover most of natural texture. So the uniform method labels them by the number of 1s and puts all others under one label. The variance method gives the variance of the samples instead, a measure of local contrast. The codes are scaled to the full gray range for the image output.
The sign function s throws away how much brighter a neighbor is. Only the order of the gray levels counts. Any change of the image that keeps this order, such as adding a constant or stretching the contrast, keeps the codes.
With P points there are 2^P codes. A quarter turn of the texture turns the bits of the code around the circle. The rotation invariant method turns every code to the smallest number among its rotations, so turned textures give the same code. Uniform codes have at most two changes between 0 and 1 around the circle, such as 00011100. They stand for spots, flat areas, line ends, edges and corners. The uniform method labels them by the number of 1s, from 0 to P, and gives all other codes the label P + 1. The method without rotation invariance, nri_uniform, keeps the turned uniform codes apart.
The variance method is not a code. It gives the variance of the P samples, a measure of the local contrast that the codes leave out. Ojala and coworkers use it together with the codes.
Kernel or operator¶
There is no kernel. The Inspector asks the engine for the stages with the current settings: the sample points on the circle and the code with the chosen method.
Parameters¶
| Parameter | Default | Values | Meaning |
|---|---|---|---|
Output output |
Codes as an image | Codes as an image, Histogram table | The code of every pixel as a gray image, or a table with how often each code occurs. With the variance method, the output is always an image. |
Points points |
8 | 4 to 24 | The number P of sample points on the circle. Each gives one bit. |
Radius radius |
1 | 1 to 8 px | The radius R of the circle of sample points. |
Method method |
Default (all codes) | Default (all codes), Rotation invariant, Uniform, rotation invariant, Uniform, not rotation invariant, Variance | How the bits become a code. The names are those of scikit-image. |
Implementation¶
lbp in engine/src/pixelnexus_engine/filters/texture.py calls skimage.feature.local_binary_pattern on the 8-bit luma. Here scikit-image places point p at the angle 2 pi p / P, counterclockwise from the right. It interpolates points between pixels bilinearly and counts points past the edge as 0. For the image output, the codes are scaled so the largest possible code is white. That code is 2^P - 1 for the default and rotation invariant methods, P + 1 for uniform, and P (P - 1) + 2 for nri_uniform. The variance is stretched so its largest value is white. The histogram table has a row for every possible code, with its count and its share of the pixels.
Worked example¶
| Quarter | Default codes used | Most common uniform label | Non-uniform share |
|---|---|---|---|
| Stripes | 12 | 5 (0.72) | 0.13 |
| Fine noise | 254 | 0 (0.18) | 0.25 |
| Coarse | 103 | 4 (0.52) | 0.01 |
| Flat | 247 | 8 (0.19) | 0.27 |
Each quarter of the test image of the figure, 80 x 80, on its own, with 8 points at radius 1. The default method has 256 codes. The uniform method has the labels 0 to 8 by the number of 1s, and label 9 for all codes that are not uniform. The share is that of the pixels of the quarter.
The stripes use only 12 of the 256 codes, since every column repeats. The fine noise uses almost all of them. The flat quarter, with noise of sigma 2 only, uses 247 codes, about as many as the fine noise. The codes cannot tell faint noise from strong noise. Local Ternary Pattern fixes this with a threshold.
Complexity¶
- Time: \(O(P N)\)
- Memory: \(O(N)\)
N is the number of pixels and P the number of points.
Artifacts and limitations¶
Limitations
- Noise in flat areas flips bits, since any difference counts.
- With many points, the default method gives very many codes, too many for a useful histogram.
Artifacts
- Speckled codes in flat, noisy areas.
Tests¶
For every method but the variance, the image must be the code of scikit-image, scaled by the code count, with the alpha kept. The code counts must be right for each method. The histogram must count every pixel. The variance must be stretched to white. Doubling the contrast and adding a constant must keep the codes inside the image. The engine API test runs the filter.
References¶
- Ojala, T., Pietikainen, M., and Maenpaa, T. Multiresolution gray-scale and rotation invariant texture classification with local binary patterns. IEEE Transactions on Pattern Analysis and Machine Intelligence 24(7), 971 to 987, 2002. DOI 10.1109/TPAMI.2002.1017623.
- scikit-image documentation, skimage.feature.local_binary_pattern.