-
Notifications
You must be signed in to change notification settings - Fork 0
/
preprocessing.py
189 lines (144 loc) · 6.25 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
import cv2 as cv
import numpy as np
class ImageTransformer:
def __init__(self, img_transformation_param):
self.transformation_parameters = img_transformation_param
@staticmethod
def resize(frame, resizing=None, plot=False):
height_old, width_old, _ = frame.shape
if resizing:
if type(resizing) == tuple:
height = resizing[1]
width = resizing[0]
elif type(resizing) == float:
height = resizing * height_old
width = resizing * width_old
scale = height / height_old
if scale < 1:
new_frame = cv.resize(frame, (width, height), interpolation=cv.INTER_AREA)
else:
new_frame = cv.resize(frame, (width, height), interpolation=cv.INTER_LINEAR)
if plot == True:
cv.imshow('Resized', new_frame)
cv.waitKey(0)
return new_frame
def crop(self, image, px_crop=None, plot=False):
if px_crop:
px_w0, px_wf, px_h0, px_hf = px_crop
else:
px_h_left = np.where(image[:,0,:] != 0)
px_h_right = np.where(image[:,-1,:] != 0)
if px_h_left[0].size != 0:
px_h0 = np.min([px_h_left[0][0],px_h_right[0][0]])
px_hf = np.max([px_h_left[0][-1],px_h_right[0][-1]])
else:
px_h0 = 0
px_hf = image.shape[0] - 1
px_w_top = np.where(image[0,:,:] != 0)
px_w_bottom = np.where(image[-1,:,:] != 0)
if px_w_top[0].size != 0:
px_w0 = np.min([px_w_top[0][0],px_w_bottom[0][0]])
px_wf = np.max([px_w_top[0][-1],px_w_bottom[0][-1]])
else:
px_w0 = 0
px_wf = image.shape[1] - 1
cropped_image = image[px_h0:px_hf,px_w0:px_wf,:]
if plot == True:
cv.imshow('Cropped', cropped_image)
cv.waitKey(0)
return cropped_image
def rotate(self, image, angle=None, rot_center_x=None, rot_center_y=None, plot=False):
image_height, image_width, _ = image.shape
if rot_center_x == None and rot_center_y == None:
rotCenter = (self.transformation_parameters['rotation'][0], self.transformation_parameters['rotation'][1])
if np.any(np.array(rotCenter) == None):
rotCenter = (image_width//2,image_height//2)
else:
rotCenter = (rot_center_x,rot_center_y)
if angle == None:
theta = self.transformation_parameters['rotation'][-1]
else:
theta = angle
A = cv.getRotationMatrix2D(rotCenter,-theta,scale=1.0)
rotated_image = cv.warpAffine(image,A,(image_width,image_height))
rotated_image = self.crop(rotated_image,plot=False) # remove black regions due to rotation
rotated_image = self.resize(rotated_image,(image.shape[1],image.shape[0]))
if plot == True:
cv.imshow('Rotated', rotated_image)
cv.waitKey(0)
return rotated_image
def translate(self, image, Dx=None, Dy=None, plot=False):
image_height, image_width, _ = image.shape
if Dx == None and Dy == None:
DX, DY = self.transformation_parameters['translation']
else:
DX = Dx
DY = Dy
A = np.array([[1,0,DX],[0,1,DY]],dtype=np.float64)
translated_image = cv.warpAffine(image,A,(image_width,image_height))
if plot == True:
cv.imshow('Translated', translated_image)
cv.waitKey(0)
return translated_image
def flip(self, image, axis, plot=False):
if axis == 'horizontal':
flipped_image = np.flip(image,axis=0)
elif axis == 'vertical':
flipped_image = np.flip(image, axis=1)
if plot == True:
cv.imshow('Flipped', flipped_image)
cv.waitKey(0)
return flipped_image
def filter(self, image, kernel_name=None, kernel_parameters=None, plot=False):
if kernel_name == 'gaussian':
kernel_size = kernel_parameters['ksize'] # kernel size
sigma = kernel_parameters['sigma'] # kernel sigma
filtered_image = cv.GaussianBlur(image,(kernel_size,kernel_size),sigma)
elif kernel_name == 'bilateral':
smoothing_diameter = kernel_parameters['d']
sigmaColor = kernel_parameters['sigmaColor']
sigmaSpace = kernel_parameters['sigmaSpace']
filtered_image = cv.bilateralFilter(image,smoothing_diameter,sigmaColor,sigmaSpace)
elif kernel_name == 'median':
kernel_size = kernel_parameters['ksize']
filtered_image = cv.medianBlur(image,kernel_size)
elif kernel_name == 'sharpen':
kernel = np.array([[0,-1,0],[-1,5,-1],[0,-1,0]])
filtered_image = cv.filter2D(image,-1,kernel)
elif kernel_name == 'sepia':
pass
#kernel = np.array([[0.272,0.534,0.131],[0.349,0.686,0.168],[0.393,0.769,0.189]])
#filtered_image = cv.filter2D(image,-1,kernel)
if plot == True:
cv.imshow('Blur', blur_image)
cv.waitKey(0)
return filtered_image
def zoom(self, image, f, plot=False):
dims = image.shape
if f == None:
zoom_factor = self.transformation_parameters['zoom']
else:
zoom_factor = f
angle = 0
cy, cx = [i//2 for i in dims[:-1]]
A = cv.getRotationMatrix2D((cx,cy),angle,zoom_factor)
zoomed_image = cv.warpAffine(image,A,dims[1::-1],flags=cv.INTER_LINEAR)
if plot == True:
cv.imshow('Zoomed image', zoomed_image)
cv.waitKey(0)
return zoomed_image
def launch_transform_operation(self, image):
plot_transformation = False
operation_functions = {
'resizing': self.resize,
'rotation': self.rotate,
'translation': self.translate,
'filter': self.filter,
'zoom': self.zoom,
'flip': self.flip,
}
for ID in self.transformation_parameters.keys():
F = operation_functions[ID]
fun_args = self.transformation_parameters[ID] + [plot_transformation]
image = F(image,*fun_args)
return image