-
Notifications
You must be signed in to change notification settings - Fork 0
/
image_augmentation.py
164 lines (136 loc) · 6.46 KB
/
image_augmentation.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
from skimage.transform import warp, AffineTransform, ProjectiveTransform
from skimage.exposure import equalize_adapthist, equalize_hist, rescale_intensity, adjust_gamma, adjust_log, adjust_sigmoid
from skimage.filters import gaussian
from skimage.util import random_noise
import random
import numpy as np
import os
def rand_range(a, b):
'''
a utility functio to generate random float values in desired range
'''
return np.random.rand() * (b - a) + a
def random_affine(im):
'''
wrapper of Affine transformation with random scale, rotation, shear and translation parameters
'''
tform = AffineTransform(scale=(rand_range(0.9, 1.1), rand_range(0.9, 1.1)),
rotation=rand_range(-0.25, 0.25),
shear=rand_range(-0.15, 0.15),
translation=(rand_range(-im.shape[0]//15, im.shape[0]//15),
rand_range(-im.shape[1]//15, im.shape[1]//15)))
return warp(im, tform.inverse, mode='reflect')
def random_perspective(im):
'''
wrapper of Projective (or perspective) transform, from 4 random points selected from 4 corners of the image within a defined region.
'''
region = 1/6
A = np.array([[0, 0], [0, im.shape[0]], [im.shape[1], im.shape[0]], [im.shape[1], 0]])
B = np.array([[int(rand_range(0, im.shape[1] * region)), int(rand_range(0, im.shape[0] * region))],
[int(rand_range(0, im.shape[1] * region)), int(rand_range(im.shape[0] * (1-region), im.shape[0]))],
[int(rand_range(im.shape[1] * (1-region), im.shape[1])), int(rand_range(im.shape[0] * (1-region), im.shape[0]))],
[int(rand_range(im.shape[1] * (1-region), im.shape[1])), int(rand_range(0, im.shape[0] * region))],
])
pt = ProjectiveTransform()
pt.estimate(A, B)
return warp(im, pt, output_shape=im.shape[:2])
def center_crop(image,label,output_size):
'''
croping the image in the center from a random margin from the borders
'''
# pad the sample if necessary
if label.shape[0] <= output_size[0] or label.shape[1] <= output_size[1] or label.shape[2] <= \
output_size[2]:
pw = max((output_size[0] - label.shape[0]) // 2 + 3, 0)
ph = max((output_size[1] - label.shape[1]) // 2 + 3, 0)
pd = max((output_size[2] - label.shape[2]) // 2 + 3, 0)
image = np.pad(image, [(pw, pw), (ph, ph), (pd, pd)], mode='constant', constant_values=0)
label = np.pad(label, [(pw, pw), (ph, ph), (pd, pd)], mode='constant', constant_values=0)
(w, h, d) = image.shape
w1 = int(round((w - output_size[0]) / 2.))
h1 = int(round((h - output_size[1]) / 2.))
d1 = int(round((d - output_size[2]) / 2.))
label = label[w1:w1 + output_size[0], h1:h1 + output_size[1], d1:d1 + output_size[2]]
image = image[w1:w1 + output_size[0], h1:h1 + output_size[1], d1:d1 + output_size[2]]
return image, label
def RandomRotFlip(image,label):
"""
Crop randomly flip the dataset in a sample
Args:
output_size (int): Desired output size
"""
k = np.random.randint(0, 4)
image = np.rot90(image, k)
label = np.rot90(label, k)
axis = np.random.randint(0, 2)
image = np.flip(image, axis=axis).copy()
label = np.flip(label, axis=axis).copy()
return image, label
def random_crop(image,label,output_size):
'''
croping the image in the center from a random margin from the borders
'''
#print('image shape_ ',np.shape(image))
# pad the sample if necessary
if label.shape[0] <= output_size[0] or label.shape[1] <= output_size[1] or label.shape[2] <= \
output_size[2]:
pw = max((output_size[0] - label.shape[0]) // 2 + 3, 0)
ph = max((output_size[1] - label.shape[1]) // 2 + 3, 0)
pd = max((output_size[2] - label.shape[2]) // 2 + 3, 0)
image = np.pad(image, [(pw, pw), (ph, ph), (pd, pd)], mode='constant', constant_values=0)
label = np.pad(label, [(pw, pw), (ph, ph), (pd, pd)], mode='constant', constant_values=0)
(w, h, d) = image.shape
w1 = np.random.randint(0, w - output_size[0])
h1 = np.random.randint(0, h - output_size[1])
d1 = np.random.randint(0, d - output_size[2])
label = label[w1:w1 + output_size[0], h1:h1 + output_size[1], d1:d1 + output_size[2]]
image = image[w1:w1 + output_size[0], h1:h1 + output_size[1], d1:d1 + output_size[2]]
return image, label
def random_intensity(im):
'''
rescales the intesity of the image to random interval of image intensity distribution
'''
return rescale_intensity(im,
in_range=tuple(np.percentile(im, (rand_range(0,10), rand_range(90,100)))),
out_range=tuple(np.percentile(im, (rand_range(0,10), rand_range(90,100)))))
def random_gamma(im):
'''
Gamma filter for contrast adjustment with random gamma value.
'''
return adjust_gamma(im, gamma=rand_range(0.5, 1.5))
def random_gaussian(im):
'''
Gaussian filter for bluring the image with random variance.
'''
return gaussian(im, sigma=rand_range(0, 5))
def random_filter(im):
'''
randomly selects an exposure filter from histogram equalizers, contrast adjustments, and intensity rescaler and applys it on the input image.
filters include: equalize_adapthist, equalize_hist, rescale_intensity, adjust_gamma, adjust_log, adjust_sigmoid, gaussian
'''
filters = [equalize_adapthist, equalize_hist, adjust_log, adjust_sigmoid, random_gamma, random_gaussian, random_intensity]
filt = random.choice(filters)
return filt(im)
def augment(im,lab, output_size,im_size=None, center_crop_flag=0,random_affine_flag=0, random_perspective_flag=0, random_filter_flag=0, random_noise_flag=0, random_crop_flag=0, random_rotflip_flag=0,resize_flag=0):
'''
image augmentation by doing a sereis of transfomations on the image.
'''
if center_crop_flag:
im, lab = center_crop(im,lab,output_size)
if random_rotflip_flag:
im, lab = RandomRotFlip(im,lab)
if random_crop_flag:
im, lab = random_crop(im,lab,output_size)
if random_affine_flag:
im = random_affine(im)
if random_perspective_flag:
im = random_perspective(im)
if random_filter_flag:
im = random_filter(im)
if random_noise_flag:
im = random_noise(im)
if resize_flag:
im = resize(im, im_size)
return im, lab
if __name__ == "__main__":
pass