-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdata_augment.py
135 lines (84 loc) · 2.45 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
import cv2
import numpy as np
from random import random
from time import sleep
import matplotlib.pyplot as plt
def process(img):
out = np.copy(img)
if int(0.5+ random()):
out = random_noise(out)
if int(0.5+ random()):
out = random_crop(out)
if int(0.5+ random()):
out = random_pos(out)
if int(0.5+ random()):
out = random_rotation(out)
if int(0.5+ random()):
out = horizontal_flip(out)
if int(0.5+ random()):
out = jitter_contrast(out)
if int(0.5+ random()):
out = random_colour(out)
return out
def random_pos(img):
(h,w) = img.shape[:2]
x0 = -10 + random()*(w/5)
y0 = -10 + random()*(h/5)
translation_matrix = np.float32([ [1,0,x0], [0,1,y0] ])
out = cv2.warpAffine(img, translation_matrix, (w, h))
return out
'Takes RGB image and changes the colour randomly'
def random_colour(img):
d = int(random()*2.999)
alpha = random()*1.5
out = np.copy(img)
out[:,:,d] = img[:,:,d]*alpha
return out
'Takes RGB image and changes the rotation randomly'
def random_rotation(img):
(h, w) = img.shape[:2]
center = (w / 2, h / 2)
theta = -20 + random()*40
M = cv2.getRotationMatrix2D(center, theta, 1.0)
out = cv2.warpAffine(img, M, (w, h))
return out
'Takes RGB image and returns a random crop of the image'
def random_crop(img):
(h, w) = img.shape[:2]
h_1_4 = h/4.0
w_1_4 = w/4.0
x0 = random()*w_1_4
y0 = random()*h_1_4
out = img[x0:x0+(3)*w_1_4, y0:y0+(3)*h_1_4]
out = cv2.resize(out, (h, w), interpolation = cv2.INTER_AREA)
return out
'Takes RGB image and returns a horizontally flipped image'
def horizontal_flip(img):
return cv2.flip(img, 1)
def jitter_contrast(img):
maxIntensity = 255.0 # depends on dtype of image data
# Parameters for manipulating image data
alpha = 1 + random()*0.5
beta = 1 + random()*0.5
phi = 0.2 + random()*2
out = np.copy(img)
out[:,:,0] = (maxIntensity/alpha)*(out[:,:,0]/(maxIntensity/beta))**phi
out[:,:,1] = (maxIntensity/alpha)*(out[:,:,1]/(maxIntensity/beta))**phi
out[:,:,2] = (maxIntensity/alpha)*(out[:,:,2]/(maxIntensity/beta))**phi
return out
def random_noise(img):
noise = np.zeros(img.shape, np.uint8)
# cv2.randn(noise,(0),(99),)
alpha = 0.1*random()
cv2.randn(noise, np.zeros(3), np.ones(3)*255*alpha)
out = img + noise
return out
if __name__ == '__main__':
img = cv2.imread('face.jpg')
cv2.imshow('Image', img)
while (True):
img2 = process(img)
cv2.imshow('Image Proc',img2)
if cv2.waitKey(1) & 0xFF == ord('q'):
pass
sleep(0.5)