Search code examples
pythonnumpyimage-processingfeature-extractionlbph-algorithm

Local Gradient Pattern Implementation


I am trying to understand Local Gradient Pattern which is described in Local gradient pattern - A novel feature representation for facial expression recognition.

There is an example of calculating new value of a pixel like below:

pattern calculation

I see that the center pixel's (which is 25) Pattern-1 value is 10 and Pattern-2 value is 01. I have several questions.

  • What will be the new value of that center pixel?
  • How LGP is related with LBP?
  • Is there any pseudo code for converting a 2D matrix using LGP (Python preferred)?

Solution

  • What will be the new value of that center pixel?

    It depends on the encoding scheme. The reference paper does not clearly explain how local gradient patterns are encoded. One possible encoding would be:

    LGP

    where

    f(x)

    If you introduce the intensity values of your example into the expressions above the pattern code results:

    code calculation example

    Please notice that the effect of using a different encoding would be a reordering of the histogram bins, but this would not have an impact on the classification accuracy.

    How LGP is related with LBP?

    LGP is simply one of the many LBP variants. Take a look at this book for a comprehensive review.

    Is there any pseudo code for converting a 2D matrix using LGP (Python preferred)?

    Give this code a try:

    import numpy as np
    
    def LGP_codes(img, r=1):
        padded = np.pad(img, (r, r), 'constant')
        a1 = padded[:-2*r, :-2*r]
        b1 = padded[:-2*r, r:-r]
        a2 = padded[:-2*r, 2*r:]
        b2 = padded[r:-r, 2*r:]
        a3 = padded[2*r:, 2*r:]
        b3 = padded[2*r:, r:-r]
        a4 = padded[2*r:, :-2*r]
        b4 = padded[r:-r, :-2*r]
        codes = (a1 >= a3) + 2*(a2 >= a4) + 4*(b1 >= b3) + 8*(b2 >= b4)
        return codes[r:-r, r:-r]
    

    Demo

    In [31]: patch = np.array([[18, 25, 14], 
        ...:                   [85, 25, 86], 
        ...:                   [45, 65, 14]])
        ...: 
    
    In [32]: LGP_codes(patch)
    Out[32]: array([[9]])