-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
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
Changes from 7 commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
5f2eb9f
add algorithm local binary pattern
zhexuanl 31eadf6
fix failed test for local binary pattern
zhexuanl a7a1532
updating DIRECTORY.md
afe2320
fix detected precommit-error
zhexuanl e0cd687
Merge branch 'master' of https://github.com/zhexuanl/Python
zhexuanl a902666
fix precommit error
zhexuanl 097d9ce
final check
zhexuanl d4a9574
Add descriptive name for parameters x and y
zhexuanl d0343fe
Update digital_image_processing/filters/local_binary_pattern.py
zhexuanl df151ba
Update digital_image_processing/filters/local_binary_pattern.py
zhexuanl 5618350
Update digital_image_processing/filters/local_binary_pattern.py
zhexuanl be25fd6
Update local_binary_pattern.py
zhexuanl d281c65
undo changes made on get_neighbors_pixel()
zhexuanl 16464f1
Merge branch 'TheAlgorithms:master' into master
zhexuanl 18a50dd
files formatted by black
zhexuanl 9afa416
Update digital_image_processing/filters/local_binary_pattern.py
zhexuanl 2744b83
add test for get_neighbors_pixel() function
zhexuanl 5eec711
reviewed
zhexuanl 8611328
fix get_neighbors_pixel
zhexuanl 484c508
Update test_digital_image_processing.py
zhexuanl 0510fb4
Merge branch 'TheAlgorithms:master' into master
zhexuanl 3b1c292
Merge branch 'TheAlgorithms:master' into master
zhexuanl 7576297
updating DIRECTORY.md
36e2d0b
Create code_quality.yml
zhexuanl 18d8ed0
Create code_quality.yml
zhexuanl b791837
Delete code_quality.yml
zhexuanl 2bdac45
Update code_quality.yml
zhexuanl f587a05
Delete code_quality.yml
cclauss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: int, y: 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: x-coordinate of the center pixel | ||
:param y: 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][y] >= center: | ||
value = 1 | ||
except Exception: | ||
pass | ||
|
||
return value | ||
|
||
|
||
def local_binary_value(image: np.ndarray, x: int, y: int) -> int: | ||
zhexuanl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
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: x coordinate of the pixel | ||
:param y: the y coordinate of the pixel | ||
:return: The decimal value of the binary value of the pixels | ||
around the center pixel. | ||
""" | ||
binary_value = [] | ||
zhexuanl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
center = image[x][y] | ||
powers = [1, 2, 4, 8, 16, 32, 64, 128] | ||
decimal_val = 0 | ||
|
||
# Starting from top right,assigning value to pixels clockwise | ||
binary_value.append(get_neighbors_pixel(image, x - 1, y + 1, center)) | ||
binary_value.append(get_neighbors_pixel(image, x, y + 1, center)) | ||
binary_value.append(get_neighbors_pixel(image, x - 1, y, center)) | ||
binary_value.append(get_neighbors_pixel(image, x + 1, y + 1, center)) | ||
binary_value.append(get_neighbors_pixel(image, x + 1, y, center)) | ||
binary_value.append(get_neighbors_pixel(image, x + 1, y - 1, center)) | ||
binary_value.append(get_neighbors_pixel(image, x, y - 1, center)) | ||
binary_value.append(get_neighbors_pixel(image, x - 1, y - 1, center)) | ||
|
||
# Converting the binary value to decimal. | ||
for i in range(len(binary_value)): | ||
decimal_val += binary_value[i] * powers[i] | ||
|
||
return decimal_val | ||
zhexuanl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.