Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix mypy errors in dilation_operation.py
  • Loading branch information
tianyizheng02 committed Jan 7, 2023
commit 9a57858c51e7de5111411018365e67f266dc6b36
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from PIL import Image


def rgb2gray(rgb: np.array) -> np.array:
def rgb2gray(rgb: np.ndarray) -> np.ndarray:
"""
Return gray image from rgb image
>>> rgb2gray(np.array([[[127, 255, 0]]]))
Expand All @@ -18,7 +18,7 @@ def rgb2gray(rgb: np.array) -> np.array:
return 0.2989 * r + 0.5870 * g + 0.1140 * b


def gray2binary(gray: np.array) -> np.array:
def gray2binary(gray: np.ndarray) -> np.ndarray:
"""
Return binary image from gray image
>>> gray2binary(np.array([[127, 255, 0]]))
Expand All @@ -35,7 +35,7 @@ def gray2binary(gray: np.array) -> np.array:
return (127 < gray) & (gray <= 255)


def dilation(image: np.array, kernel: np.array) -> np.array:
def dilation(image: np.ndarray, kernel: np.ndarray) -> np.ndarray:
"""
Return dilated image
>>> dilation(np.array([[True, False, True]]), np.array([[0, 1, 0]]))
Expand All @@ -61,14 +61,12 @@ def dilation(image: np.array, kernel: np.array) -> np.array:
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)
lena = np.array(Image.open(r"..\image_data\lena.jpg"))
# kernel to be applied
structuring_element = np.array([[0, 1, 0], [1, 1, 1], [0, 1, 0]])
output = dilation(gray2binary(rgb2gray(lena)), structuring_element)
# Save the output image
pil_img = Image.fromarray(output).convert("RGB")
pil_img.save("result_dilation.png")