Breaking

Be Strong Let's Try!

Friday, 10 September 2021

What Is Local Binary Patterns ? How To Perform This Operation In Python | Digital Image Processing

 Objective: 

The objective of this topic is to develop an understanding of local binary patterns.

 Theory: 

Local Binary Patterns operator is an image operator which transforms an image into an array or image of integer labels describing small scale appearance of the image.

What Is Local Binary Patterns ? How To Perform This Operation In Python | Digital Image Processing


For LBP, the pixel under observation acts as the threshold for its neighboring pixels. There are lots of different types of texture descriptors are used to extract features of an image. Local Binary Pattern, also known as LBP, is a simple and grey-scale invariant texture descriptor measure for classification. In LBP, a binary code is generated at each pixel by thresholding it’s neighbor hood pixels to either 0 or 1 based on the value of the centers pixel.

Steps: 

Following are steps to find Local Binary Pattern of an image. 

i. Set a pixel value as center pixel. 

ii. Collect its neighbor hood pixels (eg:3 x 3 matrix so; total number of neighbor hood pixel is 8) 

iii. Threshold it’s neighbourhood pixel value to 1 if its value is greater than or equal to center pixel value otherwise threshold it to 0. 

iv. After thresholding, collect all threshold values from neighbourhood either clockwise or anti-clockwise. The collection will give you an 8-digit binary code. Convert the binary code into decimal.

v. Replace the center pixel value with resulted decimal and do the same process for all pixel values present in image.

Here are the some task which will help us to understand this topic more widely.

1: Calculate the Local Binary Pattern of given image by following all the steps that are mentioned above.

What Is Local Binary Patterns ? How To Perform This Operation In Python | Digital Image Processing



Code:

import numpy as np
import cv2 as cv
#padding input image
def padding(img, paddingsize):
    padded = np.pad(img, (paddingsize, paddingsize),
"constant")
   
return padded
#Local Binary Pattern function
def LBP(img, paddingsize):
    dims = np.shape(img)       
#shape of input image
   
rows = dims[0]              #rows of input image
   
cols = dims[1]              #cols of input image
   
lastrow = rows - paddingsize  # last row
   
lastcol = cols - paddingsize  # last column
   
for i in range(paddingsize, lastrow):
       
for j in range(paddingsize, lastcol):
            window[:] = img[i-paddingsize:i+paddingsize+
1,j-paddingsize :j+paddingsize+1]  # window size 3*3
           
center = window[1][1]   #find center value
           
window = np.array(window)  # convert to array
            # put a zero if value is less than the centre value else put one
           
if window[0,0]<center: 
                a =
0
           
if window[0,0]>=center:   
                a =
1
           
if window[0,1]<center:
                b =
0
           
if window[0,1]>=center:
                b =
1
           
if window[0,2]<center:
                c =
0
           
if window[0,2]>=center:
                c =
1
           
if window[1,0]<center:
                d =
0
           
if window[1,0]>=center:
                d =
1
           
if window[1,2]<center:
                e =
0
           
if window[1,2]>=center:
                e =
1
           
if window[2,0]<center:
                f =
0
           
if window[2,0]>=center:
                f =
1
           
if window[2,1]<center:
                g =
0
           
if window[2,1]>=center:
                g =
1
           
if window[2,2]<center:
                h =
0
           
if window[2,2]>=center:
                h =
1
           
Arraynew = [a,b,c,d,e,f,g,h]
            Arraynew = np.array(Arraynew)
            weightedarray = [
128, 64, 32, 16, 8, 4, 2, 1]  #define a weighted array
           
weightedarray = np.array(weightedarray)           
            out = Arraynew*weightedarray 
            FinalCenter = np.sum(out)
            Output[i,j] = FinalCenter 
   
return Output

