Search code examples
pythonscikit-imagelbph-algorithm

Unexpected output from skimage local_binary_pattern


I calculate the LBP via this exemplary code:

from skimage import feature

radius_lbp = 1
n_points_lbp = 8

temp = feature.local_binary_pattern(regions_arr_lbp[i,j,:], n_points_lbp, radius_lbp, method='uniform').ravel()

whereby regions_arr_lbp[i,j,:] is a 64x64 numpy array with float64 values.

Now for clarification:

  • exam_pixel = the pixel we calculate the LBP for
  • neigh_pixel = the neighborhood pixels of exam_pixel

As far as I understand the LBP, I look at the immediate neighborhood of exam_pixel and compare its intensity with the intensities of its neigh_pixels. When using the method 'uniform', each neigh_pixel whose intensity is greater than exam_pixel's is assigned a 1, and 0 otherwise. This would lead to a theoretical maximum encoded value of 8 for exam_pixel if each pixel in neigh_pixel has a higher intensity.

However, when I run the code above, I get a maximum encoded value of 9 in my temp array multiple times. How is that possible? Have I misunderstood the LBP and how it works?


Solution

  • There are 9 different rotation-invariant uniform patterns for a neighbourhood of 8 pixels, namely:

    • 0: 00000000
    • 1: 00000001
    • 2: 00000011
    • 3: 00000111
    • 4: 00001111
    • 5: 00011111
    • 6: 00111111
    • 7: 01111111
    • 8: 11111111

    For this descriptor to be rotation-invariant the patterns 00000001, 00000010, 00000100, 00001000, 00010000, 00100000, 01000000 and 10000000 are considered the same pattern. The same applies to patterns from 2 to 7.

    All the non-uniform patterns (for example 01010101, 11001100 or 00100100) are dubbed as 9 and merged into the 10-th bin of the LBP histogram.

    Excerpt from the source code of the _local_binary_pattern() function:

                            # 1. Constant patterns patterns (with n_ones=0 and
                            # n_ones=P) and non uniform patterns are given fixed
                            # code values.