-
Notifications
You must be signed in to change notification settings - Fork 11
/
Image_preperation.py
318 lines (208 loc) · 7.36 KB
/
Image_preperation.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
# coding: utf-8
# In[2]:
import cv2
import numpy as np
import math
from scipy import ndimage, signal
import matplotlib.pyplot as plt
import matplotlib
from skimage.color import rgb2gray
from skimage.filters import gaussian
import scipy
from scipy import ndimage
from skimage.util.dtype import dtype_range
from skimage.util import img_as_ubyte
from skimage.morphology import disk
from skimage.filters import rank
from skimage import exposure
from skimage.filters import roberts, sobel, scharr, prewitt
from skimage import feature, img_as_float
import FileManager as fm
def calc_external_img_active_contour(img):
median = median_filter(img)
contrast = contrast_stretching(median)
ext_img = canny(contrast)
return ext_img
def calc_external_img_active_contour2(img):
img = np.array(img, dtype=np.uint16)
kx = np.array([[-1,0,1],[-2,0,2],[-1,0,1]])
Gx = cv2.filter2D(img,-1,kx)
ky = np.array([[-1,-2,-1],[0,0,0],[1,2,1]])
Gy = cv2.filter2D(img,-1,ky)
G = np.sqrt(Gx**2 + Gy**2)
return G
def sharpening(img):
k = np.array([[0,-1,0],[-1,5,-1],[0,-1,0]])
#image_sharpen = scipy.signal.convolve2d(img, k, 'same')
#ndimage.convolve(img, k, mode='constant', cval=0.0)
image_sharpen = cv2.filter2D(img,-1,k)
return image_sharpen
def sharpening2(img):
k1 = np.array([[1,1,1],[1,1,1],[1,1,1]])/9
smoothed = cv2.filter2D(img,-1,k1)
k2 = np.array([[0,0,0],[0,2,0],[0,0,0]])
dubbel = cv2.filter2D(img,-1,k2)
return 2*img - smoothed
def contrast_stretching(img):
# Contrast stretching
#p2, p98 = np.percentile(img, (0, 20))
return exposure.rescale_intensity(img, in_range=(0.05*255, 0.6*255))
def adaptive_equalization(img):
# Adaptive Equalization
return exposure.equalize_adapthist(img, clip_limit = 0.05, nbins = 50)
def local_equalization(img, size = 50):
# Equalization
selem = disk(size)
return rank.equalize(img, selem=selem)
def median_filter(img, size = 3):
return scipy.signal.medfilt(img, size).astype(np.uint8)
def edge_detection_low(img):
return roberts(img)
# def edge_detection_high(img):
# return sobel(img)
def edge_detection_high(img):
return canny(img)
def canny(img):
return feature.canny(img, sigma=2)
def calc_external_img(img):
img = rgb2gray(img)
sobelx64f = cv2.Sobel(img,cv2.CV_64F,1,0,ksize=5)
abs_sobel64f = np.absolute(sobelx64f)
sobelx = np.uint8(abs_sobel64f)
sobely64f = cv2.Sobel(img,cv2.CV_64F,0,1,ksize=5)
abs_sobel64f = np.absolute(sobely64f)
sobely = np.uint8(abs_sobel64f)
return -(sobelx + sobely)
def calc_external_img2(img):
img = np.array(img, dtype=np.uint16)
kx = np.array([[-1,0,1],[-2,0,2],[-1,0,1]])
Gx = cv2.filter2D(img,-1,kx)
ky = np.array([[-1,-2,-1],[0,0,0],[1,2,1]])
Gy = cv2.filter2D(img,-1,ky)
G = np.sqrt(Gx**2 + Gy**2)
return -G
def pre_processing(img):
img = radiograph_preprocess(img)
return radiograph_preprocess2(img)
def radiograph_preprocess(img):
equ = cv2.equalizeHist(img)
return equ
def radiograph_preprocess2(img):
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))
cl1 = clahe.apply(img)
return cl1
# In[5]:
######### Visualization #########
def show(img, size=7):
fig, ax = plt.subplots(figsize=(size, size))
plt.imshow(img)
plt.show()
def plot_img_and_hist(image, axes, bins=256):
"""Plot an image along with its histogram and cumulative histogram.
"""
image = img_as_float(image)
ax_img, ax_hist = axes
ax_cdf = ax_hist.twinx()
# Display image
ax_img.imshow(image, cmap=plt.cm.gray)
ax_img.set_axis_off()
# Display histogram
ax_hist.hist(image.ravel(), bins=bins, histtype='step', color='black')
ax_hist.ticklabel_format(axis='y', style='scientific', scilimits=(0, 0))
ax_hist.set_xlabel('Pixel intensity')
ax_hist.set_xlim(0, 1)
ax_hist.set_yticks([])
# Display cumulative distribution
img_cdf, bins = exposure.cumulative_distribution(image, bins)
ax_cdf.plot(bins, img_cdf, 'r')
ax_cdf.set_yticks([])
return ax_img, ax_hist, ax_cdf
def show_different_preperations(img):
matplotlib.rcParams['font.size'] = 8
# Load an example image
median = median_filter(img)
local_eq = local_equalization(median)
contrast = contrast_stretching(median)
adapt = adaptive_equalization(median)
# Display results
fig = plt.figure(figsize=(12, 7))
axes = np.zeros((2, 4), dtype=np.object)
axes[0, 0] = fig.add_subplot(2, 4, 1)
for i in range(1, 4):
axes[0, i] = fig.add_subplot(2, 4, 1+i, sharex=axes[0,0], sharey=axes[0,0])
for i in range(0, 4):
axes[1, i] = fig.add_subplot(2, 4, 5+i)
ax_img, ax_hist, ax_cdf = plot_img_and_hist(local_eq, axes[:, 3])
ax_img.set_title('local_equalization')
y_min, y_max = ax_hist.get_ylim()
ax_hist.set_ylabel('Number of pixels')
ax_hist.set_yticks(np.linspace(0, y_max, 5))
ax_img, ax_hist, ax_cdf = plot_img_and_hist(contrast, axes[:, 1])
ax_img.set_title('contrast_stretching')
ax_img, ax_hist, ax_cdf = plot_img_and_hist(adapt, axes[:, 2])
ax_img.set_title('adaptive_equalization')
ax_img, ax_hist, ax_cdf = plot_img_and_hist(img, axes[:, 0])
ax_img.set_title('median_filter')
ax_cdf.set_ylabel('Fraction of total intensity')
ax_cdf.set_yticks(np.linspace(0, 1, 5))
# prevent overlap of y-axis labels
fig.tight_layout()
plt.show()
def show_diff_edge_detectors(img):
matplotlib.rcParams['font.size'] = 8
edge_roberts = roberts(img)
edge_sobel = sobel(img)
edge_scharr = scharr(img)
edge_prewitt = prewitt(img)
canny = feature.canny(img, sigma=0.5)
fig, axes = plt.subplots(nrows=2, ncols=3, sharex=True, sharey=True,
figsize=(10, 7))
ax = axes.ravel()
ax[0].imshow(img)
ax[0].set_title('Input image')
ax[1].imshow(edge_prewitt)
ax[1].set_title('Prewitt Edge Detection')
ax[2].imshow(edge_scharr)
ax[2].set_title('Scharr Edge Detection')
ax[3].imshow(edge_sobel)
ax[3].set_title('Sobel Edge Detection')
ax[4].imshow(edge_roberts)
ax[4].set_title('Roberts Edge Detection')
ax[5].imshow(canny)
ax[5].set_title('Canny Edge Detection')
for a in ax:
a.axis('off')
plt.tight_layout()
plt.show()
def show_diff_cannies(img):
matplotlib.rcParams['font.size'] = 8
canny1 = feature.canny(img, sigma=0.01)
canny2 = feature.canny(img, sigma=0.05)
canny3 = feature.canny(img, sigma=0.1)
canny4 = roberts(img)
canny5 = sobel(img)
fig, axes = plt.subplots(nrows=2, ncols=3, sharex=True, sharey=True,
figsize=(10, 7))
ax = axes.ravel()
ax[0].imshow(img)
ax[0].set_title('Input image')
ax[1].imshow(canny1)
ax[2].imshow(canny2)
ax[3].imshow(canny3)
ax[4].imshow(canny4)
ax[5].imshow(canny5)
for a in ax:
a.axis('off')
plt.tight_layout()
plt.show()
# In[6]:
if __name__ == "__main__":
img = fm.load_radiograph()
piece = img[700:1300,1200:1800]
show(piece, 7)
median = median_filter(piece)
contrast = contrast_stretching(median)
show_different_preperations(piece)
show_diff_edge_detectors(contrast)
show_diff_cannies(contrast)
# In[39]: