Skip to content

Add Digital Image Processing Algorithm: Local Binary Pattern #6294

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 28 commits into from
Aug 24, 2022
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
5f2eb9f
add algorithm local binary pattern
zhexuanl Aug 4, 2022
31eadf6
fix failed test for local binary pattern
zhexuanl Aug 4, 2022
a7a1532
updating DIRECTORY.md
Aug 4, 2022
afe2320
fix detected precommit-error
zhexuanl Aug 4, 2022
e0cd687
Merge branch 'master' of https://github.com/zhexuanl/Python
zhexuanl Aug 4, 2022
a902666
fix precommit error
zhexuanl Aug 4, 2022
097d9ce
final check
zhexuanl Aug 4, 2022
d4a9574
Add descriptive name for parameters x and y
zhexuanl Aug 4, 2022
d0343fe
Update digital_image_processing/filters/local_binary_pattern.py
zhexuanl Aug 6, 2022
df151ba
Update digital_image_processing/filters/local_binary_pattern.py
zhexuanl Aug 6, 2022
5618350
Update digital_image_processing/filters/local_binary_pattern.py
zhexuanl Aug 6, 2022
be25fd6
Update local_binary_pattern.py
zhexuanl Aug 6, 2022
d281c65
undo changes made on get_neighbors_pixel()
zhexuanl Aug 6, 2022
16464f1
Merge branch 'TheAlgorithms:master' into master
zhexuanl Aug 6, 2022
18a50dd
files formatted by black
zhexuanl Aug 6, 2022
9afa416
Update digital_image_processing/filters/local_binary_pattern.py
zhexuanl Aug 6, 2022
2744b83
add test for get_neighbors_pixel() function
zhexuanl Aug 6, 2022
5eec711
reviewed
zhexuanl Aug 6, 2022
8611328
fix get_neighbors_pixel
zhexuanl Aug 7, 2022
484c508
Update test_digital_image_processing.py
zhexuanl Aug 7, 2022
0510fb4
Merge branch 'TheAlgorithms:master' into master
zhexuanl Aug 10, 2022
3b1c292
Merge branch 'TheAlgorithms:master' into master
zhexuanl Aug 24, 2022
7576297
updating DIRECTORY.md
Aug 24, 2022
36e2d0b
Create code_quality.yml
zhexuanl Aug 24, 2022
18d8ed0
Create code_quality.yml
zhexuanl Aug 24, 2022
b791837
Delete code_quality.yml
zhexuanl Aug 24, 2022
2bdac45
Update code_quality.yml
zhexuanl Aug 24, 2022
f587a05
Delete code_quality.yml
cclauss Aug 24, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@
* [Convolve](digital_image_processing/filters/convolve.py)
* [Gabor Filter](digital_image_processing/filters/gabor_filter.py)
* [Gaussian Filter](digital_image_processing/filters/gaussian_filter.py)
* [Local Binary Pattern](digital_image_processing/filters/local_binary_pattern.py)
* [Median Filter](digital_image_processing/filters/median_filter.py)
* [Sobel Filter](digital_image_processing/filters/sobel_filter.py)
* Histogram Equalization
Expand Down
81 changes: 81 additions & 0 deletions digital_image_processing/filters/local_binary_pattern.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import cv2
import numpy as np


def get_neighbors_pixel(
image: np.ndarray, x_coordinate: int, y_coordinate: int, center: int
) -> int:
"""
Comparing local neighborhood pixel value with threshold value of centre pixel.
Exception is required when neighborhood value of a center pixel value is null.
i.e. values present at boundaries.

:param image: The image we're working with
:param x_coordinate: x-coordinate of the pixel
:param y_coordinate: The y coordinate of the pixel
:param center: center pixel value
:return: The value of the pixel is being returned.
"""
value = 0

try:
if image[x_coordinate][y_coordinate] >= center:
value = 1
except Exception:
pass

return value


def local_binary_value(image: np.ndarray, x_coordinate: int, y_coordinate: int) -> int:
"""
It takes an image, an x and y coordinate, and returns the
decimal value of the local binary patternof the pixel
at that coordinate

:param image: the image to be processed
:param x_coordinate: x coordinate of the pixel
:param y_coordinate: the y coordinate of the pixel
:return: The decimal value of the binary value of the pixels
around the center pixel.
"""
center = image[x_coordinate][y_coordinate]
powers = [1, 2, 4, 8, 16, 32, 64, 128]

# Starting from the top right, assigning value to pixels clockwise
binary_values = [
get_neighbors_pixel(image, x_coordinate - 1, y_coordinate + 1, center),
get_neighbors_pixel(image, x_coordinate, y_coordinate + 1, center),
get_neighbors_pixel(image, x_coordinate - 1, y_coordinate, center),
get_neighbors_pixel(image, x_coordinate + 1, y_coordinate + 1, center),
get_neighbors_pixel(image, x_coordinate + 1, y_coordinate, center),
get_neighbors_pixel(image, x_coordinate + 1, y_coordinate - 1, center),
get_neighbors_pixel(image, x_coordinate, y_coordinate - 1, center),
get_neighbors_pixel(image, x_coordinate - 1, y_coordinate - 1, center),
]

# Converting the binary value to decimal.
return sum(
binary_value * power for binary_value, power in zip(binary_values, powers)
)


if __name__ == "main":

# Reading the image and converting it to grayscale.
image = cv2.imread(
"digital_image_processing/image_data/lena.jpg", cv2.IMREAD_GRAYSCALE
)

# Create a numpy array as the same height and width of read image
lbp_image = np.zeros((image.shape[0], image.shape[1]))

# Iterating through the image and calculating the
# local binary pattern value for each pixel.
for i in range(0, image.shape[0]):
for j in range(0, image.shape[1]):
lbp_image[i][j] = local_binary_value(image, i, j)

cv2.imshow("Local Binary Pattern", lbp_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
21 changes: 21 additions & 0 deletions digital_image_processing/test_digital_image_processing.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
PyTest's for Digital Image Processing
"""
import numpy as np
from cv2 import COLOR_BGR2GRAY, cvtColor, imread
from numpy import array, uint8
from PIL import Image
Expand All @@ -15,6 +16,8 @@
from digital_image_processing.filters import median_filter as med
from digital_image_processing.filters import sobel_filter as sob
from digital_image_processing.resize import resize as rs
from digital_image_processing.filters import local_binary_pattern as lbp


img = imread(r"digital_image_processing/image_data/lena_small.jpg")
gray = cvtColor(img, COLOR_BGR2GRAY)
Expand Down Expand Up @@ -91,3 +94,21 @@ def test_nearest_neighbour(
nn = rs.NearestNeighbour(imread(file_path, 1), 400, 200)
nn.process()
assert nn.output.any()


def test_local_binary_pattern():
file_path: str = "digital_image_processing/image_data/lena.jpg"

# Reading the image and converting it to grayscale.
image = imread(file_path, 0)

# Create a numpy array as the same height and width of read image
lbp_image = np.zeros((image.shape[0], image.shape[1]))

# Iterating through the image and calculating the local binary pattern value
# for each pixel.
for i in range(0, image.shape[0]):
for j in range(0, image.shape[1]):
lbp_image[i][j] = lbp.local_binary_value(image, i, j)

assert lbp_image.any()