Skip to content

Commit 5830b29

Browse files
tianyizheng02github-actions
and
github-actions
authored
Fix mypy errors in erosion_operation.py (TheAlgorithms#8603)
* updating DIRECTORY.md * Fix mypy errors in erosion_operation.py * Rename functions to use snake case * updating DIRECTORY.md * updating DIRECTORY.md * Replace raw file string with pathlib Path * Fix function name in erosion_operation.py doctest --------- Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
1 parent 84ec941 commit 5830b29

File tree

1 file changed

+23
-16
lines changed

1 file changed

+23
-16
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,48 @@
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+
11+
>>> rgb_to_gray(np.array([[[127, 255, 0]]]))
912
array([[187.6453]])
10-
>>> rgb2gray(np.array([[[0, 0, 0]]]))
13+
>>> rgb_to_gray(np.array([[[0, 0, 0]]]))
1114
array([[0.]])
12-
>>> rgb2gray(np.array([[[2, 4, 1]]]))
15+
>>> rgb_to_gray(np.array([[[2, 4, 1]]]))
1316
array([[3.0598]])
14-
>>> rgb2gray(np.array([[[26, 255, 14], [5, 147, 20], [1, 200, 0]]]))
17+
>>> rgb_to_gray(np.array([[[26, 255, 14], [5, 147, 20], [1, 200, 0]]]))
1518
array([[159.0524, 90.0635, 117.6989]])
1619
"""
1720
r, g, b = rgb[:, :, 0], rgb[:, :, 1], rgb[:, :, 2]
1821
return 0.2989 * r + 0.5870 * g + 0.1140 * b
1922

2023

21-
def gray2binary(gray: np.array) -> np.array:
24+
def gray_to_binary(gray: np.ndarray) -> np.ndarray:
2225
"""
2326
Return binary image from gray image
2427
25-
>>> gray2binary(np.array([[127, 255, 0]]))
28+
>>> gray_to_binary(np.array([[127, 255, 0]]))
2629
array([[False, True, False]])
27-
>>> gray2binary(np.array([[0]]))
30+
>>> gray_to_binary(np.array([[0]]))
2831
array([[False]])
29-
>>> gray2binary(np.array([[26.2409, 4.9315, 1.4729]]))
32+
>>> gray_to_binary(np.array([[26.2409, 4.9315, 1.4729]]))
3033
array([[False, False, False]])
31-
>>> gray2binary(np.array([[26, 255, 14], [5, 147, 20], [1, 200, 0]]))
34+
>>> gray_to_binary(np.array([[26, 255, 14], [5, 147, 20], [1, 200, 0]]))
3235
array([[False, True, False],
3336
[False, True, False],
3437
[False, True, False]])
3538
"""
3639
return (gray > 127) & (gray <= 255)
3740

3841

39-
def erosion(image: np.array, kernel: np.array) -> np.array:
42+
def erosion(image: np.ndarray, kernel: np.ndarray) -> np.ndarray:
4043
"""
4144
Return eroded image
45+
4246
>>> erosion(np.array([[True, True, False]]), np.array([[0, 1, 0]]))
4347
array([[False, False, False]])
4448
>>> erosion(np.array([[True, False, False]]), np.array([[1, 1, 0]]))
@@ -62,14 +66,17 @@ def erosion(image: np.array, kernel: np.array) -> np.array:
6266
return output
6367

6468

65-
# kernel to be applied
66-
structuring_element = np.array([[0, 1, 0], [1, 1, 1], [0, 1, 0]])
67-
6869
if __name__ == "__main__":
6970
# read original image
70-
image = np.array(Image.open(r"..\image_data\lena.jpg"))
71+
lena_path = Path(__file__).resolve().parent / "image_data" / "lena.jpg"
72+
lena = np.array(Image.open(lena_path))
73+
74+
# kernel to be applied
75+
structuring_element = np.array([[0, 1, 0], [1, 1, 1], [0, 1, 0]])
76+
7177
# Apply erosion operation to a binary image
72-
output = erosion(gray2binary(rgb2gray(image)), structuring_element)
78+
output = erosion(gray_to_binary(rgb_to_gray(lena)), structuring_element)
79+
7380
# Save the output image
7481
pil_img = Image.fromarray(output).convert("RGB")
7582
pil_img.save("result_erosion.png")

0 commit comments

Comments
 (0)