-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
351 lines (275 loc) · 12.4 KB
/
main.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
import cv2
import numpy as np
import matplotlib.pyplot as plt
from scipy.ndimage.filters import correlate
from scipy.signal import correlate2d
def normalize_matrix(image: np.ndarray, upperbound: int = 255, lowerbound: int = 0, gray_value: int = 0) -> np.ndarray:
with np.nditer(image, op_flags=['readwrite']) as it:
for i in it:
if i > upperbound:
i[...] = 255
elif i < lowerbound:
i[...] = 0
elif gray_value != 0:
i[...] = gray_value
return image
def laplace_filter(image: np.ndarray) -> np.ndarray:
"""Implementation of Laplacian filter
Args:
image(np.ndarray): image as numpy array
Returns:
np.ndarray : image after filtering
"""
laplace_kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]])
after_laplace_filter = correlate2d(image, laplace_kernel, mode='same', boundary='fill')
return after_laplace_filter
def image_sharpening(image: np.ndarray) -> np.ndarray:
"""Simple function to sharpen a given image using a laplace 3x3 kernel and normalization function
Args:
image(np.ndarray): an image as np.ndarray read by cv2 or PILLOW
Returns:
np.ndarray: sharpened image using laplace method
"""
sharpened_image = normalize_matrix(laplace_filter(image))
# We have to do this transformation in order to use cv2.imshow() if you use matplotlib there is no need
# for this line
# sharpened_image = np.array(sharpened_image, dtype=np.uint8)
return sharpened_image
def sobel_operator(image: np.ndarray) -> np.ndarray:
"""Simple implementation of Sobel edge detection
Args:
image(np.ndarray): our input image as numpy array
Returns:
np.ndarray: Image after Sobel edge detection
"""
correlated_image = list()
sobel_kernels = [np.array([[1, 0, -1], [2, 0, -2], [1, 0, -1]]), np.array([[1, 2, 1], [0, 0, 0], [-1, -2, -1]])]
# Calculating Dx and Dy
for kernel in sobel_kernels:
# Using filters.correlate instead of correlate2d to avoid 0 paddings and use automatic normalization
correlated_image.append(correlate(image, kernel))
correlated_image = list(map(np.abs, correlated_image))
final_image = normalize_matrix(np.add(correlated_image[0], correlated_image[1]))
return final_image
def pixel_theta(image: np.ndarray) -> np.ndarray:
"""Calculation of theta in the Sobel filter formula , using arctan()
Args:
image(np.ndarray): an image as input
Returns:
np.ndarray : a numpy array filled with thetas for each pixel
"""
correlated_image = list()
sobel_kernel = [np.array([[1, 0, -1], [2, 0, -2], [1, 0, -1]]), np.array([[1, 2, 1], [0, 0, 0], [-1, -2, -1]])]
for kernel in sobel_kernel:
correlated_image.append(correlate2d(image, kernel, mode='same', boundary='fill'))
# in order to avoid runtime warning
np.seterr(divide='ignore', invalid='ignore')
thetas_for_each_pixel = np.array(list(map(np.arctan, np.divide(correlated_image[1], correlated_image[0]))))
return thetas_for_each_pixel
def blur_function(image: np.ndarray, kernel_size: int) -> np.ndarray:
"""This function will return the blurry version of the original image using average function
Args:
image(np.ndarray): our image as numpy array
kernel_size(int): size of our kernel , n for nxn kernel
Returns:
np.ndarray : blurry image
"""
custom_kernel = np.ones((kernel_size, kernel_size), dtype=np.float) / kernel_size ** 2
blurry_image = correlate2d(image, custom_kernel, mode='same', boundary='fill')
return blurry_image
def unsharp_mask(image: np.ndarray, blur_kernel_size: int = 4) -> np.ndarray:
"""Simple function to apply unsharp masking filter on an image
Args:
image(np.ndarray): input image as numpy array
blur_kernel_size(int): this would be the kernel_size in blur_function(image, kernel_size)
Returns:
np.ndarray: our image after unsharp masking
"""
mask = np.subtract(image, blur_function(image, blur_kernel_size))
unsharpened_image = normalize_matrix(image + mask)
return unsharpened_image
def high_boost_filtering(image: np.ndarray, k: int, blur_kernel_size: int = 4) -> np.ndarray:
"""High boost filtering on input image with custom kernel size and blur kernel size
Args:
image(np.ndarray): our input image as numpy array
k(int): k in the formula : image + k * mask
blur_kernel_size(int): blur function kernel size
Returns:
np.ndarray: image after the high boost filtering
"""
high_boosted_image = image + k * np.subtract(image, blur_function(image, blur_kernel_size))
high_boosted_image = normalize_matrix(high_boosted_image)
return high_boosted_image
def gaussian_blur_filter(image: np.ndarray) -> np.ndarray:
"""Gausian blur function using pre-made kernel instead of original formula
Args:
image(np.ndarray): input image as numpy array
Returns:
np.ndarray: blurry image
"""
gaussian_kernel = np.array([[1, 4, 7, 4, 1], [4, 16, 26, 16, 4], [7, 26, 41, 26, 7], [4, 16, 26, 16, 4],
[1, 4, 7, 4, 1]], dtype=np.float32) / 273
blurry_image = correlate(image, gaussian_kernel)
return blurry_image
def map_to_eight_thetas(thetas_matrix: np.ndarray) -> np.ndarray:
"""Mapping our thetas to 8 degrees
Args:
thetas_matrix(np.ndarray): array of calculated thetas
Returns:
np.ndarray: mapped thetas
"""
# Saving pi value
pi = np.pi
# First we Mirror one side of the circle on the other side just to simplify the problem
with np.nditer(thetas_matrix, op_flags=['readwrite']) as it:
for i in it:
if i < 0:
i[...] += pi
# Mapping process
with np.nditer(thetas_matrix, op_flags=['readwrite']) as it:
for i in it:
if (0 <= i < pi / 8) or (7 * pi / 8 < i <= pi):
i[...] = pi / -2
elif pi / 8 < i <= 3 * pi / 8:
i[...] = pi / -4
elif 3 * pi / 8 < i <= 5 * pi / 8:
i[...] = 0
elif 5 * pi / 8 < i <= 7 * pi / 8:
i[...] = pi / 4
else:
i[...] = 0
return thetas_matrix
def non_maximum_suppresion(mapped_thetas: np.ndarray, image: np.ndarray) -> np.ndarray:
""" Non maximum suppresion operation on mapped thetas
Args:
mapped_thetas(np.ndarray): mapped to 8 degrees thetas
image(np.ndarray): original image
Returns:
np.ndarray: new image after operation
"""
new_image = np.zeros(image.shape)
neighbor_1 = 0
neighbor_2 = 255
with np.nditer(image, op_flags=['readwrite'], flags=['multi_index']) as it:
for i in it:
try:
x, y = it.multi_index
if mapped_thetas[x, y] == (np.pi / -2):
neighbor_2 = image[x, y + 1]
neighbor_1 = image[x, y - 1]
elif mapped_thetas[x, y] == (np.pi / -4):
neighbor_2 = image[x + 1, y + 1]
neighbor_1 = image[x - 1, y - 1]
elif mapped_thetas[x, y] == 0:
neighbor_2 = image[x + 1, y]
neighbor_1 = image[x - 1, y]
elif mapped_thetas[x, y] == (np.pi / 4):
neighbor_2 = image[x - 1, y + 1]
neighbor_1 = image[x + 1, y - 1]
if (i > neighbor_1) and (i > neighbor_2):
new_image[x, y] = i
else:
pass
except IndexError:
pass
return new_image
def double_threshold(image: np.ndarray, higher_ratio: float, lower_ratio: float) -> np.ndarray:
"""A double thereshould function between two rates
Args:
image(np.ndarray): image as numpy array
higher_ratio(float): Higher ratio to find upper bound
lower_ratio(float): Lower ratio to find lower bound
Returns:
np.ndarray: image after threshold appliance
"""
upper_bound, lower_bound = image.max() * higher_ratio, image.max() * higher_ratio * lower_ratio
after_threshold = normalize_matrix(image, upper_bound, lower_bound, 100)
return after_threshold
def leftover_threshold(image: np.ndarray) -> np.ndarray:
"""Leftover thresholding operation function
Args:
image(np.ndarray): input image as numpy array
Returns:
np.ndarray: image after operation
"""
with np.nditer(image, op_flags=['readwrite'], flags=['multi_index']) as it:
for i in it:
try:
x, y = it.multi_index
if 0 < i < 255:
sum_of_logical_bounds = ((image[x + 1, y - 1] == 255) or (image[x + 1, y] == 255) or
(image[x + 1, y + 1] == 255) or (image[x, y - 1] == 255) or (
image[x, y + 1] == 255) or
(image[x - 1, y - 1] == 255) or (image[x - 1, y] == 255) or (
image[x - 1, y + 1] == 255))
if sum_of_logical_bounds:
i[...] = 255
else:
i[...] = 0
except IndexError:
pass
return image
def custom_canny(image: np.ndarray, higherratio: float, lowerratio: float) -> np.ndarray:
"""Implementation of a custom Canny edge detection
Args:
image(np.ndarray): input image
higherratio: ratio for double threshold
lowerratio: ratio for double threshold
Returns:
np.ndarray: edge detected image
"""
# Wrapping up our previous functions
image_ = gaussian_blur_filter(image)
mapped_thetas = map_to_eight_thetas(pixel_theta(image_))
sobel_blurry_image = sobel_operator(image_)
sobel_blurry_image = gaussian_blur_filter(sobel_blurry_image)
canny_result = non_maximum_suppresion(mapped_thetas, sobel_blurry_image)
canny_result = leftover_threshold(double_threshold(canny_result, higherratio, lowerratio))
return canny_result
def power_transform(image: np.ndarray, gamma_: float) -> np.ndarray:
"""Gamma correction function
Args:
image(np.ndarray):input image
gamma_(float): Gamma correction ratio
Returns:
np.ndarray: image after transformation
"""
gamma_corrected = np.array(255 * (image / 255) ** gamma_, dtype='uint8')
return gamma_corrected
def bone_scan_enhancement(image: np.ndarray) -> np.ndarray:
""" Funtion to enhance the quality of bone scans
Args:
image(np.ndarray): input image
Returns:
np.ndarray: enhanced image
"""
sharpened_image = image_sharpening(image)
sobel_blur = blur_function(sobel_operator(image), 5)
# Creating the mask
enhanced_image = np.multiply(sharpened_image, sobel_blur, dtype=float)
# Scale between 0 and 255
enhanced_image /= enhanced_image.max() / 255.0
# Sum of image and mask
enhanced_image += image
# Removing values greater and 255 and lower than 0
enhanced_image = normalize_matrix(enhanced_image)
# Power law Transformation
enhanced_image = power_transform(enhanced_image, 0.9)
return enhanced_image
def plot(image, name):
plt.title(name)
plt.imshow(image, cmap='gray')
plt.show()
# This block is only generated for testing purposes
if __name__ == '__main__':
skeleton = cv2.imread('sample_images/skeleton.tif', 0).astype(np.float32)
camera_man = cv2.imread('sample_images/cameraman.tif', 0).astype(np.float32)
plot(camera_man, "original cameraman")
plot(image_sharpening(camera_man), "Image Sharpening")
plot(sobel_operator(camera_man), "Sobel Edge Detection")
plot(pixel_theta(camera_man), "Theta of Sobel formula")
plot(unsharp_mask(camera_man), "Unsharp Masking")
plot(high_boost_filtering(camera_man, 3), "High Boost Filtering")
plot(skeleton, "original medical photo")
plot(bone_scan_enhancement(skeleton), "Bone Scan Enhancement")
plot(custom_canny(camera_man, 0.088, 0.05), "Canny Edge Detection")