Add Digital Image Processing Algorithm: Local Binary Pattern (#6294) · zinating/algorithms-python@b1818af · GitHub
Skip to content

Commit b1818af

Browse files
zhexuanlgithub-actionscclauss
authored
Add Digital Image Processing Algorithm: Local Binary Pattern (TheAlgorithms#6294)
* add algorithm local binary pattern * fix failed test for local binary pattern * updating DIRECTORY.md * fix detected precommit-error * fix precommit error * final check * Add descriptive name for parameters x and y * Update digital_image_processing/filters/local_binary_pattern.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update digital_image_processing/filters/local_binary_pattern.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update digital_image_processing/filters/local_binary_pattern.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update local_binary_pattern.py * undo changes made on get_neighbors_pixel() * files formatted by black * Update digital_image_processing/filters/local_binary_pattern.py ok thanks Co-authored-by: Christian Clauss <cclauss@me.com> * add test for get_neighbors_pixel() function * reviewed * fix get_neighbors_pixel * Update test_digital_image_processing.py * updating DIRECTORY.md * Create code_quality.yml * Create code_quality.yml * Delete code_quality.yml * Update code_quality.yml * Delete code_quality.yml Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Christian Clauss <cclauss@me.com>
1 parent f31fa4e commit b1818af

3 files changed

Lines changed: 115 additions & 0 deletions

File tree

DIRECTORY.md

Lines changed: 2 additions & 0 deletions
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import cv2
2+
import numpy as np
3+
4+
5+
def get_neighbors_pixel(
6+
image: np.ndarray, x_coordinate: int, y_coordinate: int, center: int
7+
) -> int:
8+
"""
9+
Comparing local neighborhood pixel value with threshold value of centre pixel.
10+
Exception is required when neighborhood value of a center pixel value is null.
11+
i.e. values present at boundaries.
12+
13+
:param image: The image we're working with
14+
:param x_coordinate: x-coordinate of the pixel
15+
:param y_coordinate: The y coordinate of the pixel
16+
:param center: center pixel value
17+
:return: The value of the pixel is being returned.
18+
"""
19+
20+
try:
21+
return int(image[x_coordinate][y_coordinate] >= center)
22+
except (IndexError, TypeError):
23+
return 0
24+
25+
26+
def local_binary_value(image: np.ndarray, x_coordinate: int, y_coordinate: int) -> int:
27+
"""
28+
It takes an image, an x and y coordinate, and returns the
29+
decimal value of the local binary patternof the pixel
30+
at that coordinate
31+
32+
:param image: the image to be processed
33+
:param x_coordinate: x coordinate of the pixel
34+
:param y_coordinate: the y coordinate of the pixel
35+
:return: The decimal value of the binary value of the pixels
36+
around the center pixel.
37+
"""
38+
center = image[x_coordinate][y_coordinate]
39+
powers = [1, 2, 4, 8, 16, 32, 64, 128]
40+
41+
# skip get_neighbors_pixel if center is null
42+
if center is None:
43+
return 0
44+
45+
# Starting from the top right, assigning value to pixels clockwise
46+
binary_values = [
47+
get_neighbors_pixel(image, x_coordinate - 1, y_coordinate + 1, center),
48+
get_neighbors_pixel(image, x_coordinate, y_coordinate + 1, center),
49+
get_neighbors_pixel(image, x_coordinate - 1, y_coordinate, center),
50+
get_neighbors_pixel(image, x_coordinate + 1, y_coordinate + 1, center),
51+
get_neighbors_pixel(image, x_coordinate + 1, y_coordinate, center),
52+
get_neighbors_pixel(image, x_coordinate + 1, y_coordinate - 1, center),
53+
get_neighbors_pixel(image, x_coordinate, y_coordinate - 1, center),
54+
get_neighbors_pixel(image, x_coordinate - 1, y_coordinate - 1, center),
55+
]
56+
57+
# Converting the binary value to decimal.
58+
return sum(
59+
binary_value * power for binary_value, power in zip(binary_values, powers)
60+
)
61+
62+
63+
if __name__ == "main":
64+
65+
# Reading the image and converting it to grayscale.
66+
image = cv2.imread(
67+
"digital_image_processing/image_data/lena.jpg", cv2.IMREAD_GRAYSCALE
68+
)
69+
70+
# Create a numpy array as the same height and width of read image
71+
lbp_image = np.zeros((image.shape[0], image.shape[1]))
72+
73+
# Iterating through the image and calculating the
74+
# local binary pattern value for each pixel.
75+
for i in range(0, image.shape[0]):
76+
for j in range(0, image.shape[1]):
77+
lbp_image[i][j] = local_binary_value(image, i, j)
78+
79+
cv2.imshow("local binary pattern", lbp_image)
80+
cv2.waitKey(0)
81+
cv2.destroyAllWindows()

digital_image_processing/test_digital_image_processing.py

Lines changed: 32 additions & 0 deletions

0 commit comments

Comments
 (0)