-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdistort_wheel.py
149 lines (145 loc) · 5.78 KB
/
distort_wheel.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
from distort import distortion
import cv2
import constants as co
from constants import urand
import random
import numpy as np
class distortion_wheel:
def __init__(self,xdim,ydim):
"""remember xdim and ydim are flipped >_<"""
self.warp_distort = distortion(xdim,ydim)
self.wave_distort = distortion(xdim,ydim)
self.scale_distort = distortion(xdim,ydim)
self.rot_distort = distortion(xdim,ydim)
self.displace = distortion(xdim,ydim)
self.make_priorities()
self.initialize_distortions()
def set_warp(self):
self.warp_distort.create_sinusoidal_warp(
urand(co.warp_ax),
urand(co.warp_ay),
urand(co.warp_perx),
urand(co.warp_pery),
urand(co.warp_phax),
urand(co.warp_phay)
)
def set_wave(self):
self.wave_distort.create_sinusoidal_wave(
urand(co.wave_ax),
urand(co.wave_ay),
urand(co.wave_perx),
urand(co.wave_pery),
urand(co.wave_phax),
urand(co.wave_phay)
)
def set_tint(self):
self.tint = [urand(co.rgb_shift) for _ in range(3)]
def set_scale(self):
self.scx = urand(co.scale_x)
self.scy = urand(co.scale_y)
self.scale_distort.calculate_scale(
(self.scx,self.scy),
offset=(
urand(co.scale_x_offset),
urand(co.scale_y_offset)
)
)
def set_rotation(self):
self.rot_distort.calculate_rotation(
urand(co.rot_theta),
offset = (
urand(co.rot_offset_x),
urand(co.rot_offset_y)
)
)
def set_offset(self):
self.displace.create_affine(
1.,
1.,
0,
0,
urand(co.x_offset),
urand(co.y_offset)
)
def initialize_distortions(self):
self.set_warp()
self.set_wave()
self.set_scale()
self.set_rotation()
self.set_offset()
self.set_tint()
self.make_priorities()
def make_priorities(self):
self.wav_priority = urand(co.wave)
self.warp_priority = urand(co.warp)
self.affine_priority = urand(co.affine)
self.maxv = max(self.wav_priority,self.warp_priority,self.affine_priority)
self.minv = min(self.wav_priority,self.warp_priority,self.affine_priority)
def rotate_values(self, num_distorts=1):
#not particularly safe to use exec(), but should be fine
distort_list = ['self.set_' + x + '()' for x in \
['scale','wave','warp','offset','rotation','tint']]
funcs = random.sample(distort_list,num_distorts)
for func in funcs:
exec(func)
self.make_priorities()
def process_image(self,image):
#handle tint
for j, tint in enumerate(self.tint):
image[:,:,j] = np.uint8(
np.maximum(0,np.minimum(255,
np.uint(image[:,:,j]) + tint
)))
#now do distortions
if self.wav_priority == self.maxv:
image = self.wave_distort.process_image(image)
if self.warp_priority == self.minv:
if (self.scx + self.scy)/2 > 1:
image = self.scale_distort.process_image(image)
image = self.rot_distort.process_image(image)
else:
image = self.rot_distort.process_image(image)
image = self.scale_distort.process_image(image)
image = self.warp_distort.process_image(image)
else:
image = self.warp_distort.process_image(image)
if (self.scx + self.scy)/2 > 1:
image = self.scale_distort.process_image(image)
image = self.rot_distort.process_image(image)
else:
image = self.rot_distort.process_image(image)
image = self.scale_distort.process_image(image)
elif self.warp_priority == self.maxv:
image = self.warp_distort.process_image(image)
if self.wav_priority == self.minv:
if (self.scx + self.scy)/2 > 1:
image = self.scale_distort.process_image(image)
image = self.rot_distort.process_image(image)
else:
image = self.rot_distort.process_image(image)
image = self.scale_distort.process_image(image)
image = self.wave_distort.process_image(image)
else:
image = self.wave_distort.process_image(image)
if (self.scx + self.scy)/2 > 1:
image = self.scale_distort.process_image(image)
image = self.rot_distort.process_image(image)
else:
image = self.rot_distort.process_image(image)
image = self.scale_distort.process_image(image)
else:
if (self.scx + self.scy)/2 > 1:
image = self.scale_distort.process_image(image)
image = self.rot_distort.process_image(image)
else:
image = self.rot_distort.process_image(image)
image = self.scale_distort.process_image(image)
if self.wav_priority == self.minv:
image = self.warp_distort.process_image(image)
image = self.wave_distort.process_image(image)
else:
image = self.wave_distort.process_image(image)
image = self.warp_distort.process_image(image)
#displacement
self.displace.process_image(image)
return image