forked from astar-ai/calicam_mono
-
Notifications
You must be signed in to change notification settings - Fork 0
/
calicam_mono.py
278 lines (195 loc) · 6.84 KB
/
calicam_mono.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import sys
import cv2
import math
import numpy as np
#-------------------------------------------------------------------------------#
Kl = None
Dl = None
xil = None
mapx = None
mapy = None
cap_cols = None
cap_rows = None
vfov_now = 120
width_now = 640
changed = False
margin = 15. * math.pi / 180.
mode = 'kRectPerspective'
#-------------------------------------------------------------------------------#
def load_parameters(param_file):
global Kl, Dl, xil
global cap_cols, cap_rows
fs = cv2.FileStorage(param_file, cv2.FILE_STORAGE_READ)
cap_size_node = fs.getNode("cap_size")
cap_cols = int(cap_size_node.at(0).real())
cap_rows = int(cap_size_node.at(1).real())
Kl = fs.getNode("Kl").mat()
Dl = fs.getNode("Dl").mat()
xil = fs.getNode("xil").mat()
#-------------------------------------------------------------------------------#
def init_undistort_rectify_map(k, d, r, knew, xi0, size, mode):
fx = k[0, 0]
fy = k[1, 1]
cx = k[0, 2]
cy = k[1, 2]
s = k[0, 1]
k1 = d[0, 0]
k2 = d[0, 1]
p1 = d[0, 2]
p2 = d[0, 3]
ki = np.linalg.inv(knew)
ri = np.linalg.inv(r)
kri = np.linalg.inv(np.matmul(knew, r))
rows = size[0]
cols = size[1]
mapx = np.zeros((rows, cols), dtype = np.float32)
mapy = np.zeros((rows, cols), dtype = np.float32)
print("Wait, this takes a while ... ")
for r in range(rows):
for c in range(cols):
xc = 0.0
yc = 0.0
zc = 0.0
if mode == 'kRectPerspective':
cr1 = np.array([c, r, 1.])
xc = np.dot(kri[0, :], cr1)
yc = np.dot(kri[1, :], cr1)
zc = np.dot(kri[2, :], cr1)
if mode == 'kRectLonglat':
tt = (c * 1. / (cols - 1) - 0.5) * math.pi
pp = (r * 1. / (rows - 1) - 0.5) * math.pi
xn = math.sin(tt)
yn = math.cos(tt) * math.sin(pp)
zn = math.cos(tt) * math.cos(pp)
cr1 = np.array([xn, yn, zn])
xc = np.dot(ri[0, :], cr1)
yc = np.dot(ri[1, :], cr1)
zc = np.dot(ri[2, :], cr1)
if mode == 'kRectFisheye':
cr1 = np.array([c, r, 1.])
ee = np.dot(ki[0, :], cr1)
ff = np.dot(ki[1, :], cr1)
zz = 2. / (ee * ee + ff * ff + 1.)
xn = zz * ee
yn = zz * ff
zn = zz - 1.
cr1 = np.array([xn, yn, zn])
xc = np.dot(ri[0, :], cr1)
yc = np.dot(ri[1, :], cr1)
zc = np.dot(ri[2, :], cr1)
if mode == 'kRectCylindrical':
cr1 = np.array([c, r, 1.])
tt = np.dot(ki[0, :], cr1)
pp = np.dot(ki[1, :], cr1) + margin
xc = -math.sin(pp) * math.cos(tt)
yc = -math.cos(pp)
zc = math.sin(pp) * math.sin(tt)
if zc < 0.0:
mapx[r, c] = np.float32(-1.)
mapy[r, c] = np.float32(-1.)
continue
rr = math.sqrt(xc * xc + yc * yc + zc * zc)
xs = xc / rr
ys = yc / rr
zs = zc / rr
xu = xs / (zs + xi0)
yu = ys / (zs + xi0)
r2 = xu * xu + yu * yu
r4 = r2 * r2
xd = (1 + k1 * r2 + k2 * r4) * xu + 2 * p1 * xu * yu + p2 * (r2 + 2 * xu * xu)
yd = (1 + k1 * r2 + k2 * r4) * yu + 2 * p2 * xu * yu + p1 * (r2 + 2 * yu * yu)
u = fx * xd + s * yd + cx
v = fy * yd + cy
mapx[r, c] = np.float32(u)
mapy[r, c] = np.float32(v)
return mapx, mapy
#-------------------------------------------------------------------------------#
def init_rectify_map():
global mode, mapx, mapy
Rl = np.identity(3, dtype = np.float64)
Knew = None
if mode == 'kRectPerspective':
print('kRectPerspective')
vfov_rad = vfov_now * math.pi / 180.
focal = width_now / 2. / math.tan(vfov_rad / 2.)
Knew = np.identity(3, dtype = np.float64)
Knew[0, 0] = focal
Knew[1, 1] = focal
Knew[0, 2] = width_now / 2 - 0.5
Knew[1, 2] = width_now / 2 - 0.5
img_size = [width_now, width_now]
mapx, mapy = init_undistort_rectify_map(Kl, Dl, Rl, Knew, xil, img_size, 'kRectPerspective')
print('Width: {}, Height: {}, V.FoV: {}'.format(width_now, width_now, vfov_now))
if mode == 'kRectLonglat':
print('kRectLonglat')
Knew = np.identity(3, dtype = np.float64)
Knew[0, 0] = width_now / math.pi
Knew[1, 1] = width_now / math.pi
img_size = [width_now, width_now]
mapx, mapy = init_undistort_rectify_map(Kl, Dl, Rl, Knew, xil, img_size, 'kRectLonglat')
print('Width: {}, Height: {}'.format(width_now, width_now))
if mode == 'kRectFisheye':
print('kRectFisheye')
Knew = np.identity(3, dtype = np.float64)
Knew[0, 0] = width_now / 2
Knew[1, 1] = width_now / 2
Knew[0, 2] = width_now / 2 - 0.5
Knew[1, 2] = width_now / 2 - 0.5
img_size = [width_now, width_now]
mapx, mapy = init_undistort_rectify_map(Kl, Dl, Rl, Knew, xil, img_size, 'kRectFisheye')
print('Width: {}, Height: {}'.format(width_now, width_now))
if mode == 'kRectCylindrical':
print('kRectCylindrical')
Knew = np.identity(3, dtype = np.float64)
Knew[0, 0] = width_now / math.pi
Knew[1, 1] = width_now / (math.pi - 2 * margin)
img_size = [width_now, width_now]
mapx, mapy = init_undistort_rectify_map(Kl, Dl, Rl, Knew, xil, img_size, 'kRectCylindrical')
print('Width: {}, Height: {}'.format(width_now, width_now))
print('K Matrix:')
print(Knew)
print('')
#-------------------------------------------------------------------------------#
def main():
global changed, mode, mapx, mapy
param_file = "astar_calicam_mono.yml"
image_name = "times_square.jpg"
if len(sys.argv) == 2:
param_file = sys.argv[1]
if len(sys.argv) == 3:
param_file = sys.argv[1]
image_name = sys.argv[2]
load_parameters(param_file)
init_rectify_map()
raw_img = cv2.imread(image_name, 1)
param_win_name = "Raw Image: " + str(cap_cols) + " x " + str(cap_rows)
cv2.namedWindow(param_win_name)
while True:
if changed == True:
init_rectify_map()
changed = False
raw_imgl = raw_img
rect_imgl = cv2.remap(raw_imgl, mapx, mapy, cv2.INTER_LINEAR)
dim = (int(cap_cols / 2), int(cap_rows / 2))
small_img = cv2.resize(raw_imgl, dim, interpolation = cv2.INTER_NEAREST)
cv2.imshow(param_win_name, small_img)
cv2.imshow("Rectified Image", rect_imgl)
key = cv2.waitKey(1)
if key & 0xFF == ord('1'):
mode = 'kRectPerspective'
changed = True
if key & 0xFF == ord('2'):
mode = 'kRectCylindrical'
changed = True
if key & 0xFF == ord('3'):
mode = 'kRectFisheye'
changed = True
if key & 0xFF == ord('4'):
mode = 'kRectLonglat'
changed = True
if key & 0xFF == ord('q') or key == 27:
break
#-------------------------------------------------------------------------------#
if __name__ == "__main__":
main()
#-------------------------------------------------------------------------------#