-
Notifications
You must be signed in to change notification settings - Fork 0
/
erosion.py
51 lines (41 loc) · 1.35 KB
/
erosion.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# Erosion
import numpy as np
import matplotlib.pyplot as plt
from scipy import misc
def erosion(image, structuring_element):
(image_row, image_col) = image.shape
(element_row, element_col) = structuring_element.shape
eroded_image = np.copy(image)
for i in range(image_row - element_row + 1):
for j in range(image_col - element_col + 1):
mask = image[i:i+element_row, j:j+element_col]
overlap = structuring_element * mask
if np.array_equal(overlap, structuring_element * 255):
eroded_image[i, j] = 255
else:
eroded_image[i, j] = 0
return eroded_image
def threshold(image):
new_image = np.zeros(image.shape)
(row, col) = image.shape
for i in range(row):
for j in range(col):
if image[i, j] > 128:
new_image[i, j] = 255
return new_image
def show_images(image1, image2, title1, title2):
f, a = plt.subplots(1, 2)
a[0].imshow(image1, cmap='gray')
a[0].set_title(title1)
a[1].imshow(image2, cmap='gray')
a[1].set_title(title2)
plt.show()
structuring_element = np.array([
[1, 1, 1],
[1, 1, 1],
[1, 1, 1]
])
image = misc.imread('jjj.jpg')
image = threshold(image)
dil = erosion(image, structuring_element)
show_images(image, dil, 'Original Image', 'Image after Erosion')