-
Notifications
You must be signed in to change notification settings - Fork 8
/
dataaug.py
158 lines (127 loc) · 5.56 KB
/
dataaug.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
import random
from scipy import ndarray
# image processing library
import skimage as sk
from skimage import transform
from skimage import util
from skimage import io
import os
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
def randRange(a, b):
'''
a utility functio to generate random float values in desired range
'''
return pl.rand() * (b - a) + a
def randomAffine(im):
'''
wrapper of Affine transformation with random scale, rotation, shear and translation parameters
'''
tform = AffineTransform(scale=(randRange(0.75, 1.3), randRange(0.75, 1.3)),
rotation=randRange(-0.25, 0.25),
shear=randRange(-0.2, 0.2),
translation=(randRange(-im.shape[0]//10, im.shape[0]//10),
randRange(-im.shape[1]//10, im.shape[1]//10)))
return warp(im, tform.inverse, mode='reflect')
def randomPerspective(im):
'''
wrapper of Projective (or perspective) transform, from 4 random points selected from 4 corners of the image within a defined region.
'''
region = 1/4
A = pl.array([[0, 0], [0, im.shape[0]], [im.shape[1], im.shape[0]], [im.shape[1], 0]])
B = pl.array([[int(randRange(0, im.shape[1] * region)), int(randRange(0, im.shape[0] * region))],
[int(randRange(0, im.shape[1] * region)), int(randRange(im.shape[0] * (1-region), im.shape[0]))],
[int(randRange(im.shape[1] * (1-region), im.shape[1])), int(randRange(im.shape[0] * (1-region), im.shape[0]))],
[int(randRange(im.shape[1] * (1-region), im.shape[1])), int(randRange(0, im.shape[0] * region))],
])
pt = ProjectiveTransform()
pt.estimate(A, B)
return warp(im, pt, output_shape=im.shape[:2])
def randomCrop(im):
'''
croping the image in the center from a random margin from the borders
'''
margin = 1/10
start = [int(randRange(0, im.shape[0] * margin)),
int(randRange(0, im.shape[1] * margin))]
end = [int(randRange(im.shape[0] * (1-margin), im.shape[0])),
int(randRange(im.shape[1] * (1-margin), im.shape[1]))]
return im[start[0]:end[0], start[1]:end[1]]
def randomIntensity(im):
'''
rescales the intesity of the image to random interval of image intensity distribution
'''
return rescale_intensity(im,
in_range=tuple(pl.percentile(im, (randRange(0,10), randRange(90,100)))),
out_range=tuple(pl.percentile(im, (randRange(0,10), randRange(90,100)))))
def randomGamma(im):
'''
Gamma filter for contrast adjustment with random gamma value.
'''
return adjust_gamma(im, gamma=randRange(0.5, 1.5))
def randomGaussian(im):
'''
Gaussian filter for bluring the image with random variance.
'''
return gaussian(im, sigma=randRange(0, 5))
def randomFilter(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, randomGamma, randomGaussian, randomIntensity]
filt = random.choice(Filters)
return filt(im)
def randomNoise(im):
'''
random gaussian noise with random variance.
'''
var = randRange(0.001, 0.01)
return random_noise(im, var=var)
def random_rotation(image_array: ndarray):
# pick a random degree of rotation between 25% on the left and 25% on the right
random_degree = random.uniform(-25, 25)
return sk.transform.rotate(image_array, random_degree)
def random_noise(image_array: ndarray):
# add random noise to the image
return sk.util.random_noise(image_array)
def horizontal_flip(image_array: ndarray):
# horizontal flip doesn't need skimage, it's easy as flipping the image array of pixels !
return image_array[:, ::-1]
# dictionary of the transformations we defined earlier
available_transformations = {
'rotate': random_rotation,
'noise': random_noise,
'horizontal_flip': horizontal_flip,
'intensity':randomIntensity,
'gamma':randomGamma,
'gaussian':randomGaussian
}
def augment(c,l,num):
num_files_desired = num
# find all files paths from the folder
images = l
print(images)
num_generated_files = 0
while num_generated_files <= num_files_desired:
# random image from the folder
image_path = random.choice(images)
print(num_generated_files)
# read image as an two dimensional array of pixels
image_to_transform = sk.io.imread(image_path)
# random num of transformation to apply
num_transformations_to_apply = random.randint(1, len(available_transformations))
num_transformations = 0
transformed_image = None
while num_transformations <= num_transformations_to_apply:
# random transformation to apply for a single image
key = random.choice(list(available_transformations))
transformed_image = available_transformations[key](image_to_transform)
num_transformations += 1
new_file_path = '%s/augmented_image_%s.jpg' % (folder_path, num_generated_files)
# write image to the disk
io.imsave(new_file_path, transformed_image)
num_generated_files += 1