-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdata_augmentation.py
62 lines (58 loc) · 2.22 KB
/
data_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
from os import listdir
from os.path import join
import cv2
from PIL import Image
def is_image_file(filename):
return any(filename.endswith(extension) for extension in ['.png', 'bmp', '.jpg', '.jpeg', '.PNG', '.JPG', '.JPEG'])
def rotate_image(image, angle):
image_center = tuple(np.array(image.shape[1::-1]) / 2)
rot_mat = cv2.getRotationMatrix2D(image_center, angle, 1.0)
result = cv2.warpAffine(image, rot_mat, image.shape[1::-1], flags=cv2.INTER_LINEAR)
return result
idx = 0
for path in listdir('location/train'):
path = 'location/train/' + path
if is_image_file(path) == False:
continue
img = cv2.imread(path)
x = 0
y = 0
while y < img.shape[0]:
if x+96 < img.shape[1] and y+96 < img.shape[0]:
img_org = img[y:y+96 , x:x+96]
filename = 'location/Train_sub_images' + '/' + str(idx) + '.jpg'
cv2.imwrite(filename , img_org)
idx = idx+1
img_flip = cv2.flip(img_org , 0)
filename = 'location/Train_sub_images' + '/' + str(idx) + '.jpg'
cv2.imwrite(filename , img_flip)
idx = idx+1
img_rotate_90 = rotate_image(img_org , 90)
filename = 'location/Train_sub_images' + '/' + str(idx) + '.jpg'
cv2.imwrite(filename , img_rotate_90)
idx = idx+1
img_rotate_90_flip = cv2.flip(img_rotate_90 , 0)
filename = 'location/Train_sub_images' + '/' + str(idx) + '.jpg'
cv2.imwrite(filename , img_rotate_90_flip)
idx = idx+1
img_rotate_180 = rotate_image(img_org , 180)
filename = 'location/Train_sub_images' + '/' + str(idx) + '.jpg'
cv2.imwrite(filename , img_rotate_180)
idx = idx+1
img_rotate_180_flip = cv2.flip(img_rotate_180 , 0)
filename = 'location/Train_sub_images' + '/' + str(idx) + '.jpg'
cv2.imwrite(filename , img_rotate_180_flip)
idx = idx+1
img_rotate_270 = rotate_image(img_org , 270)
filename = 'location/Train_sub_images' + '/' + str(idx) + '.jpg'
cv2.imwrite(filename , img_rotate_270)
idx = idx+1
img_rotate_270_flip = cv2.flip(img_rotate_270 , 0)
filename = 'location/Train_sub_images' + '/' + str(idx) + '.jpg'
cv2.imwrite(filename , img_rotate_270_flip)
idx = idx+1
x = x + 57
else:
x = 0
y = y + 57
print(idx)