Debian Codegraph @

mrcal-show-distortion-off-pinhole

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'''Visualize the behavior or a lens model
12
13 SYNOPSIS
14
15 $ mrcal-show-distortion-off-pinhole --vectorfield left.cameramodel
16
17 ... a plot pops up showing the vector field of the difference from a pinhole
18 projection
19
20 This tool is used to examine how a lens model behaves. Depending on the model,
21 the vectors could be very large or very small, and we can scale them by passing
22 '--vectorscale s'. By default we sample in a 60x40 grid, but this spacing can be
23 controlled by passing '--gridn w h'.
24
25 By default we render a heat map of the lens effects. We can also see the
26 vectorfield by passing in --vectorfield. Or we can see the radial distortion
27 curve by passing --radial
28
29 This tool treats a pinhole projection as a baseline, and visualizes deviations
30 from this baseline. So wide lenses will have a lot of reported "distortion".
31 Thus pinhole models aren't a good baseline, but pretty much every non-mrcal tool
32 in common use treats lenses as pinhole+distortion, and that makes this
33 visualization useful.
34
35 The plots produced with --radial show the magnitude of the radial distortion as
36 a function of the distance to the center. For opencv models the radial
37 distortion is plotted directly from the projection equations: these represent
38 radial distortion explicitly. For other models, we empirically sample the imager
39 from the optical center given by intrinsics[2:4] to the 4 corners and the 4
40 centers of the imager edges. Note that the splined models often shift the
41 projection center from intrinsics[2:4], causing unnatural-looking curves near
42 the center. This isn't a a bug: intrinsics[2:4] isn't really in the center,
43 which isn't a problem for anything except this visualization.
44
45 '''
46
47
48 import sys
49 import argparse
50 import re
51 import os
52
53 def parse_args():
54
55 parser = \
56 argparse.ArgumentParser(description = __doc__,
57 formatter_class=argparse.RawDescriptionHelpFormatter)
58
59 parser.add_argument('--gridn',
60 type=int,
61 default = (60,40),
62 nargs = 2,
63 help='''How densely we should sample the imager. By default we report a 60x40 grid''')
64
65 parser.add_argument('--vectorscale',
66 type=float,
67 default = 1.0,
68 help='''Scale the vectors by this factor. Default is 1.0 (report the truth), but this is often too small to see''')
69
70 parser.add_argument('--radial',
71 action='store_true',
72 help='''Show the radial distortion scale factor instead of a colormap/vectorfield''')
73 parser.add_argument('--vectorfield',
74 action = 'store_true',
75 default = False,
76 help='''Plot the diff as a vector field instead of as a heat map. The vector field
77 contains more information (magnitude AND direction), but
78 is less clear at a glance''')
79
80 parser.add_argument('--show-fisheye-projections',
81 action='store_true',
82 help='''Applies to radial plots only. if given, the
83 radial plots include the behavior of common fisheye
84 projections, in addition to the behavior of THIS lens''')
85 parser.add_argument('--cbmax',
86 type=float,
87 default=10,
88 help='''Maximum range of the colorbar''')
89
90 parser.add_argument('--title',
91 type=str,
92 default = None,
93 help='''Title string for the plot. Overrides the default
94 title. Exclusive with --extratitle''')
95 parser.add_argument('--extratitle',
96 type=str,
97 default = None,
98 help='''Additional string for the plot to append to the
99 default title. Exclusive with --title''')
100
101 parser.add_argument('--hardcopy',
102 type=str,
103 help='''Write the output to disk, instead of making an interactive plot''')
104 parser.add_argument('--terminal',
105 type=str,
106 help=r'''gnuplotlib terminal. The default is good almost always, so most people don't
107 need this option''')
108 parser.add_argument('--set',
109 type=str,
110 action='append',
111 help='''Extra 'set' directives to pass to gnuplotlib. May be given multiple
112 times''')
113 parser.add_argument('--unset',
114 type=str,
115 action='append',
116 help='''Extra 'unset' directives to pass to gnuplotlib. May be given multiple
117 times''')
118
119 parser.add_argument('model',
120 type=str,
121 help='''Input camera model. If "-' is given, we read standard input''')
122
123 return parser.parse_args()
124
125 args = parse_args()
126
127 if args.title is not None and \
128 args.extratitle is not None:
129 print("--title and --extratitle are exclusive", file=sys.stderr)
130 sys.exit(1)
131
132 # arg-parsing is done before the imports so that --help works without building
133 # stuff, so that I can generate the manpages and README
134
135
136
137
138 import numpy as np
139 import numpysane as nps
140
141 import mrcal
142
143
144
145
146
147 try:
148 model = mrcal.cameramodel(args.model)
149 except Exception as e:
150 print(f"Couldn't load camera model '{args.model}': {e}", file=sys.stderr)
151 sys.exit(1)
152
153 if args.radial and args.vectorfield:
154 sys.stderr.write("Usage error: at most one of --radial and --vectorfield can be given\n")
155 sys.exit(1)
156
157 plotkwargs = {}
158 if args.set is not None:
159 plotkwargs['set'] = args.set
160 if args.unset is not None:
161 plotkwargs['unset'] = args.unset
162
163 if args.title is not None:
164 plotkwargs['title'] = args.title
165 if args.extratitle is not None:
166 plotkwargs['extratitle'] = args.extratitle
167
168 if args.radial:
169 plot = \
170 mrcal.show_distortion_off_pinhole_radial(model,
171 show_fisheye_projections = args.show_fisheye_projections,
172 hardcopy = args.hardcopy,
173 terminal = args.terminal,
174 **plotkwargs)
175 else:
176 plot = mrcal.show_distortion_off_pinhole(model,
177 vectorfield = args.vectorfield,
178 vectorscale = args.vectorscale,
179 cbmax = args.cbmax,
180 gridn_width = args.gridn[0],
181 gridn_height = args.gridn[1],
182
183 hardcopy = args.hardcopy,
184 terminal = args.terminal,
185 **plotkwargs)
186
187 if args.hardcopy is None:
188 plot.wait()