-
Notifications
You must be signed in to change notification settings - Fork 0
/
dilation.py
58 lines (48 loc) · 1.65 KB
/
dilation.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
52
53
54
55
56
57
58
# Dilation
import numpy as np
import matplotlib.pyplot as plt
from scipy import misc
def dilation(image, structuring_element):
# Find the number of rows and columns of both
# image and the structuring element
(image_row, image_col) = image.shape
(element_row, element_col) = structuring_element.shape
# Assign the original image as the dilated image initially
dilated_image = np.copy(image)
for i in range(image_row - element_row + 1):
for j in range(image_col - element_col + 1):
# Define the overlapping mask
mask = image[i:i+element_row, j:j+element_col]
# Check if there are any overlaps
# If there are overlaps, overlap > 0
# Otherwise overlap will be zero
overlap = np.sum(structuring_element * mask)
if overlap > 0:
dilated_image[i, j] = 255
else:
dilated_image[i, j] = 0
return dilated_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('255j.jpg')
image = threshold(image)
dil = dilation(image, structuring_element)
show_images(image, dil, 'Original Image', 'Dilated Image')