Debian Codegraph @

mrcal-show-splined-model-correction

raw
1 #!/usr/bin/env python3
2
3 # Copyright (c) 2017-2023 California Institute of Technology ("Caltech"). U.S.
4 # Government sponsorship acknowledged. All rights reserved.
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10
11 r'''Visualizes the surface represented in a splined lens model
12
13 SYNOPSIS
14
15 $ mrcal-show-splined-model-correction cam.cameramodel
16 ... a plot pops up showing the correction magnitude heatmap
17
18 Splined models are parametrized by flexible surfaces that define the projection
19 corrections (off some baseline model), and visualizing these corrections is
20 useful for understanding the projection behavior. Details of these models are
21 described in the documentation:
22
23 https://mrcal.secretsauce.net/lensmodels.html#splined-stereographic-lens-model
24
25 At this time LENSMODEL_SPLINED_STEREOGRAPHIC is the only splined model mrcal
26 has, so the baseline model is always LENSMODEL_STEREOGRAPHIC.
27
28 This tool produces a plot in the domain either of the input or the output of the
29 spline functions.
30
31 By default:
32 The plot is presented based on the spline index. With
33 LENSMODEL_SPLINED_STEREOGRAPHIC, this is the stereographic projection u.
34 This is the "forward" direction, what the projection operation actually
35 computes. In this view the knots form a regular grid, and the edge of the
36 imager forms a (possibly very irregular) curve
37
38 if --imager-domain:
39 The plot is presented based on the pixels in the imager. This is the
40 backward direction: the domain is the OUTPUT of the splined functions. In
41 this view the knot layout is (possibly highly) irregular. The edge of the
42 imager is a perfect rectangle.
43
44 Separate from the domain, the data can be presented in 3 different ways:
45
46 - Magnitude heatmap. This is the default. We plot mag(deltauxy). This displays
47 the deviation from the baseline model as a heat map.
48
49 - Individual heatmap. Selected by passing an "xy" argument. We plot deltaux or
50 deltauy, depending on the value of xy. This displays the value of one of the
51 two splined surfaces individually, as a heat map.
52
53 - Vector field. Selected by --vectorfield. Displays the correction (deltaux,
54 deltauy) as a vector field.
55
56 The splined surfaces are defined by control points we call "knots". These knots
57 are arranged in a fixed grid (defined by the model configuration) with the value
58 at each knot set in the intrinsics vector.
59
60 The configuration selects the control point density and the expected field of
61 view of the lens. If the fov_x_deg configuration value is too big, many of the
62 knots will lie well outside the visible area, and will not be used. This is
63 wasteful. If fov_x_deg is too small, then some parts of the imager will lie
64 outside of the spline-in-bounds region, resulting in less-flexible projection
65 behavior at the edges of the imager. So the field of view should roughly match
66 the actual lens+camera we're using, and we can evaluate that with this tool.
67 This tool displays the spline-in-bounds region together with the usable
68 projection region (either the valid-intrinsics region or the imager bounds).
69 Ideally, the spline-in-bounds region is slightly bigger than the usable
70 projection region.
71
72 The usable projection region visualized by this tool is controlled by
73 --show-imager-bounds. If omitted, we display the valid-intrinsics region. This
74 is recommended, but keep in mind that this region is smaller than the full
75 imager, so a fov_x_deg that aligns well for one calibration may be too small in
76 a subsequent calibration of the same lens. If the subsequent calibration has
77 better coverage, and thus a bigger valid-intrinsics region. If
78 --show-imager-bounds: we use the imager bounds instead. The issue here is that
79 the projection near the edges of the imager is usually poorly-defined because
80 usually there isn't a lot of calibration data there. This makes the projection
81 behavior at the imager edges unknowable. Consequently, plotting the projection
82 at the imager edges is usually too alarming or not alarming enough. Passing
83 --show-imager-bounds is thus recommended only if we have very good calibration
84 coverage at the edge of the imager.
85
86 '''
87
88
89 import sys
90 import argparse
91 import re
92 import os
93
94 def parse_args():
95
96 parser = \
97 argparse.ArgumentParser(description = __doc__,
98 formatter_class=argparse.RawDescriptionHelpFormatter)
99
100 parser.add_argument('--gridn',
101 type=int,
102 default = (60,40),
103 nargs = 2,
104 help='''The density of the plotted grid. By default we
105 use a 60x40 grid''')
106
107 parser.add_argument('--vectorfield',
108 action = 'store_true',
109 help='''Display the spline correction as a vector field.
110 if --vectorfield: the 'xy' argument MUST be omitted''')
111
112 parser.add_argument('--vectorscale',
113 type = float,
114 default = 1.0,
115 help='''If plotting a vector field, scale all the
116 vectors by this factor. Useful to improve legibility if
117 the vectors are too small to see''')
118
119 parser.add_argument('--title',
120 type=str,
121 default = None,
122 help='''Title string for the plot. Overrides the default
123 title. Exclusive with --extratitle''')
124 parser.add_argument('--extratitle',
125 type=str,
126 default = None,
127 help='''Additional string for the plot to append to the
128 default title. Exclusive with --title''')
129
130 parser.add_argument('--hardcopy',
131 type=str,
132 help='''Write the output to disk, instead of making an interactive plot''')
133 parser.add_argument('--terminal',
134 type=str,
135 help=r'''gnuplotlib terminal. The default is good almost always, so most people don't
136 need this option''')
137 parser.add_argument('--set',
138 type=str,
139 action='append',
140 help='''Extra 'set' directives to gnuplotlib. Can be given multiple times''')
141 parser.add_argument('--unset',
142 type=str,
143 action='append',
144 help='''Extra 'unset' directives to gnuplotlib. Can be given multiple times''')
145 parser.add_argument('--imager-domain',
146 action='store_true',
147 help='''By default, this produces a visualization in the domain of the spline-index
148 (normalized stereographic coordinates). Sometimes it's
149 more informative to look at the imager domain instead,
150 by passing this option''')
151 parser.add_argument('--show-imager-bounds',
152 action='store_true',
153 help='''By default we communicate the usable projection
154 region to the user by displaying the valid-intrinsics
155 region. This isn't available in all models. To fall back
156 on the boundary of the full imager, pass
157 --show-imager-bounds. In the usual case of incomplete
158 calibration-time coverage at the edges, this results in
159 a very unrealistic representation of reality. Leaving
160 this at the default is recommended''')
161 parser.add_argument('--observations',
162 action='store_true',
163 default=False,
164 help='''If given, I show where the chessboard corners were observed at calibration
165 time. This is useful to evaluate the reported unprojectable regions.''')
166 parser.add_argument('model',
167 type=str,
168 help='''Input camera model. If "-' is given, we read standard input''')
169
170 parser.add_argument('xy',
171 choices = ('x','y'),
172 nargs = '?',
173 help='''Optional 'x' or 'y': which surface we're looking
174 at. MUST be omitted if --vectorfield. If omitted and not
175 --vectorfield: we plot the magnitude of the
176 (deltaux,deltauy) corretion vector''')
177
178 args = parser.parse_args()
179
180 if args.title is not None and \
181 args.extratitle is not None:
182 print("--title and --extratitle are exclusive", file=sys.stderr)
183 sys.exit(1)
184
185 return args
186
187 args = parse_args()
188
189 # arg-parsing is done before the imports so that --help works without building
190 # stuff, so that I can generate the manpages and README
191
192
193
194
195 import numpy as np
196 import numpysane as nps
197
198 import mrcal
199
200
201
202 try:
203 model = mrcal.cameramodel(args.model)
204 except Exception as e:
205 print(f"Couldn't load camera model '{args.model}': {e}", file=sys.stderr)
206 sys.exit(1)
207 lensmodel = model.intrinsics()[0]
208
209 if not re.match('LENSMODEL_SPLINED', lensmodel):
210 print(f"This only makes sense with splined models. Input uses {lensmodel}",
211 file = sys.stderr)
212 sys.exit(1)
213
214 if not args.show_imager_bounds and \
215 model.valid_intrinsics_region() is None:
216 print("The given model has no valid-intrinsics region. Pass --show-imager-bounds",
217 file = sys.stderr)
218 sys.exit(1)
219
220 plotkwargs = {}
221 if args.set is not None:
222 plotkwargs['set'] = args.set
223 if args.unset is not None:
224 plotkwargs['unset'] = args.unset
225
226 if args.title is not None:
227 plotkwargs['title'] = args.title
228 if args.extratitle is not None:
229 plotkwargs['extratitle'] = args.extratitle
230
231 try:
232 plot = mrcal.show_splined_model_correction( model,
233 vectorfield = args.vectorfield,
234 xy = args.xy,
235 imager_domain = args.imager_domain,
236 gridn_width = args.gridn[0],
237 gridn_height = args.gridn[1],
238 vectorscale = args.vectorscale,
239 valid_intrinsics_region = not args.show_imager_bounds,
240 observations = args.observations,
241 hardcopy = args.hardcopy,
242 terminal = args.terminal,
243 **plotkwargs)
244 except Exception as e:
245 print(f"Error: {e}", file=sys.stderr)
246 sys.exit(1)
247
248 if args.hardcopy is None:
249 plot.wait()