-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathpreprocessing.py
362 lines (276 loc) · 11.8 KB
/
preprocessing.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
import cv2
import matplotlib.pyplot as plt
import numpy as np
from skimage.filters import (threshold_otsu, threshold_niblack, threshold_sauvola, rank)
from skimage.morphology import disk
def draw_histogram(image):
plt.hist(image.ravel(), 256, [0, 256])
plt.show()
def match_histogram(source, template):
"""
Adjust the pixel values of a gray-scale image such that its histogram
matches that of a target image
Arguments:
-----------
source: np.ndarray
Image to transform; the histogram is computed over the flattened
array
template: np.ndarray
Template image; can have different dimensions to source
Returns:
-----------
matched: np.ndarray
The transformed output image
"""
oldshape = source.shape
source = source.ravel()
template = template.ravel()
# get the set of unique pixel values and their corresponding indices and
# counts
s_values, bin_idx, s_counts = np.unique(source, return_inverse=True, return_counts=True)
t_values, t_counts = np.unique(template, return_counts=True)
# take the cumsum of the counts and normalize by the number of pixels to
# get the empirical cumulative distribution functions for the source and
# template images (maps pixel value --> quantile)
s_quantiles = np.cumsum(s_counts).astype(np.float64)
s_quantiles /= s_quantiles[-1]
t_quantiles = np.cumsum(t_counts).astype(np.float64)
t_quantiles /= t_quantiles[-1]
# interpolate linearly to find the pixel values in the template image
# that correspond most closely to the quantiles in the source image
interpolated_t_values = np.interp(s_quantiles, t_quantiles, t_values)
hist_matched_img = interpolated_t_values[bin_idx].reshape(oldshape)
result = np.array(hist_matched_img, dtype='uint8')
return result
def equalize_histogram(image):
result = cv2.equalizeHist(image)
return result
def AHE(image, radius=150):
result = rank.equalize(image=image, selem=disk(radius))
return result
def CLAHE(image, clip_limit=2.0, grid_size=8):
clahe = cv2.createCLAHE(clipLimit=clip_limit, tileGridSize=(grid_size, grid_size))
adaptive_equalized = clahe.apply(image)
return adaptive_equalized
def global_threshold(image, threshold, show_result=False, return_result=False):
ret, thresh1 = cv2.threshold(image, threshold, 255, cv2.THRESH_BINARY)
ret, thresh2 = cv2.threshold(image, threshold, 255, cv2.THRESH_BINARY_INV)
ret, thresh3 = cv2.threshold(image, threshold, 255, cv2.THRESH_TRUNC)
ret, thresh4 = cv2.threshold(image, threshold, 255, cv2.THRESH_TOZERO)
ret, thresh5 = cv2.threshold(image, threshold, 255, cv2.THRESH_TOZERO_INV)
if show_result:
titles = ['Original Image', 'BINARY', 'BINARY_INV', 'TRUNC', 'TOZERO', 'TOZERO_INV']
images = [image, thresh1, thresh2, thresh3, thresh4, thresh5]
for i in range(6):
plt.subplot(2, 3, i + 1)
plt.imshow(images[i], 'gray')
plt.title(titles[i])
plt.xticks([])
plt.yticks([])
plt.show()
if return_result:
return thresh1
def adaptive_threshold(image, blur=False, show_result=False, return_result=False):
if blur:
image = cv2.bilateralFilter(src=image, d=17, sigmaColor=35, sigmaSpace=35)
ret, th = cv2.threshold(image, 127, 255, cv2.THRESH_BINARY)
th_mean = cv2.adaptiveThreshold(image, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 35, 5)
th_gaussian = cv2.adaptiveThreshold(image, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 35, 5)
if show_result:
titles = ['Original Image', 'Global Threshold', 'Adaptive Mean Threshold', 'Adaptive Gaussian Threshold']
images = [image, th, th_mean, th_gaussian]
for i in range(4):
plt.subplot(2, 2, i + 1)
plt.imshow(images[i], 'gray')
plt.title(titles[i])
plt.xticks([])
plt.yticks([])
plt.show()
if return_result:
return th_mean, th_gaussian
def otsu(image, blur=False, show_result=False, return_result=False):
if blur:
image = cv2.GaussianBlur(src=image, ksize=(5, 5), sigmaX=0)
ret, th = cv2.threshold(image, 127, 255, cv2.THRESH_BINARY)
ret_otsu, th_otsu = cv2.threshold(image, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
if show_result:
titles = ['Original Image', 'Global Threshold', 'Otsu Binarization']
images = [image, th, th_otsu]
for i in range(3):
plt.subplot(3, 1, i + 1)
plt.imshow(images[i], cmap='gray')
plt.title(titles[i])
plt.xticks([])
plt.yticks([])
plt.show()
print("Otsu's threshold is:", ret_otsu)
if return_result:
return th_otsu
def niblack(image, window_size, k, show_result=False, return_result=False):
binary_global = image > threshold_otsu(image)
thresh_niblack = threshold_niblack(image, window_size=window_size, k=k)
binary_niblack = image > thresh_niblack
if show_result:
plt.subplot(3, 1, 1), plt.imshow(image, cmap='gray'), plt.title('Original'), plt.axis('off')
plt.subplot(3, 1, 2), plt.imshow(binary_global, cmap='gray'), plt.title('Global Threshold'), plt.axis('off')
plt.subplot(3, 1, 3), plt.imshow(binary_niblack, cmap='gray'), plt.title('Niblack Threshold'), plt.axis('off')
plt.show()
if return_result:
return binary_niblack
def sauvola(image, window_size, show_result=False, return_result=False):
binary_global = image > threshold_otsu(image)
thresh_sauvola = threshold_sauvola(image, window_size=window_size)
binary_sauvola = image > thresh_sauvola
if show_result:
plt.subplot(3, 1, 1), plt.imshow(image, cmap='gray'), plt.title('Original'), plt.axis('off')
plt.subplot(3, 1, 2), plt.imshow(binary_global, cmap='gray'), plt.title('Global Threshold'), plt.axis('off')
plt.subplot(3, 1, 3), plt.imshow(binary_sauvola, cmap='gray'), plt.title('Sauvola Threshold'), plt.axis('off')
plt.show()
binary_sauvola = np.array(binary_sauvola, dtype='uint8')
binary_sauvola = np.where(binary_sauvola == 1, np.uint8(255), np.uint8(0))
if return_result:
return binary_sauvola
def canny(image, blur=False, show_result=False, return_result=False):
if blur:
image = cv2.GaussianBlur(src=image, ksize=(5, 5), sigmaX=0)
edges = cv2.Canny(image, 140, 200)
if show_result:
plt.subplot(211), plt.imshow(image, cmap='gray')
plt.xticks([]), plt.yticks([])
plt.subplot(212), plt.imshow(edges, cmap='gray')
plt.xticks([]), plt.yticks([])
plt.show()
if return_result:
return edges
def laplacian(image, show_result=False, return_result=False):
laplacian = cv2.Laplacian(image, cv2.CV_64F)
if show_result:
plt.subplot(211), plt.imshow(image, cmap='gray')
plt.xticks([]), plt.yticks([])
plt.subplot(212), plt.imshow(laplacian, cmap='gray')
plt.xticks([]), plt.yticks([])
plt.show()
if return_result:
return laplacian
def erosion(image, kernel_size=8, iterations=2, show_result=False, return_result=False):
kernel = np.ones(shape=(kernel_size, kernel_size), dtype=np.uint8)
eroded = cv2.erode(image, kernel, iterations=iterations)
if show_result:
plt.figure(figsize=(10, 10))
plt.subplot(211), plt.imshow(image, cmap='gray')
plt.xticks([]), plt.yticks([])
plt.subplot(212), plt.imshow(eroded, cmap='gray')
plt.xticks([]), plt.yticks([])
plt.show()
if return_result:
return eroded
def dilation(image, kernel_size=8, iterations=2, show_result=False, return_result=False):
kernel = np.ones(shape=(kernel_size, kernel_size), dtype=np.uint8)
dilated = cv2.dilate(image, kernel, iterations=iterations)
if show_result:
plt.figure(figsize=(10, 10))
plt.subplot(211), plt.imshow(image, cmap='gray')
plt.xticks([]), plt.yticks([])
plt.subplot(212), plt.imshow(dilated, cmap='gray')
plt.xticks([]), plt.yticks([])
plt.show()
if return_result:
return dilated
def quadtree_decomp(image, min_size, min_std, show_result=False, return_result=False):
def split_image(image):
h, w = image.shape[0], image.shape[1]
off1X = 0
off1Y = 0
off2X = 0
off2Y = 0
if w >= h: # split X
off1X = 0
off2X = int(w / 2)
img1 = image[0:h, 0:off2X]
img2 = image[0:h, off2X:w]
else: # split Y
off1Y = 0
off2Y = int(h / 2)
img1 = image[0:off2Y, 0:w]
img2 = image[off2Y:h, 0:w]
return off1X, off1Y, img1, off2X, off2Y, img2
def extract_roi(image, min_std, min_size, offX, offY, roi_list):
h, w = image.shape[0], image.shape[1]
m, s = cv2.meanStdDev(image)
if s >= min_std and max(h, w) > min_size:
oX1, oY1, im1, oX2, oY2, im2 = split_image(image)
extract_roi(im1, min_std, min_size, offX + oX1, offY + oY1, roi_list)
extract_roi(im2, min_std, min_size, offX + oX2, offY + oY2, roi_list)
else:
roi_list.append([offX, offY, w, h, m, s])
return roi_list
h, w = image.shape[0], image.shape[1]
input_image = image
# if not isinstance(input_image, type(None)):
# if input_image.ndim > 1:
# input_image = cv2.cvtColor(input_image, cv2.COLOR_BGR2GRAY)
# else:
# pass
# else:
# print('Error on input image: ', input_image)
# exit()
roi_list = extract_roi(image=input_image, min_std=min_std, min_size=min_size, offX=0, offY=0, roi_list=list())
output_image = input_image.copy()
for roi in roi_list:
for element in range(0, len(roi)):
roi[element] = int(roi[element])
color = 255 # white color
if roi[5] < min_std:
color = 0 # black color
cv2.rectangle(output_image, (roi[0], roi[1]), (roi[0] + roi[2], roi[1] + roi[3]), color, 1)
if show_result:
if w > h:
plt.subplot(211)
else:
plt.subplot(121)
plt.imshow(image, cmap='gray')
plt.xticks([]), plt.yticks([])
if w > h:
plt.subplot(212)
else:
plt.subplot(122)
plt.imshow(output_image, cmap='gray')
plt.xticks([]), plt.yticks([])
plt.show()
if return_result:
return output_image
def imfill(image, threshold, window_size, show_result=False, return_result=False):
sa = sauvola(image=image, window_size=window_size, show_result=False, return_result=True)
sa_inv = np.invert(sa)
sa_inv = np.array(sa_inv, dtype='uint8')
retval, labels = cv2.connectedComponents(sa_inv)
hist = np.histogram(labels, retval)[0]
noise = list()
hist_copy = np.copy(hist)
hist_copy = sorted(hist_copy, reverse=True)
th = hist_copy[threshold]
for ii in range(len(hist)):
if hist[ii] > th:
noise.append(ii)
gaps = np.ones([len(sa_inv), len(sa_inv[0])], np.bool)
noise = noise[1:]
for ii in range(len(sa_inv)):
# print("---------", ii)
for num in noise:
indexes = [i for i, j in enumerate(labels[ii]) if j == num]
for jj in indexes:
gaps[ii, jj] = False
# print(jj)
if show_result:
plt.subplot(3, 1, 1), plt.imshow(X=image, cmap='gray'), plt.axis('off')
plt.subplot(3, 1, 2), plt.imshow(X=sa, cmap='gray'), plt.axis('off')
plt.subplot(3, 1, 3), plt.imshow(X=gaps, cmap='gray'), plt.axis('off')
plt.show()
if return_result:
return gaps
def eliminate_white_pixels(image):
image = np.where(image == 255, np.uint8(254), image)
return image
def eliminate_black_pixels(image):
image = np.where(image == 0, np.uint8(1), image)
return image