#main program
img = cv.imread(‘freestudystrom/Human.png' , 0)  # read input image
paddingsize = 1
padded=padding(img,paddingsize) #padd the image
Output=np.zeros(shape=(np.shape(padded)), dtype='uint8')   #definig the output image same as size of padded image
Output = LBP(padded, paddingsize)  # call the function to find output
cv.imshow('Local Binary Pattern', Output)  # display output LBP image
cv.waitKey(0)


Output:

What Is Local Binary Patterns ? How To Perform This Operation In Python | Digital Image Processing

2: You are required to find LBP of each given image then calculate histogram of each output LBP image to get feature vector. Your feature vector size would be 1*256.

What Is Local Binary Patterns ? How To Perform This Operation In Python | Digital Image Processing


Code:

import numpy as np
import cv2 as cv
from matplotlib import pyplot as plt
#padding input image
def padding(img, paddingsize):
    padded = np.pad(img, (paddingsize, paddingsize),
"constant")
   
return padded
#Local Binary Pattern function
def LBP(img, paddingsize):
    dims = np.shape(img)       
#shape of input image
   
rows = dims[0]              #rows of input image
   
cols = dims[1]              #cols of input image
   
lastrow = rows - paddingsize  # last row
   
lastcol = cols - paddingsize  # last column
   
for i in range(paddingsize, lastrow):
       
for j in range(paddingsize, lastcol):
            window[:] = img[i-paddingsize:i+paddingsize+
1,j-paddingsize:j+paddingsize+1# window size 3*3
           
center = window[1][1]   #find center value
           
window = np.array(window)  # convert to array
            # put a zero if value is less than the centre value else put one
           
if window[0,0]<center:
                a =
0
           
if window[0,0]>=center:
                a =
1
           
if window[0,1]<center:
                b =
0
           
if window[0,1]>=center:
                b =
1
           
if window[0,2]<center:
                c =
0
           
if window[0,2]>=center:
                c =
1
           
if window[1,0]<center:
                d =
0
           
if window[1,0]>=center:
                d =
1
           
if window[1,2]<center:
                e =
0
           
if window[1,2]>=center:
                e =
1
           
if window[2,0]<center:
                f =
0
           
if window[2,0]>=center:
                f =
1
           
if window[2,1]<center:
                g =
0
           
if window[2,1]>=center:
                g =
1
           
if window[2,2]<center:
                h =
0
           
if window[2,2]>=center:
                h =
1
           
Arraynew = [a,b,c,d,e,f,g,h]
            Arraynew = np.array(Arraynew)
            weightedarray = [
128, 64, 32, 16, 8, 4, 2, 1#define a weighted array
           
weightedarray = np.array(weightedarray)
            out = Arraynew*weightedarray
            FinalCenter = np.sum(out)
            Output[i,j] = FinalCenter
   
return Output

#main program
img = cv.imread('freestudystrom/chip.tif' , 0# read input image
paddingsize = 1
padded=padding(img,paddingsize) #padd the image
Output=np.zeros(shape=(np.shape(padded)), dtype='uint8')   #definig the output image same as size of padded image
Output = LBP(padded, paddingsize)  # call the function to find output
cv.imshow('Local Binary Pattern', Output)  # display output LBP image
plt.hist(Output.ravel(),256,[0,256])         #plot the histogram ; feature vector 1*256
plt.title('Histogram')
plt.show()
cv.waitKey(
0)
The code is run for all three images  

Output:

What Is Local Binary Patterns ? How To Perform This Operation In Python | Digital Image Processing
What Is Local Binary Patterns ? How To Perform This Operation In Python | Digital Image Processing

What Is Local Binary Patterns ? How To Perform This Operation In Python | Digital Image Processing
What Is Local Binary Patterns ? How To Perform This Operation In Python | Digital Image Processing

3: Construct feature vector of all images given in task 2. You feature vector should be 1*64.


Code:

import numpy as np
import cv2 as cv
from matplotlib import pyplot as plt
#padding input image
def padding(img, paddingsize):
    padded = np.pad(img, (paddingsize, paddingsize),
"constant")
   
return padded
#Local Binary Pattern function
def LBP(img, paddingsize):
    dims = np.shape(img)       
#shape of input image
   
rows = dims[0]              #rows of input image
   
cols = dims[1]              #cols of input image
   
lastrow = rows - paddingsize  # last row
   
lastcol = cols - paddingsize  # last column
   
for i in range(paddingsize, lastrow):
       
for j in range(paddingsize, lastcol):
            window[:] = img[i-paddingsize:i+paddingsize+
1,j-paddingsize:j+paddingsize+1# window size 3*3
           
center = window[1][1]   #find center value
           
window = np.array(window)  # convert to array
            # put a zero if value is less than the centre value else put one
           
if window[0,0]<center:
                a =
0
           
if window[0,0]>=center:
                a =
1
           
if window[0,1]<center:
                b =
0
           
if window[0,1]>=center:
                b =
1
           
if window[0,2]<center:
                c =
0
           
if window[0,2]>=center:
                c =
1
           
if window[1,0]<center:
                d =
0
           
if window[1,0]>=center:
                d =
1
           
if window[1,2]<center:
                e =
0
           
if window[1,2]>=center:
                e =
1
           
if window[2,0]<center:
                f =
0
           
if window[2,0]>=center:
                f =
1
           
if window[2,1]<center:
                g =
0
           
if window[2,1]>=center:
                g =
1
           
if window[2,2]<center:
                h =
0
           
if window[2,2]>=center:
                h =
1
           
Arraynew = [a,b,c,d,e,f,g,h]
            Arraynew = np.array(Arraynew)
            weightedarray = [
128, 64, 32, 16, 8, 4, 2, 1#define a weighted array
           
weightedarray = np.array(weightedarray)
            out = Arraynew*weightedarray
            FinalCenter = np.sum(out)
            Output[i,j] = FinalCenter
   
return Output

#main program
img = cv.imread(‘freestudystrom/chip.tif' , 0# read input image
paddingsize = 1
padded=padding(img,paddingsize) #padd the image
Output=np.zeros(shape=(np.shape(padded)), dtype='uint8')   #definig the output image same as size of padded image
Output = LBP(padded, paddingsize)  # call the function to find output
cv.imshow('Local Binary Pattern', Output)  # display output LBP image
plt.hist(Output.ravel(),64,[0,256])         #plot the histogram ; feature vector 1*64
plt.title('Histogram')
plt.show()
cv.waitKey(
0)
The code is run for all three images  

Output:

Chip Image

What Is Local Binary Patterns ? How To Perform This Operation In Python | Digital Image Processing

TV Noise Image

What Is Local Binary Patterns ? How To Perform This Operation In Python | Digital Image Processing

Vertical Line Pattern Image

What Is Local Binary Patterns ? How To Perform This Operation In Python | Digital Image Processing


2 comments:

Pages