-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_augment.py
172 lines (139 loc) · 5.79 KB
/
data_augment.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
import cv2
import copy
import numpy as np
from scipy.ndimage.interpolation import map_coordinates
from scipy.ndimage.filters import gaussian_filter
PATCH = 2
# 步骤:拉伸-弹性-方形化-旋转-镜像
# 弹性变换
# alpha越小,sigma越大,产生的偏差越小,和原图越接近
def Elastic_transform(image, alpha, sigma, random_state=None):
if random_state is None:
random_state = np.random.RandomState(None)
shape = image.shape
shape_size = shape[:2]
dx = gaussian_filter((random_state.rand(*shape) * 2 - 1), sigma) * alpha
dy = gaussian_filter((random_state.rand(*shape) * 2 - 1), sigma) * alpha
dz = np.zeros_like(dx)
x, y, z = np.meshgrid(np.arange(shape[1]), np.arange(shape[0]),
np.arange(shape[2]))
indices = np.reshape(y + dy,
(-1, 1)), np.reshape(x + dx,
(-1, 1)), np.reshape(z, (-1, 1))
return map_coordinates(image, indices, order=1,
mode='reflect').reshape(shape)
# 随机仿射弹性变换
def elastic_transform(image, alpha, sigma, alpha_affine, random_state=None):
if random_state is None:
random_state = np.random.RandomState(None)
shape = image.shape
shape_size = shape[:2]
# Random affine
center_square = np.float32(shape_size) // 2
square_size = min(shape_size) // 3
pts1 = np.float32([
center_square + square_size,
[center_square[0] + square_size, center_square[1] - square_size],
center_square - square_size
])
pts2 = pts1 + random_state.uniform(
-alpha_affine, alpha_affine, size=pts1.shape).astype(np.float32)
M = cv2.getAffineTransform(pts1, pts2)
image = cv2.warpAffine(image,
M,
shape_size[::-1],
borderMode=cv2.BORDER_REFLECT_101)
dx = gaussian_filter((random_state.rand(*shape) * 2 - 1), sigma) * alpha
dy = gaussian_filter((random_state.rand(*shape) * 2 - 1), sigma) * alpha
dz = np.zeros_like(dx)
x, y, z = np.meshgrid(np.arange(shape[1]), np.arange(shape[0]),
np.arange(shape[2]))
indices = np.reshape(y + dy,
(-1, 1)), np.reshape(x + dx,
(-1, 1)), np.reshape(z, (-1, 1))
return map_coordinates(image, indices, order=1,
mode='reflect').reshape(shape)
# 旋转
def rotate(image, angle, center=None, scale=1.0):
(h, w) = image.shape[:2]
# If no rotation center is specified, the center of the image is set as the rotation center
if center is None:
center = (w / 2, h / 2)
M = cv2.getRotationMatrix2D(center, angle, scale)
rotated = cv2.warpAffine(image, M, (w, h))
return rotated
# 镜像
def mirror(image):
size = image.shape
# Get an image that is the same as the original image, note this to use deep copy
iLR = copy.deepcopy(image)
h = size[0]
w = size[1]
for i in range(h): # row and col
for j in range(w):
iLR[i, w - 1 - j] = image[i, j] # Mirror formula
return iLR
# 拉伸
def resize(image, fx=1.0, fy=1.0):
size = image.shape
h = size[0]
w = size[1]
return cv2.resize(image, (int(w * fx), int(h * fy)))
# 正方形化
def squ_ize(image):
border = 4
(h, w) = image.shape[:2]
m = max(h, w)
re = cv2.copyMakeBorder(image, (m - h) // 2 + border,
m - (m - h) // 2 - h + border,
(m - w) // 2 + border,
m - (m - w) // 2 - w + border,
cv2.BORDER_CONSTANT,
value=[0, 0, 0])
return re
def exe_increser(file_dir, output_path, file_list_name, process_record,
image_type):
p = open(process_record, 'r')
process_base = int(p.readline())
p.close()
f = open(file_list_name, 'r')
image_list = f.readline().split(' ')
f.close()
rest = len(image_list) - process_base
for processed in range(rest):
img_name = image_list[processed + process_base]
image = cv2.imread(file_dir + '/' + img_name + image_type)
class_path = output_path + '/' + img_name.split('-')[0]
# img_name = img_name.split('.')[0]
# if not os.path.exists(class_path):
# os.mkdir(class_path)
for fx in (1.0, 1.1, 1.2): # x轴缩放因子
for fy in (1.0, 1.1, 1.2): # y轴缩放因子
if (fx != 1.0 and fx == fy):
continue
re = resize(image, fx, fy) # 拉伸
for alpha in (1, 2, 3, 4):
for sigma in (0.1, 0.15):
im_et = Elastic_transform(re, re.shape[1] * alpha,
re.shape[1] * sigma)
im_et_sq = squ_ize(im_et) # 方形化
iLR = mirror(im_et_sq) # 镜像
cv2.imwrite(
class_path + '/' + img_name + '_' + str(fx) + '_' +
str(fy) + '_' + str(alpha) + '_' + str(sigma) +
'.png', im_et_sq)
cv2.imwrite(
class_path + '/' + img_name + '_' + str(fx) + '_' +
str(fy) + '_' + str(alpha) + '_' + str(sigma) +
'_mirr.png', iLR)
print(str(process_base + processed) + ' ' + img_name)
p = open(process_record, 'w')
p.write(str(process_base + processed + 1))
p.close()
return
file_list_name = 'FileList.txt'
process_record = 'ProcessRecord.txt'
file_dir = 'src'
output_path = 'dst'
image_type = '.png'
exe_increser(file_dir, output_path, file_list_name, process_record, image_type)