importcv2importnumpyasnpfrommatplotlibimportpyplotaspltdefget_pixel(img,center,x,y):new_value=0try:# If local neighbourhood pixel# value is greater than or equal# to center pixel values then# set it to 1ifimg[x][y]>=center:new_value=1except:# Exception is required when# neighbourhood value of a center# pixel value is null i.e. values# present at boundaries.passreturnnew_value# Function for calculating LBPdeflbp_calculated_pixel(img,x,y):center=img[x][y]val_ar=[]# top_leftval_ar.append(get_pixel(img,center,x-1,y-1))# topval_ar.append(get_pixel(img,center,x-1,y))# top_rightval_ar.append(get_pixel(img,center,x-1,y+1))# rightval_ar.append(get_pixel(img,center,x,y+1))# bottom_rightval_ar.append(get_pixel(img,center,x+1,y+1))# bottomval_ar.append(get_pixel(img,center,x+1,y))# bottom_leftval_ar.append(get_pixel(img,center,x+1,y-1))# leftval_ar.append(get_pixel(img,center,x,y-1))# Now, we need to convert binary# values to decimalpower_val=[1,2,4,8,16,32,64,128]val=0foriinrange(len(val_ar)):val+=val_ar[i]*power_val[i]returnvalpath='GFG.png'img_bgr=cv2.imread(path,1)height,width,_=img_bgr.shape# We need to convert RGB image# into gray one because gray# image has one channel only.img_gray=cv2.cvtColor(img_bgr,cv2.COLOR_BGR2GRAY)# Create a numpy array as# the same height and width# of RGB imageimg_lbp=np.zeros((height,width),np.uint8)foriinrange(0,height):forjinrange(0,width):img_lbp[i,j]=lbp_calculated_pixel(img_gray,i,j)plt.imshow(img_bgr)plt.show()plt.imshow(img_lbp,cmap="gray")plt.show()print("LBP Program is finished")