-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage_processing.py
More file actions
239 lines (197 loc) · 8.16 KB
/
image_processing.py
File metadata and controls
239 lines (197 loc) · 8.16 KB
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
import cv2
import numpy as np
import matplotlib.pyplot as plt
import os
from typing import Dict, List, Optional, Tuple
# Save images
def save_images(image_dict: Dict[str, np.ndarray]):
'''Save all images in a dictionary, which contains the images and their corresponding names.
'''
filtered_dict = image_dict.copy()
filtered_dict.pop('Original', None)
for label, image in filtered_dict.items():
filename = f'{label}.png'
cv2.imwrite(filename, image)
print(f"Saved: '{filename}'")
# Image display
def display_image(data: Tuple[np.ndarray, str], figsize: Tuple[int, int] = (18, 18), cmap: Optional = None, filename: Optional[str] = None, visualisation = False, tight = False):
'''Display an image.
If cmap string is provided, the image is displayed with that colourmap.
If filename is provied, the image is saved with that filename.
'''
(image, name) = data
fig, ax = plt.subplots(1, 1, figsize=figsize)
ax.imshow(image, cmap=cmap)
if name:
ax.set_title(name)
ax.set_xticks([])
ax.set_yticks([])
if filename:
if tight:
plt.savefig(filename, bbox_inches = 'tight')
else:
plt.savefig(filename)
print('Image saved. Filename: ' + filename)
if visualisation:
plt.show()
plt.close()
def display_image_1D(data_set: List, figsize: Tuple[int, int] = (18, 18), orientation='horizontal', cmap: Optional[list] = None, filename: Optional[str] = None, visualisation = False):
'''Display multiple images in one direction.
Input images are stored in a list with tuples containting the images and their titles.
If cmap list is provided, the images are displayed with their corresponding colourmaps.
If filename is provided, the images are saved in one single image file.
'''
n = len(data_set)
i = 0
if orientation == 'horizontal':
fig, axs = plt.subplots(1, n, figsize=figsize)
elif orientation == 'vertical':
fig, axs = plt.subplots(n, 1, figsize=figsize)
if cmap:
assert len(data_set) == len(
cmap), 'Number of colour maps should match number of images'
for (image, name) in data_set:
axs[i].imshow(image, cmap=cmap[i])
axs[i].set_title(name)
axs[i].set_xticks([])
axs[i].set_yticks([])
i += 1
else:
for (image, name) in data_set:
axs[i].imshow(image)
axs[i].set_title(name)
axs[i].set_xticks([])
axs[i].set_yticks([])
i += 1
if filename:
plt.savefig(filename)
print('Image saved. Filename: ' + filename)
if visualisation:
plt.show()
plt.close()
def display_image_2D(data_set: List, rows: int, cols: int, figsize: Tuple[int, int] = (18, 18), cmap: Optional[list] = None, filename: Optional[str] = None, visualisation = False):
'''Display multiple images in a matrix.
Input images are stored in a list with tuples containting the images and their titles.
If cmap list is provided, the images are displayed with their corresponding colourmaps.
If filename is provided, the images are saved in one single image file.
'''
fig, axs = plt.subplots(rows, cols, figsize=figsize, squeeze=False)
row, col, i = 0, 0, 0
if cmap:
assert len(data_set) == len(
cmap), 'Number of colour maps should match number of images'
assert len(data_set) == rows * \
cols, 'Product of rows and cols should match numebr of images'
for (image, name) in data_set:
axs[row][col].imshow(image, cmap=cmap[i])
axs[row][col].set_title(name)
axs[row][col].set_xticks([])
axs[row][col].set_yticks([])
i += 1
col += 1
if col == cols:
row += 1
col = 0
else:
assert len(images) == rows * \
cols, 'Product of rows and cols should match numebr of images'
for (image, name) in data_set:
axs[row][col].imshow(image)
axs[row][col].set_title(name)
axs[row][col].set_xticks([])
axs[row][col].set_yticks([])
col += 1
if col == cols:
row += 1
col = 0
if filename:
plt.savefig(filename)
print('Image saved. Filename: ' + filename)
if visualisation:
plt.show()
plt.close()
# Algorithms
def denoise(image: np.ndarray, method: str = 'blur', **kwargs):
'''Conduct denoising on an image.
The method string dictates the denoising techinique used.
Keyword arguments are the ones used by that parcicular denoising technique.
'''
if method == 'blur':
image_denoised = cv2.blur(image, **kwargs)
elif method == 'gaussian':
image_denoised = cv2.GaussianBlur(image, **kwargs)
elif method == 'median':
image_denoised = cv2.medianBlur(image, **kwargs)
elif method == 'fastNlMeans':
image_denoised = cv2.fastNlMeansDenoising(image, **kwargs)
elif method == 'bilateral':
image_denoised = cv2.bilateralFilter(image, **kwargs)
return image_denoised
def edge_detect(image: np.ndarray, method: str):
'''Conduct edge dectection on an image.
The method string dictates the edge detection techinique used.
'''
if method == 'canny':
edges = cv2.Canny(image, 40, 100, 3)
elif method == 'sobel16':
sobelx = cv2.Sobel(image, cv2.CV_16S, 1, 0, ksize=5,
scale=1, delta=0, borderType=cv2.BORDER_DEFAULT)
sobely = cv2.Sobel(image, cv2.CV_16S, 0, 1, ksize=5,
scale=1, delta=0, borderType=cv2.BORDER_DEFAULT)
elif method == 'sobel':
sobelx = cv2.Sobel(image, cv2.CV_64F, 1, 0, ksize=5,
scale=1, delta=0, borderType=cv2.BORDER_DEFAULT)
sobely = cv2.Sobel(image, cv2.CV_64F, 0, 1, ksize=5,
scale=1, delta=0, borderType=cv2.BORDER_DEFAULT)
else:
print(r'Invalid method = {method}')
quit()
if method in ['sobel', 'sobel16']:
edges = np.abs(sobelx) + np.abs(sobely)
print(f'New edges: range=({edges.min()}, {edges.max()})')
edges_scaled = (edges*255/edges.max()).astype(np.uint8)
return edges_scaled
return edges
def edge_detect_multi(image_dict: Dict[str, np.ndarray], method: str):
'''Conduct edge detection on a dictionary of images.
'''
new_image_dict = {}
method_str = method.title()
for label, image in image_dict.items():
new_label = f'{method_str} on {label}'
new_image_dict[new_label] = edge_detect(image_dict[label], method=method)
return new_image_dict
def threshold(image: np.ndarray, method: str = 'adaptive_gaussian'):
'''Conduct thresholding on an image.
The method string dictates the thresholding techinique used, either adaptive Gaussian or Otsu.
'''
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
if method == 'adaptive_gaussian':
image_threshold = cv2.adaptiveThreshold(
image, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)
else:
thresh, image_threshold = cv2.threshold(
image, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
return image_threshold
def threshold_multi(image_dict: Dict[str, np.ndarray], method: str):
'''Conduct thresholding on a dictionary of images.
'''
new_image_dict = {}
method_str = method.title()
for label, image in image_dict.items():
new_label = f'{method_str} on {label}'
new_image_dict[new_label] = threshold_image(
image_dict[label], method=method)
return new_image_dict
def histogram(image: np.ndarray, figsize: Tuple[int, int] = (18, 18), filename: Optional[str] = None, visualisation = False):
'''Calculates and visualises the intensity histogram of an image.
'''
hist = cv2.calcHist([image], [0], None, [256], [0, 256])
fig, ax = plt.subplots(1, 1)
ax.plot(hist)
if filename:
plt.savefig(filename, bbox_inches = 'tight')
print('Image saved. Filename: ' + filename)
if visualisation:
plt.show()
plt.close()