-
-
Notifications
You must be signed in to change notification settings - Fork 47.1k
Added morphological operations, fixes: #5197 #5199
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 all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
5c908c7
Added morphological operations, fixes: #5197
Spnetic-5 e195500
Added dilation tests and type hints
Spnetic-5 e625df3
Added erosion tests and type hints
Spnetic-5 f07017c
fixes: TheAlgorithms#5197
Spnetic-5 555cc5f
fixes: TheAlgorithms#5197
Spnetic-5 0913eb2
Update erosion_operation.py
Spnetic-5 eb0b4f5
made suggested changes in dilation
Spnetic-5 d2146b8
made suggested changes in erosion
Spnetic-5 dffb122
made suggested changes in dilation
Spnetic-5 5c4c622
removed extra spaces in the tests
Spnetic-5 09a845e
removed extra spaces in the tests
Spnetic-5 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
74 changes: 74 additions & 0 deletions
74
digital_image_processing/morphological_operations/dilation_operation.py
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,74 @@ | ||
import numpy as np | ||
from PIL import Image | ||
|
||
|
||
def rgb2gray(rgb: np.array) -> np.array: | ||
""" | ||
Return gray image from rgb image | ||
>>> rgb2gray(np.array([[[127, 255, 0]]])) | ||
array([[187.6453]]) | ||
>>> rgb2gray(np.array([[[0, 0, 0]]])) | ||
array([[0.]]) | ||
>>> rgb2gray(np.array([[[2, 4, 1]]])) | ||
array([[3.0598]]) | ||
>>> rgb2gray(np.array([[[26, 255, 14], [5, 147, 20], [1, 200, 0]]])) | ||
array([[159.0524, 90.0635, 117.6989]]) | ||
""" | ||
r, g, b = rgb[:, :, 0], rgb[:, :, 1], rgb[:, :, 2] | ||
return 0.2989 * r + 0.5870 * g + 0.1140 * b | ||
|
||
|
||
def gray2binary(gray: np.array) -> np.array: | ||
""" | ||
Return binary image from gray image | ||
>>> gray2binary(np.array([[127, 255, 0]])) | ||
array([[False, True, False]]) | ||
>>> gray2binary(np.array([[0]])) | ||
array([[False]]) | ||
>>> gray2binary(np.array([[26.2409, 4.9315, 1.4729]])) | ||
array([[False, False, False]]) | ||
>>> gray2binary(np.array([[26, 255, 14], [5, 147, 20], [1, 200, 0]])) | ||
array([[False, True, False], | ||
[False, True, False], | ||
[False, True, False]]) | ||
""" | ||
return (127 < gray) & (gray <= 255) | ||
|
||
|
||
def dilation(image: np.array, kernel: np.array) -> np.array: | ||
""" | ||
Return dilated image | ||
>>> dilation(np.array([[True, False, True]]), np.array([[0, 1, 0]])) | ||
array([[False, False, False]]) | ||
>>> dilation(np.array([[False, False, True]]), np.array([[1, 0, 1]])) | ||
array([[False, False, False]]) | ||
""" | ||
output = np.zeros_like(image) | ||
image_padded = np.zeros( | ||
(image.shape[0] + kernel.shape[0] - 1, image.shape[1] + kernel.shape[1] - 1) | ||
) | ||
|
||
# Copy image to padded image | ||
image_padded[kernel.shape[0] - 2 : -1 :, kernel.shape[1] - 2 : -1 :] = image | ||
|
||
# Iterate over image & apply kernel | ||
for x in range(image.shape[1]): | ||
for y in range(image.shape[0]): | ||
summation = ( | ||
kernel * image_padded[y : y + kernel.shape[0], x : x + kernel.shape[1]] | ||
).sum() | ||
output[y, x] = int(summation > 0) | ||
return output | ||
|
||
|
||
# kernel to be applied | ||
structuring_element = np.array([[0, 1, 0], [1, 1, 1], [0, 1, 0]]) | ||
|
||
|
||
if __name__ == "__main__": | ||
# read original image | ||
image = np.array(Image.open(r"..\image_data\lena.jpg")) | ||
output = dilation(gray2binary(rgb2gray(image)), structuring_element) | ||
# Save the output image | ||
pil_img = Image.fromarray(output).convert("RGB") | ||
pil_img.save("result_dilation.png") |
74 changes: 74 additions & 0 deletions
74
digital_image_processing/morphological_operations/erosion_operation.py
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,74 @@ | ||
import numpy as np | ||
from PIL import Image | ||
|
||
|
||
def rgb2gray(rgb: np.array) -> np.array: | ||
""" | ||
Return gray image from rgb image | ||
>>> rgb2gray(np.array([[[127, 255, 0]]])) | ||
array([[187.6453]]) | ||
>>> rgb2gray(np.array([[[0, 0, 0]]])) | ||
array([[0.]]) | ||
>>> rgb2gray(np.array([[[2, 4, 1]]])) | ||
array([[3.0598]]) | ||
>>> rgb2gray(np.array([[[26, 255, 14], [5, 147, 20], [1, 200, 0]]])) | ||
array([[159.0524, 90.0635, 117.6989]]) | ||
""" | ||
r, g, b = rgb[:, :, 0], rgb[:, :, 1], rgb[:, :, 2] | ||
return 0.2989 * r + 0.5870 * g + 0.1140 * b | ||
|
||
|
||
def gray2binary(gray: np.array) -> np.array: | ||
""" | ||
Return binary image from gray image | ||
>>> gray2binary(np.array([[127, 255, 0]])) | ||
array([[False, True, False]]) | ||
>>> gray2binary(np.array([[0]])) | ||
array([[False]]) | ||
>>> gray2binary(np.array([[26.2409, 4.9315, 1.4729]])) | ||
array([[False, False, False]]) | ||
>>> gray2binary(np.array([[26, 255, 14], [5, 147, 20], [1, 200, 0]])) | ||
array([[False, True, False], | ||
[False, True, False], | ||
[False, True, False]]) | ||
""" | ||
return (127 < gray) & (gray <= 255) | ||
|
||
|
||
def erosion(image: np.array, kernel: np.array) -> np.array: | ||
""" | ||
Return eroded image | ||
>>> erosion(np.array([[True, True, False]]), np.array([[0, 1, 0]])) | ||
array([[False, False, False]]) | ||
>>> erosion(np.array([[True, False, False]]), np.array([[1, 1, 0]])) | ||
array([[False, False, False]]) | ||
""" | ||
output = np.zeros_like(image) | ||
image_padded = np.zeros( | ||
(image.shape[0] + kernel.shape[0] - 1, image.shape[1] + kernel.shape[1] - 1) | ||
) | ||
|
||
# Copy image to padded image | ||
image_padded[kernel.shape[0] - 2 : -1 :, kernel.shape[1] - 2 : -1 :] = image | ||
|
||
# Iterate over image & apply kernel | ||
for x in range(image.shape[1]): | ||
for y in range(image.shape[0]): | ||
summation = ( | ||
kernel * image_padded[y : y + kernel.shape[0], x : x + kernel.shape[1]] | ||
).sum() | ||
output[y, x] = int(summation == 5) | ||
return output | ||
|
||
|
||
# kernel to be applied | ||
structuring_element = np.array([[0, 1, 0], [1, 1, 1], [0, 1, 0]]) | ||
|
||
if __name__ == "__main__": | ||
# read original image | ||
image = np.array(Image.open(r"..\image_data\lena.jpg")) | ||
# Apply erosion operation to a binary image | ||
output = erosion(gray2binary(rgb2gray(image)), structuring_element) | ||
# Save the output image | ||
pil_img = Image.fromarray(output).convert("RGB") | ||
pil_img.save("result_erosion.png") |
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.