Skip to content

Commit d2146b8

Browse files
authored
made suggested changes in erosion
1 parent eb0b4f5 commit d2146b8

File tree

1 file changed

+4
-10
lines changed

1 file changed

+4
-10
lines changed

digital_image_processing/morphological_operations/erosion_operation.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ def rgb2gray(rgb: np.array) -> np.array:
1515
array([[159.0524, 90.0635, 117.6989]])
1616
"""
1717
r, g, b = rgb[:, :, 0], rgb[:, :, 1], rgb[:, :, 2]
18-
gray = 0.2989 * r + 0.5870 * g + 0.1140 * b
19-
return gray
18+
return 0.2989 * r + 0.5870 * g + 0.1140 * b
2019

2120

2221
def gray2binary(gray: np.array) -> np.array:
@@ -58,10 +57,7 @@ def erosion(image: np.array, kernel: np.array) -> np.array:
5857
summation = (
5958
kernel * image_padded[y : y + kernel.shape[0], x : x + kernel.shape[1]]
6059
).sum()
61-
if summation == 5:
62-
output[y, x] = 1
63-
else:
64-
output[y, x] = 0
60+
output[y, x] = int(summation == 5)
6561
return output
6662

6763

@@ -71,10 +67,8 @@ def erosion(image: np.array, kernel: np.array) -> np.array:
7167
if __name__ == "__main__":
7268
# read original image
7369
image = np.array(Image.open(r"..\image_data\lena.jpg"))
74-
# convert it into binary image
75-
binary = gray2binary(rgb2gray(image))
76-
# Apply erosion operation
77-
output = erosion(binary, structuring_element)
70+
# Apply erosion operation to a binary image
71+
output = erosion(gray2binary(rgb2gray(image)), structuring_element)
7872
# Save the output image
7973
pil_img = Image.fromarray(output).convert("RGB")
8074
pil_img.save("result_erosion.png")

0 commit comments

Comments
 (0)