2023-09-27 12:00:34 +00:00
|
|
|
from pathlib import Path
|
|
|
|
|
2021-10-16 14:32:40 +00:00
|
|
|
import numpy as np
|
|
|
|
from PIL import Image
|
|
|
|
|
|
|
|
|
2023-09-27 12:00:34 +00:00
|
|
|
def rgb_to_gray(rgb: np.ndarray) -> np.ndarray:
|
2021-10-16 14:32:40 +00:00
|
|
|
"""
|
|
|
|
Return gray image from rgb image
|
2023-09-27 12:00:34 +00:00
|
|
|
|
|
|
|
>>> rgb_to_gray(np.array([[[127, 255, 0]]]))
|
2021-10-16 14:32:40 +00:00
|
|
|
array([[187.6453]])
|
2023-09-27 12:00:34 +00:00
|
|
|
>>> rgb_to_gray(np.array([[[0, 0, 0]]]))
|
2021-10-16 14:32:40 +00:00
|
|
|
array([[0.]])
|
2023-09-27 12:00:34 +00:00
|
|
|
>>> rgb_to_gray(np.array([[[2, 4, 1]]]))
|
2021-10-16 14:32:40 +00:00
|
|
|
array([[3.0598]])
|
2023-09-27 12:00:34 +00:00
|
|
|
>>> rgb_to_gray(np.array([[[26, 255, 14], [5, 147, 20], [1, 200, 0]]]))
|
2021-10-16 14:32:40 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2023-09-27 12:00:34 +00:00
|
|
|
def gray_to_binary(gray: np.ndarray) -> np.ndarray:
|
2021-10-16 14:32:40 +00:00
|
|
|
"""
|
|
|
|
Return binary image from gray image
|
2023-08-15 21:27:41 +00:00
|
|
|
|
2023-09-27 12:00:34 +00:00
|
|
|
>>> gray_to_binary(np.array([[127, 255, 0]]))
|
2021-10-16 14:32:40 +00:00
|
|
|
array([[False, True, False]])
|
2023-09-27 12:00:34 +00:00
|
|
|
>>> gray_to_binary(np.array([[0]]))
|
2021-10-16 14:32:40 +00:00
|
|
|
array([[False]])
|
2023-09-27 12:00:34 +00:00
|
|
|
>>> gray_to_binary(np.array([[26.2409, 4.9315, 1.4729]]))
|
2021-10-16 14:32:40 +00:00
|
|
|
array([[False, False, False]])
|
2023-09-27 12:00:34 +00:00
|
|
|
>>> gray_to_binary(np.array([[26, 255, 14], [5, 147, 20], [1, 200, 0]]))
|
2021-10-16 14:32:40 +00:00
|
|
|
array([[False, True, False],
|
|
|
|
[False, True, False],
|
|
|
|
[False, True, False]])
|
|
|
|
"""
|
2023-03-01 16:23:33 +00:00
|
|
|
return (gray > 127) & (gray <= 255)
|
2021-10-16 14:32:40 +00:00
|
|
|
|
|
|
|
|
2023-09-27 12:00:34 +00:00
|
|
|
def erosion(image: np.ndarray, kernel: np.ndarray) -> np.ndarray:
|
2021-10-16 14:32:40 +00:00
|
|
|
"""
|
|
|
|
Return eroded image
|
2023-09-27 12:00:34 +00:00
|
|
|
|
2021-10-16 14:32:40 +00:00
|
|
|
>>> 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
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
# read original image
|
2023-09-27 12:00:34 +00:00
|
|
|
lena_path = Path(__file__).resolve().parent / "image_data" / "lena.jpg"
|
|
|
|
lena = np.array(Image.open(lena_path))
|
|
|
|
|
|
|
|
# kernel to be applied
|
|
|
|
structuring_element = np.array([[0, 1, 0], [1, 1, 1], [0, 1, 0]])
|
|
|
|
|
2021-10-16 14:32:40 +00:00
|
|
|
# Apply erosion operation to a binary image
|
2023-09-27 12:00:34 +00:00
|
|
|
output = erosion(gray_to_binary(rgb_to_gray(lena)), structuring_element)
|
|
|
|
|
2021-10-16 14:32:40 +00:00
|
|
|
# Save the output image
|
|
|
|
pil_img = Image.fromarray(output).convert("RGB")
|
|
|
|
pil_img.save("result_erosion.png")
|