-
Notifications
You must be signed in to change notification settings - Fork 0
/
morphological_operators.py
62 lines (44 loc) · 1.96 KB
/
morphological_operators.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
59
60
61
62
import numpy as np
from skimage import util
def erosion(image: np.ndarray, se=np.ones((3, 3))):
n_rows = se.shape[0] // 2
n_cols = se.shape[1] // 2
padded_image = util.pad(image, ((n_rows, n_rows), (n_cols, n_cols)), 'constant', constant_values=0)
for i in range(n_rows, image.shape[0]+n_rows):
for j in range(n_cols, image.shape[1]+n_cols):
# for a in range(-n_rows, n_rows+1):
# for b in range(-n_cols, n_cols+1):
# aux_pix = padded_image[i+a, j+b]
# aux_kernel = se[a+n_rows, b+n_cols]
# conv_pixel += aux_pix * aux_kernel
se_img = padded_image[i - n_rows: i + n_rows + 1, j - n_cols: j + n_cols + 1]
# print(se_img.shape)
mask = (se_img * se) > 0
# print(se_img)
mini = np.min(se_img[mask])
image[i - n_rows, j - n_cols] = mini
return image
def dilation(image: np.ndarray, se=np.ones((3, 3))):
se = np.fliplr(se)
n_rows = se.shape[0] // 2
n_cols = se.shape[1] // 2
padded_image = util.pad(image, ((n_rows, n_rows), (n_cols, n_cols)), 'constant', constant_values=0)
for i in range(n_rows, image.shape[0] + n_rows):
for j in range(n_cols, image.shape[1] + n_cols):
# for a in range(-n_rows, n_rows+1):
# for b in range(-n_cols, n_cols+1):
# aux_pix = padded_image[i+a, j+b]
# aux_kernel = se[a+n_rows, b+n_cols]
# conv_pixel += aux_pix * aux_kernel
se_img = padded_image[i - n_rows: i + n_rows + 1, j - n_cols : j + n_cols +1]
# print(se_img.shape)
mask = (se_img * se) > 0
mini = np.max(se_img[mask])
image[i - n_rows, j - n_cols] = mini
return image
def morphological_gradient(image: np.ndarray):
img1 = image.copy()
img2 = image.copy()
img1 = dilation(img1)
img2 = erosion(img2)
return img1 - img2