VOOZH about

URL: https://www.geeksforgeeks.org/python/create-local-binary-pattern-of-an-image-using-opencv-python/

โ‡ฑ Create Local Binary Pattern of an image using OpenCV-Python - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Create Local Binary Pattern of an image using OpenCV-Python

Last Updated : 28 Apr, 2025

In this article, we will discuss the image and how to find a binary pattern using the pixel value of the image. As we all know, image is also known as a set of pixels. When we store an image in computers or digitally, itโ€™s corresponding pixel values are stored. So, when we read an image to a variable using OpenCV in Python, the variable stores the pixel values of the image. As we can see in following example: 

Examples: Input : ๐Ÿ‘ Image
Output :

[[[255 255 255]
 [255 255 255]
 [255 255 255]
 ...
 [255 255 255]
 [255 255 255]
 [255 255 255]]

 [[255 255 255]
 [255 255 255]
 [255 255 255]
 ...
 [255 255 255]
 [255 255 255]
 [255 255 255]]

 [[255 255 255]
 [255 255 255]
 [255 255 255]
 ...
 [255 255 255]
 [255 255 255]
 [255 255 255]]

 ...

 [[255 255 255]
 [255 255 255]
 [255 255 255]
 ...
 [255 255 255]
 [255 255 255]
 [255 255 255]]

 [[255 255 255]
 [255 255 255]
 [255 255 255]
 ...
 [255 255 255]
 [255 255 255]
 [255 255 255]]

 [[255 255 255]
 [255 255 255]
 [255 255 255]
 ...
 [255 255 255]
 [255 255 255]
 [255 255 255]]]

Pixel values of the image will be stored in the variable and below is a part of the NumPy array which stores the values.

Local Binary Pattern

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 grayscale invariant texture descriptor measure for classification. In LBP, a binary code is generated at each pixel by thresholding itโ€™s neighbourhood pixels to either 0 or 1 based on the value of the centre pixel. The rule for finding LBP of an image is as follows:

  1. Set a pixel value as center pixel.
  2. Collect its neighbourhood pixels (Here I am taking a 3 x 3 matrix so; total number of neighbourhood pixel is 8)
  3. Threshold itโ€™s neighbourhood pixel value to 1 if its value is greater than or equal to centre pixel value otherwise threshold it to 0.
  4. 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.
  5. Replace the center pixel value with resulted decimal and do the same process for all pixel values present in image.

Letโ€™s take an example to understand it properly. Letโ€™s take a pixel value from the above output to find its binary pattern from its local neighbourhood. So, I am taking a value โ€˜149โ€™ (present at 15th row and 19nd column) and its 8 neighbourhood pixels to form a 3 x 3 matrix. ๐Ÿ‘ Image
Collect the thresholding values either clockwise or anti-clockwise. Here, I am collecting them clockwise from top-left. So, after collecting, the binary value will be as follows: ๐Ÿ‘ Image
Then, convert the binary code into decimal and place it at center of matrix.

1 x 27 + 1 x 26 + 1 x 25 + 0 x 24 + 0 x 23 + 0 x 22 + 0 x 21 +1 x 20 
= 128 + 64 + 32 + 0 + 0 + 0 + 0 + 1
= 225

Now, the resulted matrix will look like, ๐Ÿ‘ Image
Now, letโ€™s do it using python 

Output: ๐Ÿ‘ python-lbp
The output shown in examples contains some values in its X-axis and Y-axis which refers to the width and height of the input image respectively.

Comment
Article Tags: