DIP+CV · Programming Exercise

EP07_04 — 🟠 Manual Encoding of the LBP Descriptor

7.18.4 EP07_04 🟠 Manual Encoding of the LBP Descriptor

The local_binary_pattern function from scikit-image, used in the texture classification project, automatically computes the LBP code for each pixel of an image. Before using it as a black box, you have been tasked with manually implementing the computation of the classic LBP code (\(P=8\), \(R=1\)) for the central pixel of a \(3\times3\) neighborhood, exactly as defined in the equation in the chapter.

In addition to the code, the texture inspection system also needs to know whether that pattern is uniform — a pattern is uniform when the number of transitions (\(0\to1\) or \(1\to0\)) when traversing the 8 bits circularly (returning from the last bit to the first) is at most 2, a property exploited by the uniform variant of the LBP mentioned in the chapter.

7.18.4.1 📋 Implementation Guidelines

  1. Quantity: Read the integer \(T\) (number of neighborhoods to process).
  2. Data for each neighborhood: For each of the \(T\) neighborhoods, read a \(3\times3\) matrix of integers (intensities), provided in 3 lines of 3 values each. The central pixel is at position [1][1].
  3. Order of neighbors: Traverse the 8 neighbors in clockwise order, starting at the top-left corner, in the following sequence of positions [row][column]: [0][0], [0][1], [0][2], [1][2], [2][2], [2][1], [2][0], [1][0]. This corresponds to the index \(p = 0, 1, \ldots, 7\) in the LBP equation.
  4. Threshold function: For each neighbor \(p\) with intensity \(g_p\) and center \(g_c\), compute \(s(g_p - g_c)\), which is 1 if \(g_p \geq g_c\) and 0 otherwise.
  5. LBP code: Compute \[ \mathrm{LBP} = \sum_{p=0}^{7} s(g_p - g_c)\, 2^p. \]
  6. Transitions: Considering the circular bit sequence \(s_0, s_1, \ldots, s_7\) (in the order from item 3), count how many consecutive pairs adjacent in the circular sequence (including the pair \(s_7, s_0\)) differ from each other.
  7. Classification: If the number of transitions is \(\le 2\), classify as UNIFORME; otherwise, classify as NAO_UNIFORME.
  8. Output: For each neighborhood, in input order, print the LBP code (decimal integer, \(0\)–\(255\)), the number of transitions, and the classification.

7.18.4.2 📌 Computational Constraints

  • Fixed neighbor order: The order from item 3 is mandatory — reversing it produces a numerically different code, even though it represents the same visual pattern.
  • Non-strict comparison: \(s(z) = 1\) when \(z \ge 0\) (the chapter itself defines equality as included in the 1 case).
  • Circular counting: Do not forget the pair that closes the cycle (\(s_7\) with \(s_0\)); ignoring this pair is a common mistake that incorrectly classifies uniform patterns.

7.18.4.3 🧠 Theoretical Foundation

Pattern (bits \(s_0\ldots s_7\)) Transitions Interpretation
00000000 or 11111111 0 Homogeneous region (light or dark patch)
00001111 2 Simple edge between two regions
01010101 8 Alternating contrast texture — non-uniform

Uniform patterns are concentrated in smooth texture regions or simple edges; non-uniform patterns tend to correspond to high-frequency noise. For this reason, the uniform LBP histogram, used in the texture classification project, groups all non-uniform patterns into a single bin, reducing the dimensionality of the descriptor.

7.18.4.4 📦 Input and Output Specification (VPL)

Input:

  • Line 1: Integer \(T\).
  • For each neighborhood: 3 lines with 3 integers each (\(3\times3\) matrix).

Output:

  • \(T\) lines, in the format LBP=<int> transicoes=<int> <UNIFORME|NAO_UNIFORME>.

7.18.4.5 📌 Examples

Input Output Observation
1
10 10 10
10 50 10
10 10 10
LBP=0 transicoes=0 UNIFORME Center is the brightest; all neighbors generate bit 0.
1
90 90 90
10 50 10
90 90 90
LBP=119 transicoes=4 NAO_UNIFORME Light and dark neighbors alternate in the neighborhood.
🎮 Simulator EP07_04: LBP Code of a Neighborhood 3×3 P = 8, R = 1
Click a cell in the neighborhood to toggle between light and dark (the center is fixed) and observe the resulting LBP code. The label p indicates the equation index.
–
Figure 7.24: EP07_04 Simulator: LBP Code of a 3x3 Neighborhood
%%writefile EP07_04.py
# Python code
Overwriting EP07_04.py
TestSuite("EP07_04.py").run()
✔️ EP07_04.cases already exists in casos/
📋 5 case(s) loaded from casos/EP07_04.cases

🔍 Testing Python: EP07_04.py
⚠️ EP07_04.py: Empty file (fewer than 3 lines). Tests skipped.