-
Notifications
You must be signed in to change notification settings - Fork 0
/
morphology.py
327 lines (258 loc) · 11.3 KB
/
morphology.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
import numpy as np
def morph(img=None, oper=None, iterations=1, strel=np.ones((3, 3)), bin=0):
# This function accepts accepts a binary shape (img), morphological operation, structuring element
# and number of iterations. It performs the specified operation of img by the structuring element
# for the required number of iterations and returns the result.
if bin:
img = binarize(img)
if oper is None:
print('Choose specific morphological operator: erosion, dilation, closing, opening')
return
img = find_range(img)
if oper == 'er':
func = erosion
elif oper == 'di':
func = dilation
elif oper == 'cl':
func = closing
elif oper == 'op':
func = opening
morphed_img = func(img, strel)
if iterations > 1:
for i in range(iterations - 1):
morphed_img = func(morphed_img, strel)
i += 1
return morphed_img
def erosion(img, strel=np.ones((3, 3))):
# The erosion function accepts a binary shape (img) and a structuring element. If no structuring element
# is passed to the function, a 3-by-3 array of ones is used as a default.
# The function returns the erosion of img by the structuring element.
# img = binarize(img)
img = find_range(img)
img /= 255
dim = img.shape
strel = np.asarray(strel, dtype=np.uint8)
orig = ((strel.shape[0]-1) // 2, (strel.shape[1] -1) // 2)
x, y = strel.shape[0], strel.shape[1]
a = np.zeros(dim)
for i in range(dim[0]):
for j in range(dim[1]):
# Step I - Find the intersection of structuring element and current x-by-y subset of img.
# If there is no intersection, continue:
inters = img[fit(i - orig[0]):i - orig[0] + x, fit(j - orig[1]):j - orig[1] + y]
if inters.shape[0] == 0 or inters.shape[1] == 0:
continue
# Step II - Find points (x0, y0), (x1, y1) such that the shape of
# structuring element [x0:x1, y0:y1] will be equal to the shape of intersection:
if orig[0] - i > 0:
x0 = orig[0] - i
else:
x0 = 0
if i + (x - orig[0]) > dim[0]:
x1 = (dim[0] + orig[0]) - (i + 1)
else:
x1 = x - 1
if orig[1] - j > 0:
y0 = orig[1] - j
else:
y0 = 0
if j + (y - orig[1]) > dim[1]:
y1 = (dim[1] + orig[1]) - (j + 1)
else:
y1 = y - 1
# Step III - Compute match:
match = np.logical_and(inters, strel[x0:x1 + 1, y0:y1 + 1])
if np.array_equal(match, strel[x0:x1 + 1, y0:y1 + 1]):
a[i, j] = 1
return np.asarray(a * 255, dtype=np.uint8)
def dilation(img, strel=np.ones((3, 3))):
# The erosion function accepts a binary shape (img) and a structuring element. If no structuring element
# is passed to the function, a 3-by-3 array of ones is used as a default.
# The function returns the dilation of the binary shape by the structuring element.
# img = binarize(img)
img = find_range(img)
img = img // 255
dim = img.shape
strel=np.asarray(strel, dtype=np.uint8)
orig = ((strel.shape[0] - 1) // 2, (strel.shape[1] - 1) // 2)
x, y = strel.shape[0], strel.shape[1]
a = np.zeros(dim)
for i in range(dim[0]):
for j in range(dim[1]):
# Step I - Find the intersection of structuring element and current x-by-y subset of img.
# If there is no intersection, continue:
inters = img[fit(i - orig[0]):i - orig[0] + x, fit(j - orig[1]):j - orig[1] + y]
if inters.shape[0] == 0 or inters.shape[1] == 0:
continue
# Step II - find points (x0, y0), (x1, y1) such that the shape of
# structuring element [x0:x1, y0:y1] will be equal to the shape of intersection:
if orig[0] - i > 0:
x0 = orig[0] - i
else:
x0 = 0
if i + (x - orig[0]) > dim[0]:
x1 = (dim[0] + orig[0]) - (i + 1)
else:
x1 = x - 1
if orig[1] - j > 0:
y0 = orig[1] - j
else:
y0 = 0
if j + (y - orig[1]) > dim[1]:
y1 = (dim[1] + orig[1]) - (j + 1)
else:
y1 = y - 1
# Step III - Compute match:
match = np.logical_and(inters, strel[x0:x1 + 1, y0:y1 + 1])
if match.any():
a[i, j] = 1
return np.asarray(a * 255, dtype=np.uint8)
def opening(img, strel=np.ones((3, 3))):
# The erosion function accepts a binary shape (img) and a structuring element. If no structuring element
# is passed to the function, a 3-by-3 array of ones is used as a default.
# The function returns the opening of img by the structuring element.
return dilation(erosion(img, strel), strel)
def closing(img, strel=np.ones((3, 3))):
# The erosion function accepts a binary shape (img) and a structuring element. If no structuring element
# is passed to the function, a 3-by-3 array of ones is used as a default.
# The function returns the closing of img by the structuring element.
return erosion(dilation(img, strel), strel)
def reverse(img):
# Auxiliary function to convert black-over-white images to white-over-black images.
rev = segment(img)
rev[img == 0] = 255
rev[img == 255] = 0
return np.asarray(rev, dtype=np.uint8)
def gray_conversion(img):
# Auxiliary function to convert color images to grayscale.
if len(img.shape) == 2:
return img
else:
gray_img = np.asarray(img, dtype=np.float64)
gray_img = 0.07 * img[:, :, 2] + 0.72 * img[:, :, 1] + 0.21 * img[:, :, 0]
gray_img = np.reshape(gray_img, (gray_img.shape[0], gray_img.shape[1]))
return gray_img.astype(np.float64)
def segment(img, th=None):
# This function performs a basic segmentation of a grayscale image according to given threshold.
# Mean value of the image is used as default.
if th is None:
th = int(np.round(np.mean(img, axis=0).mean()))
seg_img = img.copy()
seg_img[seg_img < th] = 0
seg_img[seg_img >= th] = 255
return np.asarray(seg_img, dtype=np.uint8)
def hist_thresh(img):
# This function performs segmentation of a grayscale image by computing the optimal threshold.
mean = int(np.round(np.mean(img, axis=0).mean()))
while True:
fg_mean = np.mean(img[img > int(np.round(mean))])
bg_mean = np.mean(img[img <= int(np.round(mean))])
new_mean = (bg_mean + fg_mean) / 2
if new_mean == mean:
return segment(img, new_mean)
mean = new_mean
def binarize(img, mode='hist', th=None):
# Auxiliary function to convert color or grayscale images to black and white (binary).
bin_img = img.copy()
if len(img.shape) == 3:
bin_img = gray_conversion(np.copy(img))
if mode == 'seg':
return segment(bin_img, th)
if mode == 'hist':
return hist_thresh(bin_img)
def fit(index):
# Auxiliary function to compute intersection of a structuring element and a binary image.
if index < 0:
return 0
else:
return index
def find_range(img):
# Auxiliary function to turn binary images into [0, 255] format over [0, 1].
if img[img == 1].any():
return np.asarray(img * 255, dtype=np.float64)
else:
return np.asarray(img, dtype=np.float64)
def strel(shape='rect', dim=(3, 3), rad=3):
if shape == 'rect':
return np.ones(dim, np.uint8)
if shape == 'cross':
strel = np.zeros(dim, np.uint)
strel[dim[0] // 2, :] = 1
strel[:, dim[1] // 2] = 1
return strel
if shape == 'diag':
return np.eye(dim[0], dim[1], dtype=np.uint8)
if shape == 'anti':
strel = np.eye(dim[0], dim[1], dtype=np.uint8)
return np.fliplr(strel)
if shape == 'ellipse':
rad = abs(np.round(rad))
strel = np.zeros((2*rad - 1, 2*rad - 1), np.uint8)
origin = (2*rad - 1) // 2
for x in range(2*rad - 1):
for y in range(2*rad - 1):
if (x - origin)**2 + (y - origin)**2 < rad**2 - 1:
strel[x, y] = 1
return strel
if shape == 'disk':
rad = abs(np.round(rad))
strel = np.zeros((2*rad - 1, 2*rad - 1), np.uint8)
origin = (2*rad - 1) // 2
for x in range(2*rad - 1):
for y in range(2*rad - 1):
if abs(x - origin) + abs(y - origin) < rad:
strel[x, y] = 1
return strel
def hist_eq(img):
hist = [np.sum(img == i) for i in range(256)]
hist = np.array(hist, dtype=np.float64)
PDF = hist/img.size
CDF = np.cumsum(PDF)
CDF = np.asarray(CDF, dtype=np.float64)
mod_img = np.zeros(img.shape, dtype=np.uint8)
for i in range(256):
mod_img[img == i] = np.round(CDF[i] * 255)
return mod_img
def erosion_ver2(img, strel=np.ones((3, 3))):
# The erosion function accepts a binary shape (img) and a structuring element. If no structuring element
# is passed to the function, a 3-by-3 array of ones is used as a default.
# The function returns the erosion of img by the structuring element.
img = binarize(img, 'seg')
img = find_range(img)
img /= 255
dim = img.shape
strel=np.asarray(strel, dtype=np.uint8)
orig = ((strel.shape[0])// 2, (strel.shape[1])// 2)
x, y = strel.shape[0], strel.shape[1]
a = np.zeros(dim)
for i in range(orig[0], dim[0] - orig[0], 1):
for j in range(orig[1], dim[1]-orig[1], 1):
intersection = img[i - orig[0]:i - orig[0] + x, j - orig[1]:j - orig[0] + y]
intersection_sum = intersection + strel
overlap = intersection[intersection_sum == 2]
u = np.count_nonzero(overlap)
v = np.count_nonzero(strel)
if u == v:
a[i, j] = 1
return np.asarray(a*255 , dtype=np.uint8)
def dilation_ver2(img, strel=np.ones((3, 3))):
# The erosion function accepts a binary shape (img) and a structuring element. If no structuring element
# is passed to the function, a 3-by-3 array of ones is used as a default.
# The function returns the erosion of img by the structuring element.
# img = binarize(img, 'seg')
img = find_range(img)
img /= 255
dim = img.shape
strel = np.asarray(strel, dtype=np.uint8)
orig = ((strel.shape[0])// 2, (strel.shape[1])// 2)
x, y = strel.shape[0], strel.shape[1]
a = np.zeros(dim)
for i in range(orig[0], dim[0] - orig[0], 1):
for j in range(orig[1], dim[1] - orig[1], 1):
intersection = img[i - orig[0]:i - orig[0] + x, j - orig[1]:j - orig[0] + y]
intersection_sum = intersection + strel
overlap = intersection[intersection_sum == 2]
u = np.count_nonzero(overlap)
if u >= 1:
a[i, j] = 1
return np.asarray(a * 255, dtype=np.uint8)