-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransform.py
294 lines (231 loc) · 8.85 KB
/
transform.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
import cv2
import numpy as np
import random
import torch
def random_cropping(image, target_shape=(128, 128), p=0.5):
zeros = np.zeros(target_shape)
target_w, target_h = target_shape
width, height = image.shape
if random.random() < p:
start_x = random.randint(0, target_w - width)
start_y = random.randint(0, target_h - height)
zeros[start_x:start_x+width, start_y:start_y+height] = image
else:
start_x = (target_w - width)//2
start_y = (target_h - height)//2
zeros[start_x:start_x+width, start_y:start_y+height] = image
return zeros
def TTA_cropps(image, target_shape=(128, 128, 3)):
width, height, d = image.shape
target_w, target_h, d = target_shape
start_x = (target_w - width) // 2
start_y = (target_h - height) // 2
starts = [[start_x, start_y], [0, 0], [ 2 * start_x, 0],
[0, 2 * start_y], [2 * start_x, 2 * start_y]]
images = []
for start_index in starts:
image_ = image.copy()
x, y = start_index
zeros = np.zeros(target_shape)
zeros[x:x + width, y: y+height, :] = image_
image_ = zeros.copy()
image_ = (torch.from_numpy(image_).div(255)).float()
image_ = image_.permute(2, 0, 1)
images.append(image_)
zeros = np.fliplr(zeros)
image_ = zeros.copy()
image_ = (torch.from_numpy(image_).div(255)).float()
image_ = image_.permute(2, 0, 1)
images.append(image_)
return images
def random_erase(image,mask, p=0.5):
if random.random() < p:
width, height, d = image.shape
x = random.randint(0, width)
y = random.randint(0, height)
b_w = random.randint(5,10)
b_h = random.randint(5,10)
image[x:x+b_w, y:y+b_h] = 0
mask[x:x+b_w, y:y+b_h] = 0
return image, mask
def random_cropping3d(image, mask, target_shape=(8, 128, 128), p=0.5):
zeros = np.zeros(target_shape)
target_l,target_w, target_h = target_shape
length, width, height = image.shape
if random.random() < p:
start_x = random.randint(0, target_w - width)
start_y = random.randint(0, target_h - height)
else:
start_x = (target_w - width) // 2
start_y = (target_h - height) // 2
zeros[:target_l,start_x:start_x+width, start_y:start_y+height] = image
return zeros
def random_shift(image,mask, p=0.5):
if random.random() < p:
width, height, d = image.shape
zero_image = np.zeros_like(image)
zero_mask = np.zeros_like(mask)
w = random.randint(0, 20) - 10
h = random.randint(0, 30) - 15
zero_image[max(0, w): min(w+width, width), max(h, 0): min(h+height, height)] = \
image[max(0, -w): min(-w+width, width), max(-h, 0): min(-h+height, height)]
zero_mask[max(0, w): min(w + width, width), max(h, 0): min(h + height, height)] = \
mask[max(0, -w): min(-w + width, width), max(-h, 0): min(-h + height, height)]
image = zero_image.copy()
mask = zero_mask.copy()
return image, mask
def random_scale(image, mask, p=0.5):
if random.random() < p:
scale = random.random() * 0.1 + 0.9
assert 0.9 <= scale <= 1
width, height, d = image.shape
zero_image = np.zeros_like(image)
zero_mask = np.zeros_like(mask)
new_width = round(width * scale)
new_height = round(height * scale)
image = cv2.resize(image, (new_height, new_width))
mask = cv2.resize(mask, (new_height, new_width))
start_w = random.randint(0, width - new_width)
start_h = random.randint(0, height - new_height)
zero_image[start_w: start_w + new_width,
start_h:start_h+new_height] = image
image = zero_image.copy()
zero_mask[start_w: start_w + new_width,
start_h:start_h + new_height] = mask
mask = zero_mask.copy()
return image, mask
def change_scale(image, scale=1):
if 1:
assert 0.9 <= scale <= 1
width, height, d = image.shape
zero_image = np.zeros_like(image)
new_width = round(width * scale)
new_height = round(height * scale)
image = cv2.resize(image, (new_height, new_width))
start_w = (width - new_width)//2
start_h = (height - new_height)//2
zero_image[start_w: start_w + new_width,
start_h:start_h+new_height] = image
image = zero_image.copy()
return image
def random_flip(image, p=0.5):
if random.random() < p:
if len(image.shape) == 2:
image = np.flip(image, 1)
elif len(image.shape) == 3:
image = np.transpose(image, (1, 2, 0))
image = np.flip(image, 1)
image = np.transpose(image, (2, 0, 1))
return image
def do_gaussian_noise(image, sigma=0.5):
lab = cv2.cvtColor(image, cv2.COLOR_BGR2LAB)
gray, a, b = cv2.split(lab)
gray = gray.astype(np.float32)/255
H,W = gray.shape
noise = np.random.normal(0,sigma,(H,W))
noisy = gray + noise
noisy = (np.clip(noisy,0,1)*255).astype(np.uint8)
lab = cv2.merge((noisy, a, b))
image = cv2.cvtColor(lab, cv2.COLOR_LAB2BGR)
return image
def do_speckle_noise(image, sigma=0.5):
lab = cv2.cvtColor(image, cv2.COLOR_BGR2LAB)
gray, a, b = cv2.split(lab)
gray = gray.astype(np.float32)/255
H,W = gray.shape
noise = sigma*np.random.randn(H,W)
noisy = gray + gray * noise
noisy = (np.clip(noisy,0,1)*255).astype(np.uint8)
lab = cv2.merge((noisy, a, b))
image = cv2.cvtColor(lab, cv2.COLOR_LAB2BGR)
return image
def do_inv_speckle_noise(image, sigma=0.5):
lab = cv2.cvtColor(image, cv2.COLOR_BGR2LAB)
gray, a, b = cv2.split(lab)
gray = gray.astype(np.float32)/255
H,W = gray.shape
noise = sigma*np.random.randn(H,W)
noisy = gray + (1-gray) * noise
noisy = (np.clip(noisy,0,1)*255).astype(np.uint8)
lab = cv2.merge((noisy, a, b))
image = cv2.cvtColor(lab, cv2.COLOR_LAB2BGR)
return image
def random_angle_rotate(image, mask, angles=[-30, 30]):
angle = random.randint(0, angles[1]-angles[0]) + angles[0]
image = rotate(image, angle)
mask = rotate(mask, angle)
return image, mask
def rotate(image, angle, center=None, scale=1.0):
(h, w) = image.shape[:2]
if center is None:
center = (w / 2, h / 2)
M = cv2.getRotationMatrix2D(center, angle, scale)
rotated = cv2.warpAffine(image, M, (w, h))
return rotated
## illumination ====================================================================================
def do_brightness_shift(image, alpha=0.125):
image = image.astype(np.float32)
image = image + alpha*255
image = np.clip(image, 0, 255).astype(np.uint8)
return image
def do_brightness_multiply(image, alpha=1):
image = image.astype(np.float32)
image = alpha*image
image = np.clip(image, 0, 255).astype(np.uint8)
return image
def do_contrast(image, alpha=1.0):
image = image.astype(np.float32)
gray = image * np.array([[[0.114, 0.587, 0.299]]]) #rgb to gray (YCbCr)
gray = (3.0 * (1.0 - alpha) / gray.size) * np.sum(gray)
image = alpha*image + gray
image = np.clip(image, 0, 255).astype(np.uint8)
return image
#https://www.pyimagesearch.com/2015/10/05/opencv-gamma-correction/
def do_gamma(image, gamma=1.0):
table = np.array([((i / 255.0) ** (1.0 / gamma)) * 255
for i in np.arange(0, 256)]).astype("uint8")
return cv2.LUT(image, table) # apply gamma correction using the lookup table
def do_clahe(image, clip=2, grid=16):
grid=int(grid)
lab = cv2.cvtColor(image, cv2.COLOR_BGR2LAB)
gray, a, b = cv2.split(lab)
gray = cv2.createCLAHE(clipLimit=clip, tileGridSize=(grid,grid)).apply(gray)
lab = cv2.merge((gray, a, b))
image = cv2.cvtColor(lab, cv2.COLOR_LAB2BGR)
return image
def do_flip_transpose(image, type=0):
#choose one of the 8 cases
if type==1: #rotate90
image = image.transpose(1,0,2)
image = cv2.flip(image,1)
if type==2: #rotate180
image = cv2.flip(image,-1)
if type==3: #rotate270
image = image.transpose(1,0,2)
image = cv2.flip(image,0)
if type==4: #flip left-right
image = cv2.flip(image,1)
if type==5: #flip up-down
image = cv2.flip(image,0)
if type==6:
image = cv2.flip(image,1)
image = image.transpose(1,0,2)
image = cv2.flip(image,1)
if type==7:
image = cv2.flip(image,0)
image = image.transpose(1,0,2)
image = cv2.flip(image,1)
return image
def bgr_to_gray(image):
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
image = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR)
return image
def do_flip_transpose_4(image, type=0):
#choose one of the 8 cases
if type==0: #rotate180
image = cv2.flip(image,-1)
if type==1: #flip left-right
image = cv2.flip(image,1)
if type==2: #flip up-down
image = cv2.flip(image,0)
return image