Local Ternary Pattern¶
| Fact | Value |
|---|---|
| Also called | LTP, Local ternary patterns |
| Status | Implemented and tested |
| Family | Texture, corners, blobs and features |
| Engine | Science engine |
| Type | Nonlinear, spatial, local ternary codes |
| Output | Image |
| Operator shown as | algorithm (exact) |
| Support radius | 1 |
| Code | engine/src/pixelnexus_engine/filters/texture.py, ltp |
| Operator code | engine/src/pixelnexus_engine/filters/texture.py, ltp_representation |
| Packages | NumPy |
Overview¶
A local ternary pattern works like a local binary pattern, with one change. A neighbor within a threshold t of the center counts as about the same, neither brighter nor darker. Tan and Triggs added this dead band so that small noise in flat areas no longer flips the code. The three states give too many codes for a histogram, so the pattern is split into two binary codes. The upper code marks the clearly brighter neighbors, and the lower code the clearly darker ones. The filter shows one of them as a gray image, or counts both in a histogram table. It runs in the PixelNexus engine.
How it works¶
- Take the 8-bit luma.
- Compare each of the 8 neighbors with the center, with the dead band t.
- Set a bit of the upper code for each brighter neighbor and of the lower code for each darker one.
- Show one code as an image, or count both in a histogram.
Mathematical foundation¶
The formula shows the default parameter values.
where
- \(c\): The center pixel
- \(n_k\): The neighbor at 45 k degrees, counterclockwise from the right
- \(t\): The threshold
- \(U, L\): The upper and lower codes
A local binary pattern sets a bit for every neighbor at least as bright as the center, so in a flat, noisy area noise alone decides the bits. Tan and Triggs added a dead band of width t: a neighbor within t of the center counts as 0, neither brighter nor darker. The ternary code has 3^8 values, too many for a histogram, so it is split into two binary codes. The upper code U has a bit for each neighbor that is clearly brighter, and the lower code L for each that is clearly darker. Bit k belongs to the neighbor at 45 k degrees, counterclockwise from the right. With t = 0, the upper code is the local binary pattern of the 8 neighbors.
In a flat area with noise, every difference between a neighbor and the center is small. A binary pattern still turns each of them into a bit, so the code is random. With the dead band, differences smaller than t give 0 in both codes, and the code of a flat area is 0.
The price is that t is in gray levels. Doubling the contrast of the image moves more differences past t, so the codes are no longer independent of the contrast as binary patterns are. Adding a constant still keeps them, away from the image edge.
Kernel or operator¶
There is no kernel. The Inspector asks the engine for the stages: the ternary comparison with the current threshold, and the upper and lower codes.
Parameters¶
| Parameter | Default | Values | Meaning |
|---|---|---|---|
Output output |
Upper code | Upper code, Lower code, Histogram table | The upper code, from the brighter neighbors, the lower code, from the darker ones, or a table of how often each code occurs in both. |
Threshold threshold |
5 | 0 to 50 levels | The band t around the center in which a neighbor counts as about the same. |
Border mode border |
Reflect 101 (default) | Reflect 101 (default), Reflect, Replicate, Constant (zero), Wrap | How pixels outside the image are filled when the window reaches past the edge. |
Implementation¶
ltp in engine/src/pixelnexus_engine/filters/texture.py is PixelNexus's own code. It pads the 8-bit luma by one pixel with the border mode and compares each of the 8 neighbors with the center. Bit k of both codes belongs to the neighbor at 45 k degrees, counterclockwise from the right: right, up right, up, up left, left, down left, down and down right. A code goes into the image as it is, from 0 to 255. The histogram table has one row for each of the 256 codes, with the count of the upper and of the lower code. Tan and Triggs use neighbors on a circle of any radius. This version uses the 8 direct neighbors only.
Worked example¶
| Quarter | t = 0 | t = 2 | t = 5 | t = 10 |
|---|---|---|---|---|
| Stripes | 0.00 | 0.00 | 0.00 | 0.00 |
| Fine noise | 0.00 | 0.00 | 0.00 | 0.00 |
| Coarse | 0.00 | 0.00 | 0.02 | 0.21 |
| Flat | 0.00 | 0.00 | 0.54 | 0.99 |
The share of pixels whose upper and lower codes are both 0, so that no neighbor counts as brighter or darker. Each quarter of the test image of the figure, 80 x 80, on its own.
With t = 0, every neighbor counts as brighter or darker, so no pixel is quiet. The flat quarter, with noise of sigma 2, is quiet at t = 5 in about half of its pixels and almost everywhere at t = 10. The stripes and the fine noise have large differences, so they keep their codes. About a fifth of the coarse noise is quiet at t = 10, where its slopes are gentle.
Complexity¶
- Time: \(O(N)\)
- Memory: \(O(N)\)
N is the number of pixels.
Artifacts and limitations¶
Limitations
- The threshold is in gray levels, so it no longer ignores contrast changes as LBP does.
- This version uses the 8 direct neighbors only, not a circle of any radius.
Artifacts
- Codes that change suddenly where a difference crosses the threshold.
Tests¶
The codes must match a direct loop over every pixel and neighbor for several thresholds and border modes. A single bright neighbor must set bit k for the neighbor at 45 k degrees. A flat area with noise smaller than t must give 0 in both codes, where a binary pattern gives many codes. With t = 0, a flat image must set every bit of both codes. The outputs must be the codes and their histograms. The engine API test runs the filter.
References¶
- Tan, X., and Triggs, B. Enhanced local texture feature sets for face recognition under difficult lighting conditions. IEEE Transactions on Image Processing 19(6), 1635 to 1650, 2010. DOI 10.1109/TIP.2010.2042645.