-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdistortion_model.py
143 lines (117 loc) · 3.84 KB
/
distortion_model.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
import math
import numpy as np
def distortionParameter(types):
parameters = []
if (types == 'barrel'):
Lambda = np.random.random_sample() * -5e-5 / 4
x0 = 256
y0 = 256
parameters.append(Lambda)
parameters.append(x0)
parameters.append(y0)
return parameters
elif (types == 'pincushion'):
Lambda = np.random.random_sample() * 8.6e-5 / 4
x0 = 128
y0 = 128
parameters.append(Lambda)
parameters.append(x0)
parameters.append(y0)
return parameters
elif (types == 'rotation'):
theta = np.random.random_sample() * 30 - 15
radian = math.pi * theta / 180
sina = math.sin(radian)
cosa = math.cos(radian)
parameters.append(sina)
parameters.append(cosa)
return parameters
elif (types == 'shear'):
shear = np.random.random_sample() * 0.8 - 0.4
parameters.append(shear)
return parameters
elif (types == 'projective'):
x1 = 0
x4 = np.random.random_sample() * 0.1 + 0.1
x2 = 1 - x1
x3 = 1 - x4
y1 = 0.005
y4 = 1 - y1
y2 = y1
y3 = y4
a31 = ((x1 - x2 + x3 - x4) * (y4 - y3) - (y1 - y2 + y3 - y4) * (x4 - x3)) / (
(x2 - x3) * (y4 - y3) - (x4 - x3) * (y2 - y3))
a32 = ((y1 - y2 + y3 - y4) * (x2 - x3) - (x1 - x2 + x3 - x4) * (y2 - y3)) / (
(x2 - x3) * (y4 - y3) - (x4 - x3) * (y2 - y3))
a11 = x2 - x1 + a31 * x2
a12 = x4 - x1 + a32 * x4
a13 = x1
a21 = y2 - y1 + a31 * y2
a22 = y4 - y1 + a32 * y4
a23 = y1
parameters.append(a11)
parameters.append(a12)
parameters.append(a13)
parameters.append(a21)
parameters.append(a22)
parameters.append(a23)
parameters.append(a31)
parameters.append(a32)
return parameters
elif (types == 'wave'):
mag = np.random.random_sample() * 32
parameters.append(mag)
return parameters
def distortionModel(types, xd, yd, W, H, parameter):
if (types == 'barrel' or types == 'pincushion'):
Lambda = parameter[0]
x0 = parameter[1]
y0 = parameter[2]
coeff = 1 + Lambda * ((xd - x0) ** 2 + (yd - y0) ** 2)
if (coeff == 0):
xu = W
yu = H
else:
xu = (xd - x0) / coeff + x0
yu = (yd - y0) / coeff + y0
return xu, yu
elif (types == 'rotation'):
sina = parameter[0]
cosa = parameter[1]
xu = cosa * xd + sina * yd + (1 - sina - cosa) * W / 2
yu = -sina * xd + cosa * yd + (1 + sina - cosa) * H / 2
return xu, yu
elif (types == 'shear'):
shear = parameter[0]
xu = xd + shear * yd - shear * W / 2
yu = yd
return xu, yu
elif (types == 'projective'):
a11 = parameter[0]
a12 = parameter[1]
a13 = parameter[2]
a21 = parameter[3]
a22 = parameter[4]
a23 = parameter[5]
a31 = parameter[6]
a32 = parameter[7]
im = xd / (W - 1.0)
jm = yd / (H - 1.0)
xu = (W - 1.0) * (a11 * im + a12 * jm + a13) / (a31 * im + a32 * jm + 1)
yu = (H - 1.0) * (a21 * im + a22 * jm + a23) / (a31 * im + a32 * jm + 1)
return xu, yu
elif (types == 'wave'):
mag = parameter[0]
yu = yd
xu = xd + mag * math.sin(math.pi * 4 * yd / W)
return xu, yu
def random_k():
k1 = np.random.random_sample( )*8.5e-4
k_params = [k1, k1 * 1e-04, k1 * 1e-06, k1 * 3e-15]
return k_params
def fisheye_distortion(i, j, k_params, center_x, center_y):
r_d = np.sqrt((i - center_x) ** 2 + (j - center_y) ** 2)
r_c = 0
for i, k in enumerate(k_params, start=1):
r_c += k * (r_d ** (2 * i - 1))
return r_d, r_c