-
Notifications
You must be signed in to change notification settings - Fork 16
/
mrcal-from-kalibr
executable file
·377 lines (302 loc) · 13.8 KB
/
mrcal-from-kalibr
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#!/usr/bin/env python3
# Copyright (c) 2017-2023 California Institute of Technology ("Caltech"). U.S.
# Government sponsorship acknowledged. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
r'''Converts a kalibr-formatted camera model to the .cameramodel file format
SYNOPSIS
$ mrcal-from-kalibr model.yaml
Wrote model-cam0.cameramodel
Wrote model-cam1.cameramodel
File formats supported by mrcal are described at
https://mrcal.secretsauce.net/cameramodels.html#cameramodel-file-formats
This tool converts the given model(s) to the mrcal-native .cameramodel file
format. No changes to the content are made; this is purely a format converter
(the mrcal-convert-lensmodel tool fits different lens models instead). Model
filenames are given on the commandline. Output is written to the same directory,
with the same filename, but annotated with the camera name from the data file,
and with a .cameramodel extension.
If the model is omitted or given as "-", the input is read from standard input,
and the output is written to standard output
Unlike mrcal .cameramodel files where one camera is described by one file, the
.yaml files used by kalibr are intended to describe multiple cameras. This
format conversion tool will write out multiple .cameramodel file to represent
the given data. To select a single camera, pass --camera CAMERA.
By default we set the extrinsics of camera-0 as they appear in the input: the
"previous" camera from camera-0 is used as the reference coordinate system. If
we want to force camera-0 to have an identity transform, pass
--cam0-at-reference. Usually this will be the case anyway in the input data, but
this option makes sure.
The kalibr format is described at
https://github.com/ethz-asl/kalibr/wiki/Yaml-formats
At this time we only support
- LENSMODEL_PINHOLE
- LENSMODEL_OPENCV4
- LENSMODEL_CAHVOR (experimental)
- LENSMODEL_CAHVORE (experimental)
which corresponds to
camera_model == 'pinhole' and distortion_model in ('radtan','none')
'''
import sys
import argparse
import re
import os
def parse_args():
parser = \
argparse.ArgumentParser(description = __doc__,
formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument('--cam0-at-reference',
action='store_true',
default=False,
help='''By default we set the extrinsics of camera-0 as
they appear in the input. If we want to force camera-0
to have an identity transform, pass --cam0-at-reference.
Usually this will be the case anyway in the input data,
but this option makes sure''')
parser.add_argument('--camera',
help='''Kalibr model files may contain multiple cameras,
selected by this argument. If omitted, all available
cameras will be read. If given, we read JUST the one
requested camera''')
parser.add_argument('--force', '-f',
action='store_true',
default=False,
help='''By default existing files are not overwritten. Pass --force to overwrite them
without complaint''')
parser.add_argument('--outdir',
required=False,
type=lambda d: d if os.path.isdir(d) else \
parser.error("--outdir requires an existing directory as the arg, but got '{}'".format(d)),
help='''Directory to write the output models into. If omitted, we write the output
models to the same directory as the input models''')
parser.add_argument('model',
default=['-'],
nargs='*',
type=str,
help='''Input camera model; '-' to read standard input''')
args = parser.parse_args()
if len(args.model) > 1:
print("ERROR: only one input file may be processed", file=sys.stderr)
sys.exit(1)
args.model = args.model[0]
return args
args = parse_args()
# arg-parsing is done before the imports so that --help works without building
# stuff, so that I can generate the manpages and README
import mrcal
import mrcal.cahvor
import yaml
import numpy as np
import numpysane as nps
Rt_camprev_ref = None
# stolen from mrcal-to-kalibr. Please consolidate
def Rt_is_identity(Rt):
cos_th = (np.trace(Rt[:3,:]) - 1.) / 2.
# cos_th ~ 1 - x^2/2
th_sq = (1 - cos_th)*2.
norm2_t = nps.norm2(Rt[3,:])
return norm2_t < 1e-12 and th_sq < 1e-12
def convert_one(d,name):
global Rt_camprev_ref
# I expect at least this structure:
#
# camera_model: pinhole
# intrinsics: [461.629, 460.152, 362.680, 246.049]
# distortion_model: radtan
# distortion_coeffs: [-0.27695497, 0.06712482, 0.00087538, 0.00011556]
# resolution: [752, 480]
# T_cn_cnm1:
# - [ 0.99998854, 0.00216014, 0.00427195,-0.11003785]
# - [-0.00221074, 0.99992702, 0.01187697, 0.00045792]
# - [-0.00424598,-0.01188627, 0.99992034,-0.00064487]
# - [0.0, 0.0, 0.0, 1.0]
#
# T_cn_cnm1 may be omitted: I use an identity transform in that case
camera_model_known = set(('pinhole',
'cahvor','cahvore'),)
if not d['camera_model'] in camera_model_known:
print(f"ERROR: at this time I only support kalibr camera_model: {camera_model_known}; got camera_model={d['camera_model']}",
file=sys.stderr)
sys.exit(1)
if not re.match('cahvor',d['camera_model']):
# The "usual" path
distortion_model_known = set(('radtan','none'),)
if not d['distortion_model'] in distortion_model_known:
print(f"ERROR: at this time I only support kalibr distortion_model: {distortion_model_known}; got distortion_model={d['distortion_model']}",
file=sys.stderr)
sys.exit(1)
if d['distortion_model'] == 'radtan':
intrinsics = ('LENSMODEL_OPENCV4',
np.array(d['intrinsics'] +
d['distortion_coeffs'],
dtype=float))
else:
intrinsics = ('LENSMODEL_PINHOLE',
np.array(d['intrinsics'],
dtype=float))
m = mrcal.cameramodel( intrinsics = intrinsics,
imagersize = np.array(d['resolution'],
dtype=int) )
else:
# cahvor or cahvore. This can only come from the hacked kalibr at jpl
if 'distortion_model' in d:
print(f"ERROR: camera_model={d['camera_model']}, so no distortion_model should have been given, but we have distortion_model={d['distortion_model']}",
file=sys.stderr)
sys.exit(1)
intrinsics = np.array(d['intrinsics'])
if d['camera_model'] == 'cahvor':
if intrinsics.size != 6*3:
print(f"ERROR: camera_model='cahvor' should have {6*3} intrinsics values, but got {intrinsics.size}",
file=sys.stderr)
sys.exit(1)
C,A,H,V,O,R = intrinsics.reshape(6,3)
E = None
else:
# cahvore
if intrinsics.size != 7*3:
print(f"ERROR: camera_model='cahvor' should have {7*3} intrinsics values, but got {intrinsics.size}",
file=sys.stderr)
sys.exit(1)
C,A,H,V,O,R,E = intrinsics.reshape(7,3)
try:
m = mrcal.cahvor._construct_model(C,A,H,V,O,R,E,
is_cahvor_or_cahvore = True,
is_cahvore = E is not None,
cahvore_linearity = d.get('cahvore_linearity',0.37),
Dimensions = np.array(d['resolution'],dtype=np.int32),
name = name)
except Exception as e:
print(f"ERROR: couldn't parse cahvor(e) model: {e}",
file=sys.stderr)
sys.exit(1)
if not Rt_is_identity(m.extrinsics_Rt_fromref()):
print(f"ERROR: given cahvor(e) model has non-identity extrinsics. These may conflict with the extrinsics in the rest of the kalibr file",
file=sys.stderr)
sys.exit(1)
if not 'T_cn_cnm1' in d:
Rt_cam_camprev = mrcal.identity_Rt()
else:
T_cn_cnm1 = np.array(d['T_cn_cnm1'])
if T_cn_cnm1.shape != (4,4):
print(f"ERROR: T_cn_cnm1 must be a 4x4 array; got T_cn_cnm1.shape={T_cn_cnm1.shape}",
file=sys.stderr)
sys.exit()
if np.any(T_cn_cnm1[3,:] - np.array((0,0,0,1))):
print(f"ERROR: T_cn_cnm1 must have a last row of (0,0,0,1); got {T_cn_cnm1[3,:]}",
file=sys.stderr)
sys.exit()
# The documentation is here:
#
# https://github.com/ethz-asl/kalibr/wiki/Yaml-formats
#
# It says:
#
# T_cn_cnm1 camera extrinsic transformation, always with respect to the
# last camera in the chain (e.g. cam1: T_cn_cnm1 = T_c1_c0, takes cam0 to
# cam1 coordinates)
#
# This isn't clear. I assume "last" means "previous"
R_cam_camprev = T_cn_cnm1[:3,:3]
if nps.norm2((nps.matmult(R_cam_camprev, R_cam_camprev.T) - np.eye(3)).ravel()) > 1e-12:
print(f"ERROR: T_cn_cnm1[:3,:3] must be a valid rotation. Instead it is {T_cn_cnm1[:3,:3]}",
file=sys.stderr)
sys.exit(1)
t_cam_camprev = T_cn_cnm1[:3, 3]
Rt_cam_camprev = nps.glue(R_cam_camprev,t_cam_camprev, axis=-2)
if Rt_camprev_ref is not None:
Rt_cam_ref = mrcal.compose_Rt(Rt_cam_camprev, Rt_camprev_ref)
else:
# This is the first camera. What do we use as our reference?
if args.cam0_at_reference:
Rt_cam_ref = mrcal.identity_Rt()
else:
# By default the "prev cam" from cam0 is the reference
Rt_cam_ref = Rt_cam_camprev
Rt_camprev_ref = Rt_cam_ref # for the next one
m.extrinsics_Rt_fromref(Rt_cam_ref)
return m
def convert_all(f, which):
# which is None = "all cameras"
try:
D = yaml.safe_load(f)
except yaml.scanner.ScannerError as e:
print("Error parsing YAML:\n\n")
print(e)
sys.exit(1)
if len(D) == 0:
print("Zero models read from input",
file=sys.stderr)
sys.exit(1)
if 'camera_model' in D:
# one camera; stored inline
d = D
if which is not None:
print(f"Error: a single in-line model found in the file, but a specific camera is requested: '{which}'. Omit --camera",
file=sys.stderr)
sys.exit(1)
try:
return ( \
(None, convert_one(d,'camera')),
)
except KeyError as e:
print(f"Error parsing input; missing key: {e}", file=sys.stderr)
sys.exit(1)
if which is not None and \
not which in D:
print(f"Error: asked for camera '{which}', but the given file contains only these cameras: {tuple(D.keys())}",
file=sys.stderr)
sys.exit(1)
# The extrinsics are specified as a serial chain. So I need to parse all the
# cameras, even if I only need to save one
models = dict()
for name,d in D.items():
try:
models[name] = convert_one(d,name)
except KeyError as e:
print(f"Error parsing input; missing key: {e}", file=sys.stderr)
sys.exit(1)
if which is None:
# all cameras requested
return [ (name, models[name]) \
for name in models.keys() ]
return ( \
(which, models[which]),
)
if args.model == '-':
if sys.stdin.isatty():
# This isn't an error per-se. But most likely the user ran this
# without redirecting any data into it. Without this check the
# program will sit there, waiting for input. Which will look strange
# to an unsuspecting user
print("Trying to read a model from standard input, but no file is being redirected into it",
file=sys.stderr)
sys.exit(1)
names_models = convert_all(sys.stdin, which = args.camera)
if len(names_models) > 1:
print(f"Reading from standard input may only produce ONE model on stdout, but got more here. Pass --camera to select the camera you want; available cameras: {list(n for n,m in names_models)}",
file=sys.stderr)
sys.exit(1)
names_models[0][1].write(sys.stdout)
else:
base,extension = os.path.splitext(args.model)
if extension.lower() == '.cameramodel':
print("Input file is already in the cameramodel format (judging from the filename). Doing nothing",
file=sys.stderr)
sys.exit(0)
if args.outdir is not None:
base = args.outdir + '/' + os.path.split(base)[1]
with open(args.model, "r") as f:
names_models = convert_all(f, which = args.camera)
for name,model in names_models:
if name is None: name = ''
else: name = f"-{name}"
filename_out = f"{base}{name}.cameramodel"
if not args.force and os.path.exists(filename_out):
print(f"Target model '{filename_out}' already exists. Doing nothing with this model. Pass -f to overwrite",
file=sys.stderr)
continue
model.write(filename_out)
print("Wrote " + filename_out)