```python
import numpy as np
import matplotlib.pylab as plt
from numpy.lib.stride_tricks import sliding_window_view
```

## Implementing a Function to Compute the Local Binary Pattern

Local Binary Pattern (LBP) is a simple yet very efficient texture feature commonly used in image processing.

 

\begin{align}
LBP(p) = \sum_{i=0}^{7} [f(n_i) > f(p)]2^i$
\end{align}


```python
def local_binary_pattern_list_comprehension(img):
    neighbors = [(0,2),(1,2),(2,2),\
                 (2,1), (2,0), (1,0),\
                 (0,0),(0,1)]
    H,W = img.shape
    aux_img = np.zeros((H+2,W+2))
    aux_img[1:-1,1:-1] = img
    
    return sum((aux_img[x:H+x,y:W+y] > img)*2**i for (i,(x,y)) in enumerate(neighbors))
```


```python
def local_binary_pattern_loop(img):
    neighbors = [(0,2),(1,2),(2,2),\
                 (2,1), (2,0), (1,0),\
                 (0,0),(0,1)]
    H,W = img.shape
    aux_img = np.zeros((H+2,W+2))
    aux_img[1:-1,1:-1] = img
    lbp = np.zeros(img.shape)
    for i, (x,y) in enumerate(neighbors):
        lbp+= (aux_img[x:H+x,y:W+y] > img)*2**i
    return lbp
```


```python
def lbp_sliding_window_view(img):
    codebook  = np.array([[[[64,128,1],\
                            [32,0,2],\
                            [16,8,4]]]], dtype = np.uint8)
    H,W = img.shape
    aux_img = np.zeros((H+2,W+2))
    aux_img[1:-1,1:-1] = img
    aux_img_view = sliding_window_view(aux_img, (3,3))
    lbp = ((aux_img_view > img[:,:,np.newaxis,np.newaxis])*codebook).sum(axis = (2,3))
    return lbp
```


```python
from PIL import Image

img = np.array(Image.open('../Figures/lena.png').convert('L'))
```


```python
%%timeit

local_binary_pattern_loop(img)
```

    8.39 ms ± 414 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)



```python
%%timeit

lbp_sliding_window_view(img)
```

    24.7 ms ± 1.05 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)



```python
%%timeit

local_binary_pattern_list_comprehension(img)
```

    8.86 ms ± 450 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)


65a48e00caada21038013b87b38aa97d72cb5e19
ipynb
Jupyter Notebook
Submissions/lbp_compiled.ipynb
yash30147101/ENEL645
15b54b0188be3aa214295d5dd37bb51b39a31e58
MIT
Submissions/lbp_compiled.ipynb
yash30147101/ENEL645
15b54b0188be3aa214295d5dd37bb51b39a31e58
MIT
Submissions/lbp_compiled.ipynb
yash30147101/ENEL645
15b54b0188be3aa214295d5dd37bb51b39a31e58
MIT
Qwen/Qwen-72B
1. YES
2. YES