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
|
#!/usr/bin/env python3
r'''Study the precision and accuracy of the various triangulation routines'''
import sys
import argparse
import re
import os
def parse_args():
parser = \
argparse.ArgumentParser(description = __doc__,
formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument('--Nsamples',
type=int,
default=100000,
help='''How many random samples to evaluate. 100000 by
default''')
group = parser.add_mutually_exclusive_group(required = True)
group.add_argument('--ellipses',
action='store_true',
help='''Display the ellipses and samples in the xy plane''')
group.add_argument('--ranges',
action='store_true',
help='''Display the distribution of the range''')
parser.add_argument('--samples',
action='store_true',
help='''If --ellipses, plot the samples ALSO. Usually
this doesn't clarify anything, so the default is to omit
them''')
parser.add_argument('--cache',
type=str,
choices=('read','write'),
help=f'''A cache file stores the recalibration results;
computing these can take a long time. This option allows
us to or write the cache instead of sampling. The cache
file is hardcoded to a cache file (in /tmp). By default,
we do neither: we don't read the cache (we sample
instead), and we do not write it to disk when we're
done. This option is useful for tests where we reprocess
the same scenario repeatedly''')
parser.add_argument('--observed-point',
type = float,
nargs = 3,
default = ( 5000., 300., 2000.),
help='''The camera0 coordinate of the observed point.
Default is ( 5000., 300., 2000.)''')
parser.add_argument('--title',
type=str,
default = None,
help='''Title string for the plot. Overrides the default
title. Exclusive with --extratitle''')
parser.add_argument('--extratitle',
type=str,
default = None,
help='''Additional string for the plot to append to the
default title. Exclusive with --title''')
parser.add_argument('--hardcopy',
type=str,
help='''Write the output to disk, instead of an interactive plot''')
parser.add_argument('--terminal',
type=str,
help=r'''gnuplotlib terminal. The default is good almost always, so most people don't
need this option''')
parser.add_argument('--set',
type=str,
action='append',
help='''Extra 'set' directives to gnuplotlib. Can be given multiple times''')
parser.add_argument('--unset',
type=str,
action='append',
help='''Extra 'unset' directives to gnuplotlib. Can be given multiple times''')
args = parser.parse_args()
if args.title is not None and \
args.extratitle is not None:
print("--title and --extratitle are exclusive", file=sys.stderr)
sys.exit(1)
return args
args = parse_args()
import numpy as np
import numpysane as nps
import gnuplotlib as gp
import pickle
import os.path
# I import the LOCAL mrcal
scriptdir = os.path.dirname(os.path.realpath(__file__))
sys.path[:0] = f"{scriptdir}/../..",
import mrcal
############ bias visualization
#
# I simulate pixel noise, and see what that does to the triangulation. Play with
# the geometric details to get a sense of how these behave
model0 = mrcal.cameramodel( intrinsics = ('LENSMODEL_PINHOLE',
np.array((1000., 1000., 500., 500.))),
imagersize = np.array((1000,1000)) )
model1 = mrcal.cameramodel( intrinsics = ('LENSMODEL_PINHOLE',
np.array((1100., 1100., 500., 500.))),
imagersize = np.array((1000,1000)) )
# square camera layout
t01 = np.array(( 1., 0.1, -0.2))
R01 = mrcal.R_from_r(np.array((0.001, -0.002, -0.003)))
Rt01 = nps.glue(R01, t01, axis=-2)
p = np.array(args.observed_point)
q0 = mrcal.project(p, *model0.intrinsics())
sigma = 0.1
cache_file = "/tmp/triangulation-study-cache.pickle"
if args.cache is None or args.cache == 'write':
v0local_noisy, v1local_noisy,v0_noisy,v1_noisy,_,_,_,_ = \
mrcal.synthetic_data. \
_noisy_observation_vectors_for_triangulation(p,Rt01,
model0.intrinsics(),
model1.intrinsics(),
args.Nsamples,
sigma = sigma)
p_sampled_geometric = mrcal.triangulate_geometric( v0_noisy, v1_noisy, t01 )
p_sampled_lindstrom = mrcal.triangulate_lindstrom( v0local_noisy, v1local_noisy, Rt01 )
p_sampled_leecivera_l1 = mrcal.triangulate_leecivera_l1( v0_noisy, v1_noisy, t01 )
p_sampled_leecivera_linf = mrcal.triangulate_leecivera_linf( v0_noisy, v1_noisy, t01 )
p_sampled_leecivera_mid2 = mrcal.triangulate_leecivera_mid2( v0_noisy, v1_noisy, t01 )
p_sampled_leecivera_wmid2 = mrcal.triangulate_leecivera_wmid2(v0_noisy, v1_noisy, t01 )
q0_sampled_geometric = mrcal.project(p_sampled_geometric, *model0.intrinsics())
q0_sampled_lindstrom = mrcal.project(p_sampled_lindstrom, *model0.intrinsics())
q0_sampled_leecivera_l1 = mrcal.project(p_sampled_leecivera_l1, *model0.intrinsics())
q0_sampled_leecivera_linf = mrcal.project(p_sampled_leecivera_linf, *model0.intrinsics())
q0_sampled_leecivera_mid2 = mrcal.project(p_sampled_leecivera_mid2, *model0.intrinsics())
q0_sampled_leecivera_wmid2 = mrcal.project(p_sampled_leecivera_wmid2, *model0.intrinsics())
range_sampled_geometric = nps.mag(p_sampled_geometric)
range_sampled_lindstrom = nps.mag(p_sampled_lindstrom)
range_sampled_leecivera_l1 = nps.mag(p_sampled_leecivera_l1)
range_sampled_leecivera_linf = nps.mag(p_sampled_leecivera_linf)
range_sampled_leecivera_mid2 = nps.mag(p_sampled_leecivera_mid2)
range_sampled_leecivera_wmid2 = nps.mag(p_sampled_leecivera_wmid2)
if args.cache is not None:
with open(cache_file,"wb") as f:
pickle.dump((v0local_noisy,
v1local_noisy,
v0_noisy,
v1_noisy,
p_sampled_geometric,
p_sampled_lindstrom,
p_sampled_leecivera_l1,
p_sampled_leecivera_linf,
p_sampled_leecivera_mid2,
p_sampled_leecivera_wmid2,
q0_sampled_geometric,
q0_sampled_lindstrom,
q0_sampled_leecivera_l1,
q0_sampled_leecivera_linf,
q0_sampled_leecivera_mid2,
q0_sampled_leecivera_wmid2,
range_sampled_geometric,
range_sampled_lindstrom,
range_sampled_leecivera_l1,
range_sampled_leecivera_linf,
range_sampled_leecivera_mid2,
range_sampled_leecivera_wmid2),
f)
print(f"Wrote cache to {cache_file}")
else:
with open(cache_file,"rb") as f:
(v0local_noisy,
v1local_noisy,
v0_noisy,
v1_noisy,
p_sampled_geometric,
p_sampled_lindstrom,
p_sampled_leecivera_l1,
p_sampled_leecivera_linf,
p_sampled_leecivera_mid2,
p_sampled_leecivera_wmid2,
q0_sampled_geometric,
q0_sampled_lindstrom,
q0_sampled_leecivera_l1,
q0_sampled_leecivera_linf,
q0_sampled_leecivera_mid2,
q0_sampled_leecivera_wmid2,
range_sampled_geometric,
range_sampled_lindstrom,
range_sampled_leecivera_l1,
range_sampled_leecivera_linf,
range_sampled_leecivera_mid2,
range_sampled_leecivera_wmid2) = \
pickle.load(f)
plot_options = {}
if args.set is not None:
plot_options['set'] = args.set
if args.unset is not None:
plot_options['unset'] = args.unset
if args.hardcopy is not None:
plot_options['hardcopy'] = args.hardcopy
if args.terminal is not None:
plot_options['terminal'] = args.terminal
if args.ellipses:
# Plot the reprojected pixels and the fitted ellipses
data_tuples = \
[ *mrcal.utils._plot_args_points_and_covariance_ellipse( q0_sampled_geometric, 'geometric' ),
*mrcal.utils._plot_args_points_and_covariance_ellipse( q0_sampled_lindstrom, 'lindstrom' ),
*mrcal.utils._plot_args_points_and_covariance_ellipse( q0_sampled_leecivera_l1, 'lee-civera-l1' ),
*mrcal.utils._plot_args_points_and_covariance_ellipse( q0_sampled_leecivera_linf, 'lee-civera-linf' ),
*mrcal.utils._plot_args_points_and_covariance_ellipse( q0_sampled_leecivera_mid2, 'lee-civera-mid2' ),
*mrcal.utils._plot_args_points_and_covariance_ellipse( q0_sampled_leecivera_wmid2,'lee-civera-wmid2' ), ]
if not args.samples:
# Not plotting samples. Get rid of all the "dots" I'm plotting
data_tuples = [ t for t in data_tuples if \
not (isinstance(t[-1], dict) and \
'_with' in t[-1] and \
t[-1]['_with'] == 'dots') ]
if args.title is not None:
title = args.title
else:
title = 'Reprojected triangulated point'
if args.extratitle is not None:
title += ': ' + args.extratitle
gp.plot( *data_tuples,
( q0,
dict(_with = 'points pt 3 ps 2',
tuplesize = -2,
legend = 'Ground truth')),
square = True,
wait = 'hardcopy' not in plot_options,
title = title,
**plot_options)
elif args.ranges:
# Plot the range distribution
range_ref = nps.mag(p)
if args.title is not None:
title = args.title
else:
title = "Range distribution"
if args.extratitle is not None:
title += ': ' + args.extratitle
gp.plot( nps.cat( range_sampled_geometric,
range_sampled_lindstrom,
range_sampled_leecivera_l1,
range_sampled_leecivera_linf,
range_sampled_leecivera_mid2,
range_sampled_leecivera_wmid2 ),
legend = np.array(( 'range_sampled_geometric',
'range_sampled_lindstrom',
'range_sampled_leecivera_l1',
'range_sampled_leecivera_linf',
'range_sampled_leecivera_mid2',
'range_sampled_leecivera_wmid2' )),
histogram=True,
binwidth=200,
_with='lines',
_set = f'arrow from {range_ref},graph 0 to {range_ref},graph 1 nohead lw 5',
wait = 'hardcopy' not in plot_options,
title = title,
**plot_options)
else:
raise Exception("Getting here is a bug")
|