Skip to content

Commit 5c908c7

Browse files
authored
Added morphological operations, fixes: TheAlgorithms#5197
1 parent 77b243e commit 5c908c7

File tree

2 files changed

+117
-0
lines changed

2 files changed

+117
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import numpy as np
2+
from PIL import Image
3+
4+
5+
def rgb2gray(rgb):
6+
"""
7+
Return gray image from rgb image
8+
"""
9+
r, g, b = rgb[:, :, 0], rgb[:, :, 1], rgb[:, :, 2]
10+
gray = 0.2989 * r + 0.5870 * g + 0.1140 * b
11+
return gray
12+
13+
14+
def gray2binary(gray):
15+
"""
16+
Return binary image from gray image
17+
"""
18+
return (127 < gray) & (gray <= 255)
19+
20+
21+
def dilation(image, kernel):
22+
"""
23+
Return dilated image
24+
"""
25+
output = np.zeros_like(image)
26+
image_padded = np.zeros(
27+
(image.shape[0] + kernel.shape[0] - 1, image.shape[1] + kernel.shape[1] - 1)
28+
)
29+
30+
# Copy image to padded image
31+
image_padded[kernel.shape[0] - 2 : -1 :, kernel.shape[1] - 2 : -1 :] = image
32+
33+
# Iterate over image & apply kernel
34+
for x in range(image.shape[1]):
35+
for y in range(image.shape[0]):
36+
summation = (
37+
kernel * image_padded[y : y + kernel.shape[0], x : x + kernel.shape[1]]
38+
).sum()
39+
if summation > 0:
40+
output[y, x] = 1
41+
else:
42+
output[y, x] = 0
43+
return output
44+
45+
46+
# kernel to be applied
47+
structuring_element = np.array([[0, 1, 0], [1, 1, 1], [0, 1, 0]])
48+
49+
50+
if __name__ == "__main__":
51+
# read original image
52+
image = np.array(Image.open(r"..\image_data\lena.jpg"))
53+
# convert it into binary image
54+
binary = gray2binary(rgb2gray(image))
55+
# Apply dilation operation
56+
output = dilation(binary, structuring_element)
57+
# Save the output image
58+
pil_img = Image.fromarray(output).convert("RGB")
59+
pil_img.save("result_dilation.png")
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import numpy as np
2+
from PIL import Image
3+
4+
5+
def rgb2gray(rgb):
6+
"""
7+
Return gray image from rgb image
8+
"""
9+
r, g, b = rgb[:, :, 0], rgb[:, :, 1], rgb[:, :, 2]
10+
gray = 0.2989 * r + 0.5870 * g + 0.1140 * b
11+
return gray
12+
13+
14+
def gray2binary(gray):
15+
"""
16+
Return binary image from gray image
17+
"""
18+
return (127 < gray) & (gray <= 255)
19+
20+
21+
def erosion(image, kernel):
22+
"""
23+
Return eroded image
24+
"""
25+
output = np.zeros_like(image)
26+
image_padded = np.zeros(
27+
(image.shape[0] + kernel.shape[0] - 1, image.shape[1] + kernel.shape[1] - 1)
28+
)
29+
30+
# Copy image to padded image
31+
image_padded[kernel.shape[0] - 2 : -1 :, kernel.shape[1] - 2 : -1 :] = image
32+
33+
# Iterate over image & apply kernel
34+
for x in range(image.shape[1]):
35+
for y in range(image.shape[0]):
36+
summation = (
37+
kernel * image_padded[y : y + kernel.shape[0], x : x + kernel.shape[1]]
38+
).sum()
39+
if summation == 5:
40+
output[y, x] = 1
41+
else:
42+
output[y, x] = 0
43+
return output
44+
45+
46+
# kernel to be applied
47+
structuring_element = np.array([[0, 1, 0], [1, 1, 1], [0, 1, 0]])
48+
49+
if __name__ == "__main__":
50+
# read original image
51+
image = np.array(Image.open(r"..\image_data\lena.jpg"))
52+
# convert it into binary image
53+
binary = gray2binary(rgb2gray(image))
54+
# Apply erosion operation
55+
output = erosion(binary, structuring_element)
56+
# Save the output image
57+
pil_img = Image.fromarray(output).convert("RGB")
58+
pil_img.save("result_erosion.png")

0 commit comments

Comments
 (0)