Skip to content

Commit a213cea

Browse files
tianyizheng02github-actionscclauss
authored
Fix mypy errors in dilation_operation.py (TheAlgorithms#8595)
* updating DIRECTORY.md * Fix mypy errors in dilation_operation.py * Rename functions to use snake case * updating DIRECTORY.md * updating DIRECTORY.md * Replace raw file string with pathlib Path * Update digital_image_processing/morphological_operations/dilation_operation.py Co-authored-by: Christian Clauss <[email protected]> --------- Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Christian Clauss <[email protected]>
1 parent 59cae16 commit a213cea

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

digital_image_processing/morphological_operations/dilation_operation.py

+18-17
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,43 @@
1+
from pathlib import Path
2+
13
import numpy as np
24
from PIL import Image
35

46

5-
def rgb2gray(rgb: np.array) -> np.array:
7+
def rgb_to_gray(rgb: np.ndarray) -> np.ndarray:
68
"""
79
Return gray image from rgb image
8-
>>> rgb2gray(np.array([[[127, 255, 0]]]))
10+
>>> rgb_to_gray(np.array([[[127, 255, 0]]]))
911
array([[187.6453]])
10-
>>> rgb2gray(np.array([[[0, 0, 0]]]))
12+
>>> rgb_to_gray(np.array([[[0, 0, 0]]]))
1113
array([[0.]])
12-
>>> rgb2gray(np.array([[[2, 4, 1]]]))
14+
>>> rgb_to_gray(np.array([[[2, 4, 1]]]))
1315
array([[3.0598]])
14-
>>> rgb2gray(np.array([[[26, 255, 14], [5, 147, 20], [1, 200, 0]]]))
16+
>>> rgb_to_gray(np.array([[[26, 255, 14], [5, 147, 20], [1, 200, 0]]]))
1517
array([[159.0524, 90.0635, 117.6989]])
1618
"""
1719
r, g, b = rgb[:, :, 0], rgb[:, :, 1], rgb[:, :, 2]
1820
return 0.2989 * r + 0.5870 * g + 0.1140 * b
1921

2022

21-
def gray2binary(gray: np.array) -> np.array:
23+
def gray_to_binary(gray: np.ndarray) -> np.ndarray:
2224
"""
2325
Return binary image from gray image
24-
>>> gray2binary(np.array([[127, 255, 0]]))
26+
>>> gray_to_binary(np.array([[127, 255, 0]]))
2527
array([[False, True, False]])
26-
>>> gray2binary(np.array([[0]]))
28+
>>> gray_to_binary(np.array([[0]]))
2729
array([[False]])
28-
>>> gray2binary(np.array([[26.2409, 4.9315, 1.4729]]))
30+
>>> gray_to_binary(np.array([[26.2409, 4.9315, 1.4729]]))
2931
array([[False, False, False]])
30-
>>> gray2binary(np.array([[26, 255, 14], [5, 147, 20], [1, 200, 0]]))
32+
>>> gray_to_binary(np.array([[26, 255, 14], [5, 147, 20], [1, 200, 0]]))
3133
array([[False, True, False],
3234
[False, True, False],
3335
[False, True, False]])
3436
"""
3537
return (gray > 127) & (gray <= 255)
3638

3739

38-
def dilation(image: np.array, kernel: np.array) -> np.array:
40+
def dilation(image: np.ndarray, kernel: np.ndarray) -> np.ndarray:
3941
"""
4042
Return dilated image
4143
>>> dilation(np.array([[True, False, True]]), np.array([[0, 1, 0]]))
@@ -61,14 +63,13 @@ def dilation(image: np.array, kernel: np.array) -> np.array:
6163
return output
6264

6365

64-
# kernel to be applied
65-
structuring_element = np.array([[0, 1, 0], [1, 1, 1], [0, 1, 0]])
66-
67-
6866
if __name__ == "__main__":
6967
# read original image
70-
image = np.array(Image.open(r"..\image_data\lena.jpg"))
71-
output = dilation(gray2binary(rgb2gray(image)), structuring_element)
68+
lena_path = Path(__file__).resolve().parent / "image_data" / "lena.jpg"
69+
lena = np.array(Image.open(lena_path))
70+
# kernel to be applied
71+
structuring_element = np.array([[0, 1, 0], [1, 1, 1], [0, 1, 0]])
72+
output = dilation(gray_to_binary(rgb_to_gray(lena)), structuring_element)
7273
# Save the output image
7374
pil_img = Image.fromarray(output).convert("RGB")
7475
pil_img.save("result_dilation.png")

0 commit comments

Comments
 (0)