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 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234
|
#!/usr/bin/env python3
r'''Simulates different chessboard dances to find the best technique
We want the shortest chessboard dances that produce the most confident results.
In a perfect world would put the chessboard in all the locations where we would
expect to use the visual system, since the best confidence is obtained in
regions where the chessboard was observed.
However, due to geometric constraints it is sometimes impossible to put the
board in the right locations. This tool clearly shows that filling the field of
view produces best results. But very wide lenses require huge chessboards,
displayed very close to the lens in order to fill the field of view. This means
that using a wide lens to look out to infinity will always result in potentially
too little projection confidence. This tool is intended to find the kind of
chessboard dance to get good confidences by simulating different geometries and
dances.
We arrange --Ncameras cameras horizontally, with an identity rotation, evenly
spaced with a spacing of --camera-spacing meters. The left camera is at the
origin.
We show the cameras lots of dense chessboards ("dense" meaning that every camera
sees all the points of all the chessboards). The chessboard come in two
clusters: "near" and "far". Each cluster is centered straight ahead of the
midpoint of all the cameras, with some random noise on the position and
orientation. The distances from the center of the cameras to the center of the
clusters are given by --range. This tool solves the calibration problem, and
generates uncertainty-vs-range curves. Each run of this tool generates a family
of this curve, for different values of Nframes-far, the numbers of chessboard
observations in the "far" cluster.
This tool scans some parameter (selected by --scan), and reports the
uncertainty-vs-range for the different values of this parameter (as a plot and
as an output vnl).
If we don't want to scan any parameter, and just want a single
uncertainty-vs-range plot, don't pass --scan.
'''
import sys
import argparse
import re
import os
def parse_args():
def positive_float(string):
try:
value = float(string)
except:
raise argparse.ArgumentTypeError("argument MUST be a positive floating-point number. Got '{}'".format(string))
if value <= 0:
raise argparse.ArgumentTypeError("argument MUST be a positive floating-point number. Got '{}'".format(string))
return value
def positive_int(string):
try:
value = int(string)
except:
raise argparse.ArgumentTypeError("argument MUST be a positive integer. Got '{}'".format(string))
if value <= 0 or abs(value-float(string)) > 1e-6:
raise argparse.ArgumentTypeError("argument MUST be a positive integer. Got '{}'".format(string))
return value
parser = \
argparse.ArgumentParser(description = __doc__,
formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument('--icam-uncertainty',
default = 0,
type=int,
help='''Which camera to use for the uncertainty reporting. I use the left-most one
(camera 0) by default''')
parser.add_argument('--camera-spacing',
default = 0.3,
type=float,
help='How many meters between adjacent cameras in our synthetic world')
parser.add_argument('--object-spacing',
default="0.077",
type=str,
help='''Width of each square in the calibration board, in meters. If --scan
object_width_n, this is used as the spacing for the
SMALLEST width. As I change object_width_n, I adjust
object_spacing accordingly, to keep the total board size
constant. If --scan object_spacing, this is MIN,MAX to
specify the bounds of the scan. In that case
object_width_n stays constant, so the board changes
size''')
parser.add_argument('--object-width-n',
type=str,
help='''How many points the calibration board has per horizontal side. If both are
omitted, we default the width and height to 10 (both
must be specified to use a non-default value). If --scan
object_width_n, this is MIN,MAX to specify the bounds of
the scan. In that case I assume a square object, and
ignore --object-height-n. Scanning object-width-n keeps
the board size constant''')
parser.add_argument('--object-height-n',
type=int,
help='''How many points the calibration board has per vertical side. If both are
omitted, we default the width and height to 10 (both
must be specified to use a non-default value). If --scan
object_width_n, this is ignored, and set equivalent to
the object width''')
parser.add_argument('--observed-pixel-uncertainty',
type=positive_float,
default = 1.0,
help='''The standard deviation of x and y pixel coordinates of the input observations
I generate. The distribution of the inputs is gaussian,
with the standard deviation specified by this argument.
Note: this is the x and y standard deviation, treated
independently. If each of these is s, then the LENGTH of
the deviation of each pixel is a Rayleigh distribution
with expected value s*sqrt(pi/2) ~ s*1.25''')
parser.add_argument('--lensmodel',
required=False,
type=str,
help='''Which lens model to use for the simulation. If omitted, we use the model
given on the commandline. We may want to use a
parametric model to generate data (model on the
commandline), but a richer splined model to solve''')
parser.add_argument('--skip-calobject-warp-solve',
action='store_true',
default=False,
help='''By default we assume the calibration target is
slightly deformed, and we compute this deformation. If
we want to assume that the chessboard shape is fixed,
pass this option. The actual shape of the board is given
by --calobject-warp''')
parser.add_argument('--calobject-warp',
type=float,
nargs=2,
default=(0.002, -0.005),
help='''The "calibration-object warp". These specify the
flex of the chessboard. By default, the board is
slightly warped (as is usually the case in real life).
To use a perfectly flat board, specify "0 0" here''')
parser.add_argument('--show-geometry-first-solve',
action = 'store_true',
help='''If given, display the camera, chessboard geometry after the first solve, and
exit. Used for debugging''')
parser.add_argument('--show-uncertainty-first-solve',
action = 'store_true',
help='''If given, display the uncertainty (at infinity) and observations after the
first solve, and exit. Used for debugging''')
parser.add_argument('--write-models-first-solve',
action = 'store_true',
help='''If given, write the solved models to disk after the first solve, and exit.
Used for debugging. Useful to check fov_x_deg when solving for a splined model''')
parser.add_argument('--which',
choices=('all-cameras-must-see-full-board',
'some-cameras-must-see-full-board',
'all-cameras-must-see-half-board',
'some-cameras-must-see-half-board'),
default='all-cameras-must-see-half-board',
help='''What kind of random poses are accepted. Default
is all-cameras-must-see-half-board''')
parser.add_argument('--fixed-frames',
action='store_true',
help='''Optimize the geometry keeping the chessboard frames fixed. This reduces the
freedom of the solution, and produces more confident
calibrations. It's possible to use this in reality by
surverying the chessboard poses''')
parser.add_argument('--method',
choices=('mean-pcam', 'bestq', 'cross-reprojection-rrp-Jfp'),
default='mean-pcam',
help='''Multiple uncertainty quantification methods are available. We default to 'mean-pcam' ''')
parser.add_argument('--ymax',
type=float,
default = 10.0,
help='''If given, use this as the upper extent of the uncertainty plot.''')
parser.add_argument('--uncertainty-at-range-sampled-min',
type=float,
help='''If given, use this as the lower bound of the ranges we look at when
evaluating projection confidence''')
parser.add_argument('--uncertainty-at-range-sampled-max',
type=float,
help='''If given, use this as the upper bound of the ranges we look at when
evaluating projection confidence''')
parser.add_argument('--explore',
action='store_true',
help='''Drop into a REPL at the end''')
parser.add_argument('--scan',
type=str,
default='',
choices=('range',
'tilt_radius',
'Nframes',
'Ncameras',
'object_width_n',
'object_spacing',
'num_far_constant_Nframes_near',
'num_far_constant_Nframes_all'),
help='''Study the effect of some parameter on uncertainty. The parameter is given as
an argument ot this function. Valid choices:
('range','tilt_radius','Nframes','Ncameras',
'object_width_n', 'object_spacing', 'num_far_constant_Nframes_near',
'num_far_constant_Nframes_all'). Scanning object-width-n
keeps the board size constant''')
parser.add_argument('--scan-object-spacing-compensate-range-from',
type=float,
help=f'''Only applies if --scan object-spacing. By
default we vary the object spacings without touching
anything else: the chessboard grows in size as we
increase the spacing. If given, we try to keep the
apparent size constant: as the object spacing grows, we
increase the range. The nominal range given in this
argument''')
parser.add_argument('--range',
default = '0.5,4.0',
type=str,
help='''if --scan num_far_...: this is "NEAR,FAR"; specifying the near and far ranges
to the chessboard to evaluate. if --scan range this is
"MIN,MAX"; specifying the extents of the ranges to
evaluate. Otherwise: this is RANGE, the one range of
chessboards to evaluate''')
parser.add_argument('--tilt-radius',
default='30.',
type=str,
help='''The radius of the uniform distribution used to
sample the pitch and yaw of the chessboard
observations, in degrees. The default is 30, meaning
that the chessboard pitch and yaw are sampled from
[-30deg,30deg]. if --scan tilt_radius: this is
TILT-MIN,TILT-MAX specifying the bounds of the two
tilt-radius values to evaluate. Otherwise: this is the
one value we use''')
parser.add_argument('--roll-radius',
type=float,
default=20.,
help='''The synthetic chessboard orientation is sampled
from a uniform distribution: [-RADIUS,RADIUS]. The
pitch,yaw radius is specified by the --tilt-radius. The
roll radius is selected here. "Roll" is the rotation
around the axis normal to the chessboard plane. Default
is 20deg''')
parser.add_argument('--x-radius',
type=float,
default=None,
help='''The synthetic chessboard position is sampled
from a uniform distribution: [-RADIUS,RADIUS]. The x
radius is selected here. "x" is direction in the
chessboard plane, and is also the axis along which the
cameras are distributed. A resonable default (possibly
range-dependent) is chosen if omitted. MUST be None if
--scan num_far_...''')
parser.add_argument('--y-radius',
type=float,
default=None,
help='''The synthetic chessboard position is sampled
from a uniform distribution: [-RADIUS,RADIUS]. The y
radius is selected here. "y" is direction in the
chessboard plane, and is also normal to the axis along
which the cameras are distributed. A resonable default
(possibly range-dependent) is chosen if omitted. MUST be
None if --scan num_far_...''')
parser.add_argument('--z-radius',
type=float,
default=None,
help='''The synthetic chessboard position is sampled
from a uniform distribution: [-RADIUS,RADIUS]. The z
radius is selected here. "z" is direction normal to the
chessboard plane. A resonable default (possibly
range-dependent) is chosen if omitted. MUST be None if
--scan num_far_...''')
parser.add_argument('--Ncameras',
default = '1',
type=str,
help='''How many cameras oberve our synthetic world. By default we just have one
camera. if --scan Ncameras: this is
NCAMERAS-MIN,NCAMERAS-MAX specifying the bounds of the
camera counts to evaluate. Otherwise: this is the one
Ncameras value to use''')
parser.add_argument('--Nframes',
type=str,
help='''How many observed frames we have. Ignored if --scan num_far_... if
--scan Nframes: this is NFRAMES-MIN,NFRAMES-MAX specifying the bounds of the
frame counts to evaluate. Otherwise: this is the one Nframes value to use''')
parser.add_argument('--Nframes-near',
type=positive_int,
help='''Used if --scan num_far_constant_Nframes_near. The number of "near" frames is
given by this argument, while we look at the effect of
adding more "far" frames.''')
parser.add_argument('--Nframes-all',
type=positive_int,
help='''Used if --scan num_far_constant_Nframes_all. The number of "near"+"far"
frames is given by this argument, while we look at the
effect of replacing "near" frames with "far" frames.''')
parser.add_argument('--Nscan-samples',
type=positive_int,
default=8,
help='''How many values of the parameter being scanned to evaluate. If we're scanning
something (--scan ... given) then by default the scan
evaluates 8 different values. Otherwise this is set to 1''')
parser.add_argument('--hardcopy',
help=f'''Filename to plot into. If omitted, we make an interactive plot. This is
passed directly to gnuplotlib''')
parser.add_argument('--terminal',
help=f'''gnuplot terminal to use for the plots. This is passed directly to
gnuplotlib. Omit this unless you know what you're doing''')
parser.add_argument('--extratitle',
help=f'''Extra title string to add to a plot''')
parser.add_argument('--title',
help=f'''Full title string to use in a plot. Overrides
the default and --extratitle''')
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''')
parser.add_argument('model',
type = str,
help='''Baseline camera model. I use the intrinsics from this model to generate
synthetic data. We probably want the "true" model to not
be too crazy, so this should probably by a parametric
(not splined) model''')
args = parser.parse_args()
if args.title is not None and args.extratitle is not None:
print("--title and --extratitle are mutually exclusive", file=sys.stderr)
sys.exit(1)
return args
args = parse_args()
if args.object_width_n is None and \
args.object_height_n is None:
args.object_width_n = "10"
args.object_height_n = 10
elif not ( args.object_width_n is not None and \
args.object_height_n is not None) and \
args.scan != 'object_width_n':
raise Exception("Either --object-width-n or --object-height-n are given: you must pass both or neither")
# arg-parsing is done before the imports so that --help works without building
# stuff, so that I can generate the manpages and README
import numpy as np
import numpysane as nps
import gnuplotlib as gp
import copy
import os.path
# I import the LOCAL mrcal
scriptdir = os.path.dirname(os.path.realpath(__file__))
sys.path[:0] = f"{scriptdir}/../..",
import mrcal
def split_list(s, t):
r'''Splits a comma-separated list
None -> None
"A,B,C" -> (A,B,C)
"A" -> A
'''
if s is None: return None,0
l = [t(x) for x in s.split(',')]
if len(l) == 1: return l[0],1
return l,len(l)
def first(s):
r'''first in a list, or the value if a scalar'''
if hasattr(s, '__iter__'): return s[0]
return s
def last(s):
r'''last in a list, or the value if a scalar'''
if hasattr(s, '__iter__'): return s[-1]
return s
controllable_args = \
dict( tilt_radius = dict(_type = float),
range = dict(_type = float),
Ncameras = dict(_type = int),
Nframes = dict(_type = int),
object_spacing = dict(_type = float),
object_width_n = dict(_type = int) )
for a in controllable_args.keys():
l,n = split_list(getattr(args, a), controllable_args[a]['_type'])
controllable_args[a]['value'] = l
controllable_args[a]['listlen'] = n
if any( controllable_args[a]['listlen'] > 2 for a in controllable_args.keys() ):
raise Exception(f"All controllable args must have either at most 2 values. {a} has {controllable_args[a]['listlen']}")
controllable_arg_0values = [ a for a in controllable_args.keys() if controllable_args[a]['listlen'] == 0 ]
controllable_arg_2values = [ a for a in controllable_args.keys() if controllable_args[a]['listlen'] == 2 ]
if len(controllable_arg_2values) == 0: controllable_arg_2values = ''
elif len(controllable_arg_2values) == 1: controllable_arg_2values = controllable_arg_2values[0]
else: raise Exception(f"At most 1 controllable arg may have 2 values. Instead I saw: {controllable_arg_2values}")
if re.match("num_far_constant_Nframes_", args.scan):
if args.x_radius is not None or \
args.y_radius is not None or \
args.z_radius is not None:
raise Exception("--x-radius and --y-radius and --z-radius are exclusive with --scan num_far_...")
# special case
if 'Nframes' not in controllable_arg_0values:
raise Exception(f"I'm scanning '{args.scan}', so --Nframes must not have been given")
if 'range' != controllable_arg_2values:
raise Exception(f"I'm scanning '{args.scan}', so --range must have 2 values")
if args.scan == "num_far_constant_Nframes_near":
if args.Nframes_all is not None:
raise Exception(f"I'm scanning '{args.scan}', so --Nframes-all must not have have been given")
if args.Nframes_near is None:
raise Exception(f"I'm scanning '{args.scan}', so --Nframes-near must have have been given")
else:
if args.Nframes_near is not None:
raise Exception(f"I'm scanning '{args.scan}', so --Nframes-near must not have have been given")
if args.Nframes_all is None:
raise Exception(f"I'm scanning '{args.scan}', so --Nframes-all must have have been given")
else:
if args.scan != controllable_arg_2values:
# This covers the scanning-nothing-no2value-anything case
raise Exception(f"I'm scanning '{args.scan}', the arg given 2 values is '{controllable_arg_2values}'. They must match")
if len(controllable_arg_0values):
raise Exception(f"I'm scanning '{args.scan}', so all controllable args should have some value. Missing: '{controllable_arg_0values}")
if args.scan == '':
args.Nscan_samples = 1
Nuncertainty_at_range_samples = 80
uncertainty_at_range_sampled_min = args.uncertainty_at_range_sampled_min
if uncertainty_at_range_sampled_min is None:
uncertainty_at_range_sampled_min = first(controllable_args['range']['value'])/10.
uncertainty_at_range_sampled_max = args.uncertainty_at_range_sampled_max
if uncertainty_at_range_sampled_max is None:
uncertainty_at_range_sampled_max = last(controllable_args['range']['value']) *10.
uncertainty_at_range_samples = \
np.logspace( np.log10(uncertainty_at_range_sampled_min),
np.log10(uncertainty_at_range_sampled_max),
Nuncertainty_at_range_samples)
# I want the RNG to be deterministic
np.random.seed(0)
model_intrinsics = mrcal.cameramodel(args.model)
calobject_warp_true_ref = np.array(args.calobject_warp)
def solve(Ncameras,
Nframes_near, Nframes_far,
object_spacing,
models_true,
# q.shape = (Nframes, Ncameras, object_height, object_width, 2)
# Rt_ref_board.shape = (Nframes, 4,3)
q_true_near, Rt_ref_board_true_near,
q_true_far, Rt_ref_board_true_far,
fixed_frames = args.fixed_frames):
q_true_near = q_true_near [:Nframes_near]
Rt_ref_board_true_near = Rt_ref_board_true_near[:Nframes_near]
if q_true_far is not None:
q_true_far = q_true_far [:Nframes_far ]
Rt_ref_board_true_far = Rt_ref_board_true_far [:Nframes_far ]
else:
q_true_far = np.zeros( (0,) + q_true_near.shape[1:],
dtype = q_true_near.dtype)
Rt_ref_board_true_far = np.zeros( (0,) + Rt_ref_board_true_near.shape[1:],
dtype = Rt_ref_board_true_near.dtype)
calobject_warp_true = calobject_warp_true_ref.copy()
Nframes_all = Nframes_near + Nframes_far
Rt_ref_board_true = nps.glue( Rt_ref_board_true_near,
Rt_ref_board_true_far,
axis = -3 )
# Dense observations. All the cameras see all the boards
indices_frame_camera = np.zeros( (Nframes_all*Ncameras, 2), dtype=np.int32)
indices_frame = indices_frame_camera[:,0].reshape(Nframes_all,Ncameras)
indices_frame.setfield(nps.outer(np.arange(Nframes_all, dtype=np.int32),
np.ones((Ncameras,), dtype=np.int32)),
dtype = np.int32)
indices_camera = indices_frame_camera[:,1].reshape(Nframes_all,Ncameras)
indices_camera.setfield(nps.outer(np.ones((Nframes_all,), dtype=np.int32),
np.arange(Ncameras, dtype=np.int32)),
dtype = np.int32)
indices_frame_camintrinsics_camextrinsics = \
nps.glue(indices_frame_camera,
indices_frame_camera[:,(1,)],
axis=-1)
# If not fixed_frames: we use camera0 as the reference cordinate system, and
# we allow the chessboard poses to move around. Else: the reference
# coordinate system is arbitrary, but all cameras are allowed to move
# around. The chessboards poses are fixed
if not fixed_frames:
indices_frame_camintrinsics_camextrinsics[:,2] -= 1
q = nps.glue( q_true_near,
q_true_far,
axis = -5 )
# apply noise
q += np.random.randn(*q.shape) * args.observed_pixel_uncertainty
# The observations are dense (in the data every camera sees all the
# chessboards), but some of the observations WILL be out of bounds. I
# pre-mark those as outliers so that the solve doesn't do weird stuff
# Set the weights to 1 initially
# shape (Nframes, Ncameras, object_height_n, object_width_n, 3)
observations = nps.glue(q,
np.ones( q.shape[:-1] + (1,) ),
axis = -1)
# shape (Ncameras, 1, 1, 2)
imagersizes = nps.mv( nps.cat(*[ m.imagersize() for m in models_true ]),
-2, -4 )
# mark the out-of-view observations as outliers
observations[ np.any( q < 0, axis=-1 ), 2 ] = -1.
observations[ np.any( q-imagersizes >= 0, axis=-1 ), 2 ] = -1.
# shape (Nobservations, Nh, Nw, 2)
observations = nps.clump( observations,
n = 2 )
intrinsics = nps.cat( *[m.intrinsics()[1] for m in models_true] )
# If not fixed_frames: we use camera0 as the reference cordinate system, and
# we allow the chessboard poses to move around. Else: the reference
# coordinate system is arbitrary, but all cameras are allowed to move
# around. The chessboards poses are fixed
if fixed_frames:
extrinsics = nps.cat( *[m.rt_cam_ref() for m in models_true] )
else:
extrinsics = nps.cat( *[m.rt_cam_ref() for m in models_true[1:]] )
if len(extrinsics) == 0: extrinsics = None
if nps.norm2(models_true[0].rt_cam_ref()) > 1e-6:
raise Exception("models_true[0] must sit at the origin")
imagersizes = nps.cat( *[m.imagersize() for m in models_true] )
optimization_inputs = \
dict( # intrinsics filled in later
rt_cam_ref = extrinsics,
rt_ref_frame = mrcal.rt_from_Rt(Rt_ref_board_true),
points = None,
observations_board = observations,
indices_frame_camintrinsics_camextrinsics = indices_frame_camintrinsics_camextrinsics,
observations_point = None,
indices_point_camintrinsics_camextrinsics = None,
# lensmodel filled in later
calobject_warp = copy.deepcopy(calobject_warp_true),
imagersizes = imagersizes,
calibration_object_spacing = object_spacing,
verbose = False,
# do_optimize_extrinsics filled in later
# do_optimize_intrinsics_core filled in later
do_optimize_frames = False,
do_optimize_intrinsics_distortions = True,
do_optimize_calobject_warp = False, # turn this on, and reoptimize later, if needed
do_apply_regularization = True,
do_apply_outlier_rejection = False)
if args.lensmodel is None:
lensmodel = model_intrinsics.intrinsics()[0]
else:
lensmodel = args.lensmodel
Nintrinsics = mrcal.lensmodel_num_params(lensmodel)
if re.search("SPLINED", lensmodel):
# These are already mostly right, So I lock them down while I seed the
# intrinsics
optimization_inputs['do_optimize_extrinsics'] = False
# I pre-optimize the core, and then lock it down
optimization_inputs['lensmodel'] = 'LENSMODEL_STEREOGRAPHIC'
optimization_inputs['intrinsics'] = intrinsics[:,:4].copy()
optimization_inputs['do_optimize_intrinsics_core'] = True
stats = mrcal.optimize(**optimization_inputs)
print(f"## optimized. rms = {stats['rms_reproj_error__pixels']}", file=sys.stderr)
# core is good. Lock that down, and get an estimate for the control
# points
optimization_inputs['do_optimize_intrinsics_core'] = False
optimization_inputs['lensmodel'] = lensmodel
optimization_inputs['intrinsics'] = nps.glue(optimization_inputs['intrinsics'],
np.zeros((Ncameras,Nintrinsics-4),),axis=-1)
stats = mrcal.optimize(**optimization_inputs)
print(f"## optimized. rms = {stats['rms_reproj_error__pixels']}", file=sys.stderr)
# Ready for a final reoptimization with the geometry
optimization_inputs['do_optimize_extrinsics'] = True
if not fixed_frames:
optimization_inputs['do_optimize_frames'] = True
else:
optimization_inputs['lensmodel'] = lensmodel
if not mrcal.lensmodel_metadata_and_config(lensmodel)['has_core'] or \
not mrcal.lensmodel_metadata_and_config(model_intrinsics.intrinsics()[0])['has_core']:
raise Exception("I'm assuming all the models here have a core. It's just lazy coding. If you see this, feel free to fix.")
if lensmodel == model_intrinsics.intrinsics()[0]:
# Same model. Grab the intrinsics. They're 99% right
optimization_inputs['intrinsics'] = intrinsics.copy()
else:
# Different model. Grab the intrinsics core, optimize the rest
optimization_inputs['intrinsics'] = nps.glue(intrinsics[:,:4],
np.zeros((Ncameras,Nintrinsics-4),),axis=-1)
optimization_inputs['do_optimize_intrinsics_core'] = True
optimization_inputs['do_optimize_extrinsics'] = True
if not fixed_frames:
optimization_inputs['do_optimize_frames'] = True
stats = mrcal.optimize(**optimization_inputs)
print(f"## optimized. rms = {stats['rms_reproj_error__pixels']}", file=sys.stderr)
if not args.skip_calobject_warp_solve:
optimization_inputs['do_optimize_calobject_warp'] = True
stats = mrcal.optimize(**optimization_inputs)
print(f"## optimized. rms = {stats['rms_reproj_error__pixels']}", file=sys.stderr)
return optimization_inputs
def observation_centroid(optimization_inputs, icam):
r'''mean pixel coordinate of all non-outlier points seen by a given camera'''
ifcice = optimization_inputs['indices_frame_camintrinsics_camextrinsics']
observations = optimization_inputs['observations_board']
# pick the camera I want
observations = observations[ifcice[:,1] == icam]
# ignore outliers
q = observations[ (observations[...,2] > 0), :2]
return np.mean(q, axis=-2)
def eval_one_rangenear_tilt(models_true,
range_near, range_far, tilt_radius,
object_width_n, object_height_n, object_spacing,
uncertainty_at_range_samples,
Ncameras,
Nframes_near_samples, Nframes_far_samples):
# I want the RNG to be deterministic
np.random.seed(0)
uncertainties = np.zeros((len(Nframes_far_samples),
len(uncertainty_at_range_samples)),
dtype=float)
radius_cameras = (args.camera_spacing * (Ncameras-1)) / 2.
x_radius = args.x_radius if args.x_radius is not None else range_near*2. + radius_cameras
y_radius = args.y_radius if args.y_radius is not None else range_near*2.
z_radius = args.z_radius if args.z_radius is not None else range_near/10.
# shapes (Nframes, Ncameras, Nh, Nw, 2),
# (Nframes, 4,3)
q_true_near, Rt_ref_board_true_near = \
mrcal.synthesize_board_observations(models_true,
object_width_n = object_width_n,
object_height_n = object_height_n,
object_spacing = object_spacing,
calobject_warp = calobject_warp_true_ref,
rt_ref_boardcenter = np.array((0., 0., 0., radius_cameras, 0, range_near,)),
rt_ref_boardcenter__noiseradius = \
np.array((np.pi/180. * tilt_radius,
np.pi/180. * tilt_radius,
np.pi/180. * args.roll_radius,
x_radius, y_radius, z_radius)),
Nframes = np.max(Nframes_near_samples),
which = args.which)
if range_far is not None:
q_true_far, Rt_ref_board_true_far = \
mrcal.synthesize_board_observations(models_true,
object_width_n = object_width_n,
object_height_n = object_height_n,
object_spacing = object_spacing,
calobject_warp = calobject_warp_true_ref,
rt_ref_boardcenter = np.array((0., 0., 0., radius_cameras, 0, range_far,)),
rt_ref_boardcenter__noiseradius = \
np.array((np.pi/180. * tilt_radius,
np.pi/180. * tilt_radius,
np.pi/180. * args.roll_radius,
range_far*2. + radius_cameras,
range_far*2.,
range_far/10.)),
Nframes = np.max(Nframes_far_samples),
which = args.which)
else:
q_true_far = None
Rt_ref_board_true_far = None
for i_Nframes_far in range(len(Nframes_far_samples)):
Nframes_far = Nframes_far_samples [i_Nframes_far]
Nframes_near = Nframes_near_samples[i_Nframes_far]
optimization_inputs = solve(Ncameras,
Nframes_near, Nframes_far,
object_spacing,
models_true,
q_true_near, Rt_ref_board_true_near,
q_true_far, Rt_ref_board_true_far)
models_out = \
[ mrcal.cameramodel( optimization_inputs = optimization_inputs,
icam_intrinsics = icam ) \
for icam in range(Ncameras) ]
model = models_out[args.icam_uncertainty]
if args.show_geometry_first_solve:
mrcal.show_geometry(models_out,
show_calobjects = True,
wait = True)
sys.exit()
if args.show_uncertainty_first_solve:
mrcal.show_projection_uncertainty(model,
method = args.method,
observations= True,
wait = True)
sys.exit()
if args.write_models_first_solve:
for i in range(len(models_out)):
f = f"/tmp/camera{i}.cameramodel"
if os.path.exists(f):
input(f"File {f} already exists, and I want to overwrite it. Press enter to overwrite. Ctrl-c to exit")
models_out[i].write(f)
print(f"Wrote {f}")
sys.exit()
# shape (N,3)
# I sample the center of the imager
pcam_samples = \
mrcal.unproject( observation_centroid(optimization_inputs,
args.icam_uncertainty),
*model.intrinsics(),
normalize = True) * \
nps.dummy(uncertainty_at_range_samples, -1)
uncertainties[i_Nframes_far] = \
mrcal.projection_uncertainty(pcam_samples,
model,
method = args.method,
what = 'worstdirection-stdev')
return uncertainties
output_table_legend = 'range_uncertainty_sample Nframes_near Nframes_far Ncameras range_near range_far tilt_radius object_width_n object_spacing uncertainty'
output_table_fmt = '%f %d %d %d %f %f %f %d %f %f'
output_table_icol__range_uncertainty_sample = 0
output_table_icol__Nframes_near = 1
output_table_icol__Nframes_far = 2
output_table_icol__Ncameras = 3
output_table_icol__range_near = 4
output_table_icol__range_far = 5
output_table_icol__tilt_radius = 6
output_table_icol__object_width_n = 7
output_table_icol__object_spacing = 8
output_table_icol__uncertainty = 9
output_table_Ncols = 10
output_table = np.zeros( (args.Nscan_samples, Nuncertainty_at_range_samples, output_table_Ncols), dtype=float)
output_table[:,:, output_table_icol__range_uncertainty_sample] += uncertainty_at_range_samples
if re.match("num_far_constant_Nframes_", args.scan):
Nfar_samples = args.Nscan_samples
if args.scan == "num_far_constant_Nframes_near":
Nframes_far_samples = np.linspace(0,
args.Nframes_near//4,
Nfar_samples, dtype=int)
Nframes_near_samples = Nframes_far_samples*0 + args.Nframes_near
else:
Nframes_far_samples = np.linspace(0,
args.Nframes_all,
Nfar_samples, dtype=int)
Nframes_near_samples = args.Nframes_all - Nframes_far_samples
models_true = \
[ mrcal.cameramodel(intrinsics = model_intrinsics.intrinsics(),
imagersize = model_intrinsics.imagersize(),
rt_ref_cam = np.array((0,0,0,
i*args.camera_spacing,
0,0), dtype=float) ) \
for i in range(controllable_args['Ncameras']['value']) ]
# shape (args.Nscan_samples, Nuncertainty_at_range_samples)
output_table[:,:, output_table_icol__uncertainty] = \
eval_one_rangenear_tilt(models_true,
*controllable_args['range']['value'],
controllable_args['tilt_radius']['value'],
controllable_args['object_width_n']['value'],
args.object_height_n,
controllable_args['object_spacing']['value'],
uncertainty_at_range_samples,
controllable_args['Ncameras']['value'],
Nframes_near_samples, Nframes_far_samples)
output_table[:,:, output_table_icol__Nframes_near] += nps.transpose(Nframes_near_samples)
output_table[:,:, output_table_icol__Nframes_far] += nps.transpose(Nframes_far_samples)
output_table[:,:, output_table_icol__Ncameras] = controllable_args['Ncameras']['value']
output_table[:,:, output_table_icol__range_near] = controllable_args['range']['value'][0]
output_table[:,:, output_table_icol__range_far] = controllable_args['range']['value'][1]
output_table[:,:, output_table_icol__tilt_radius ] = controllable_args['tilt_radius']['value']
output_table[:,:, output_table_icol__object_width_n ] = controllable_args['object_width_n']['value']
output_table[:,:, output_table_icol__object_spacing ] = controllable_args['object_spacing']['value']
samples = Nframes_far_samples
elif args.scan == "range":
Nframes_near_samples = np.array( (controllable_args['Nframes']['value'],), dtype=int)
Nframes_far_samples = np.array( (0,), dtype=int)
models_true = \
[ mrcal.cameramodel(intrinsics = model_intrinsics.intrinsics(),
imagersize = model_intrinsics.imagersize(),
rt_ref_cam = np.array((0,0,0,
i*args.camera_spacing,
0,0), dtype=float) ) \
for i in range(controllable_args['Ncameras']['value']) ]
Nrange_samples = args.Nscan_samples
range_samples = np.linspace(*controllable_args['range']['value'],
Nrange_samples, dtype=float)
for i_range in range(Nrange_samples):
output_table[i_range,:, output_table_icol__uncertainty] = \
eval_one_rangenear_tilt(models_true,
range_samples[i_range], None,
controllable_args['tilt_radius']['value'],
controllable_args['object_width_n']['value'],
args.object_height_n,
controllable_args['object_spacing']['value'],
uncertainty_at_range_samples,
controllable_args['Ncameras']['value'],
Nframes_near_samples, Nframes_far_samples)[0]
output_table[:,:, output_table_icol__Nframes_near] = controllable_args['Nframes']['value']
output_table[:,:, output_table_icol__Nframes_far] = 0
output_table[:,:, output_table_icol__Ncameras] = controllable_args['Ncameras']['value']
output_table[:,:, output_table_icol__range_near] += nps.transpose(range_samples)
output_table[:,:, output_table_icol__range_far] = -1
output_table[:,:, output_table_icol__tilt_radius ] = controllable_args['tilt_radius']['value']
output_table[:,:, output_table_icol__object_width_n ] = controllable_args['object_width_n']['value']
output_table[:,:, output_table_icol__object_spacing ] = controllable_args['object_spacing']['value']
samples = range_samples
elif args.scan == "tilt_radius":
Nframes_near_samples = np.array( (controllable_args['Nframes']['value'],), dtype=int)
Nframes_far_samples = np.array( (0,), dtype=int)
models_true = \
[ mrcal.cameramodel(intrinsics = model_intrinsics.intrinsics(),
imagersize = model_intrinsics.imagersize(),
rt_ref_cam = np.array((0,0,0,
i*args.camera_spacing,
0,0), dtype=float) ) \
for i in range(controllable_args['Ncameras']['value']) ]
Ntilt_rad_samples = args.Nscan_samples
tilt_rad_samples = np.linspace(*controllable_args['tilt_radius']['value'],
Ntilt_rad_samples, dtype=float)
for i_tilt in range(Ntilt_rad_samples):
output_table[i_tilt,:, output_table_icol__uncertainty] = \
eval_one_rangenear_tilt(models_true,
controllable_args['range']['value'], None,
tilt_rad_samples[i_tilt],
controllable_args['object_width_n']['value'],
args.object_height_n,
controllable_args['object_spacing']['value'],
uncertainty_at_range_samples,
controllable_args['Ncameras']['value'],
Nframes_near_samples, Nframes_far_samples)[0]
output_table[:,:, output_table_icol__Nframes_near] = controllable_args['Nframes']['value']
output_table[:,:, output_table_icol__Nframes_far] = 0
output_table[:,:, output_table_icol__Ncameras] = controllable_args['Ncameras']['value']
output_table[:,:, output_table_icol__range_near] = controllable_args['range']['value']
output_table[:,:, output_table_icol__range_far] = -1
output_table[:,:, output_table_icol__tilt_radius] += nps.transpose(tilt_rad_samples)
output_table[:,:, output_table_icol__object_width_n ] = controllable_args['object_width_n']['value']
output_table[:,:, output_table_icol__object_spacing ] = controllable_args['object_spacing']['value']
samples = tilt_rad_samples
elif args.scan == "Ncameras":
Nframes_near_samples = np.array( (controllable_args['Nframes']['value'],), dtype=int)
Nframes_far_samples = np.array( (0,), dtype=int)
N_Ncameras_samples = args.Nscan_samples
Ncameras_samples = np.linspace(*controllable_args['Ncameras']['value'],
N_Ncameras_samples, dtype=int)
for i_Ncameras in range(N_Ncameras_samples):
Ncameras = Ncameras_samples[i_Ncameras]
models_true = \
[ mrcal.cameramodel(intrinsics = model_intrinsics.intrinsics(),
imagersize = model_intrinsics.imagersize(),
rt_ref_cam = np.array((0,0,0,
i*args.camera_spacing,
0,0), dtype=float) ) \
for i in range(Ncameras) ]
output_table[i_Ncameras,:, output_table_icol__uncertainty] = \
eval_one_rangenear_tilt(models_true,
controllable_args['range']['value'], None,
controllable_args['tilt_radius']['value'],
controllable_args['object_width_n']['value'],
args.object_height_n,
controllable_args['object_spacing']['value'],
uncertainty_at_range_samples,
Ncameras,
Nframes_near_samples, Nframes_far_samples)[0]
output_table[:,:, output_table_icol__Nframes_near] = controllable_args['Nframes']['value']
output_table[:,:, output_table_icol__Nframes_far] = 0
output_table[:,:, output_table_icol__Ncameras] += nps.transpose(Ncameras_samples)
output_table[:,:, output_table_icol__range_near] = controllable_args['range']['value']
output_table[:,:, output_table_icol__range_far] = -1
output_table[:,:, output_table_icol__tilt_radius] = controllable_args['tilt_radius']['value']
output_table[:,:, output_table_icol__object_width_n ] = controllable_args['object_width_n']['value']
output_table[:,:, output_table_icol__object_spacing ] = controllable_args['object_spacing']['value']
samples = Ncameras_samples
elif args.scan == "Nframes":
Nframes_far_samples = np.array( (0,), dtype=int)
models_true = \
[ mrcal.cameramodel(intrinsics = model_intrinsics.intrinsics(),
imagersize = model_intrinsics.imagersize(),
rt_ref_cam = np.array((0,0,0,
i*args.camera_spacing,
0,0), dtype=float) ) \
for i in range(controllable_args['Ncameras']['value']) ]
N_Nframes_samples = args.Nscan_samples
Nframes_samples = np.linspace(*controllable_args['Nframes']['value'],
N_Nframes_samples, dtype=int)
for i_Nframes in range(N_Nframes_samples):
Nframes = Nframes_samples[i_Nframes]
Nframes_near_samples = np.array( (Nframes,), dtype=int)
output_table[i_Nframes,:, output_table_icol__uncertainty] = \
eval_one_rangenear_tilt(models_true,
controllable_args['range']['value'], None,
controllable_args['tilt_radius']['value'],
controllable_args['object_width_n']['value'],
args.object_height_n,
controllable_args['object_spacing']['value'],
uncertainty_at_range_samples,
controllable_args['Ncameras']['value'],
Nframes_near_samples, Nframes_far_samples)[0]
output_table[:,:, output_table_icol__Nframes_near]+= nps.transpose(Nframes_samples)
output_table[:,:, output_table_icol__Nframes_far] = 0
output_table[:,:, output_table_icol__Ncameras] = controllable_args['Ncameras']['value']
output_table[:,:, output_table_icol__range_near] = controllable_args['range']['value']
output_table[:,:, output_table_icol__range_far] = -1
output_table[:,:, output_table_icol__tilt_radius] = controllable_args['tilt_radius']['value']
output_table[:,:, output_table_icol__object_width_n ] = controllable_args['object_width_n']['value']
output_table[:,:, output_table_icol__object_spacing ] = controllable_args['object_spacing']['value']
samples = Nframes_samples
elif args.scan == "object_width_n":
Nframes_near_samples = np.array( (controllable_args['Nframes']['value'],), dtype=int)
Nframes_far_samples = np.array( (0,), dtype=int)
models_true = \
[ mrcal.cameramodel(intrinsics = model_intrinsics.intrinsics(),
imagersize = model_intrinsics.imagersize(),
rt_ref_cam = np.array((0,0,0,
i*args.camera_spacing,
0,0), dtype=float) ) \
for i in range(controllable_args['Ncameras']['value']) ]
Nsamples = args.Nscan_samples
samples = np.linspace(*controllable_args['object_width_n']['value'],
Nsamples, dtype=int)
# As I move the width, I adjust the spacing to keep the total board size
# constant. The object spacing in the argument applies to the MIN value of
# the object_width_n.
W = (controllable_args['object_width_n']['value'][0]-1) * controllable_args['object_spacing']['value']
object_spacing = W / samples
for i_sample in range(Nsamples):
output_table[i_sample,:, output_table_icol__uncertainty] = \
eval_one_rangenear_tilt(models_true,
controllable_args['range']['value'], None,
controllable_args['tilt_radius']['value'],
samples[i_sample],
samples[i_sample],
object_spacing[i_sample],
uncertainty_at_range_samples,
controllable_args['Ncameras']['value'],
Nframes_near_samples, Nframes_far_samples)[0]
output_table[:,:, output_table_icol__Nframes_near] = controllable_args['Nframes']['value']
output_table[:,:, output_table_icol__Nframes_far] = 0
output_table[:,:, output_table_icol__Ncameras] = controllable_args['Ncameras']['value']
output_table[:,:, output_table_icol__range_near] = controllable_args['range']['value']
output_table[:,:, output_table_icol__range_far] = -1
output_table[:,:, output_table_icol__tilt_radius] = controllable_args['tilt_radius']['value']
output_table[:,:, output_table_icol__object_width_n ]+= nps.transpose(samples)
output_table[:,:, output_table_icol__object_spacing ]+= nps.transpose(object_spacing)
elif args.scan == "object_spacing":
Nframes_near_samples = np.array( (controllable_args['Nframes']['value'],), dtype=int)
Nframes_far_samples = np.array( (0,), dtype=int)
models_true = \
[ mrcal.cameramodel(intrinsics = model_intrinsics.intrinsics(),
imagersize = model_intrinsics.imagersize(),
rt_ref_cam = np.array((0,0,0,
i*args.camera_spacing,
0,0), dtype=float) ) \
for i in range(controllable_args['Ncameras']['value']) ]
Nsamples = args.Nscan_samples
samples = np.linspace(*controllable_args['object_spacing']['value'],
Nsamples, dtype=float)
# As I move the spacing, I leave object_width_n, letting the total board
# size change. The object spacing in the argument applies to the MIN value
# of the object_width_n.
for i_sample in range(Nsamples):
r = controllable_args['range']['value']
if args.scan_object_spacing_compensate_range_from:
r *= samples[i_sample]/args.scan_object_spacing_compensate_range_from
output_table[i_sample,:, output_table_icol__uncertainty] = \
eval_one_rangenear_tilt(models_true,
r, None,
controllable_args['tilt_radius']['value'],
controllable_args['object_width_n']['value'],
args.object_height_n,
samples[i_sample],
uncertainty_at_range_samples,
controllable_args['Ncameras']['value'],
Nframes_near_samples, Nframes_far_samples)[0]
output_table[:,:, output_table_icol__Nframes_near] = controllable_args['Nframes']['value']
output_table[:,:, output_table_icol__Nframes_far] = 0
output_table[:,:, output_table_icol__Ncameras] = controllable_args['Ncameras']['value']
output_table[:,:, output_table_icol__range_far] = -1
output_table[:,:, output_table_icol__tilt_radius] = controllable_args['tilt_radius']['value']
output_table[:,:, output_table_icol__object_width_n ] = controllable_args['object_width_n']['value']
output_table[:,:, output_table_icol__object_spacing ]+= nps.transpose(samples)
if args.scan_object_spacing_compensate_range_from:
output_table[:,:, output_table_icol__range_near] += controllable_args['range']['value'] * nps.transpose(samples/samples[0])
else:
output_table[:,:, output_table_icol__range_near] = controllable_args['range']['value']
else:
# no --scan. We just want one sample
Nframes_near_samples = np.array( (controllable_args['Nframes']['value'],), dtype=int)
Nframes_far_samples = np.array( (0,), dtype=int)
models_true = \
[ mrcal.cameramodel(intrinsics = model_intrinsics.intrinsics(),
imagersize = model_intrinsics.imagersize(),
rt_ref_cam = np.array((0,0,0,
i*args.camera_spacing,
0,0), dtype=float) ) \
for i in range(controllable_args['Ncameras']['value']) ]
output_table[0,:, output_table_icol__uncertainty] = \
eval_one_rangenear_tilt(models_true,
controllable_args['range']['value'], None,
controllable_args['tilt_radius']['value'],
controllable_args['object_width_n']['value'],
args.object_height_n,
controllable_args['object_spacing']['value'],
uncertainty_at_range_samples,
controllable_args['Ncameras']['value'],
Nframes_near_samples, Nframes_far_samples)[0]
output_table[:,:, output_table_icol__Nframes_near] = controllable_args['Nframes']['value']
output_table[:,:, output_table_icol__Nframes_far] = 0
output_table[:,:, output_table_icol__Ncameras] = controllable_args['Ncameras']['value']
output_table[:,:, output_table_icol__range_near] = controllable_args['range']['value']
output_table[:,:, output_table_icol__range_far] = -1
output_table[:,:, output_table_icol__tilt_radius] = controllable_args['tilt_radius']['value']
output_table[:,:, output_table_icol__object_width_n ] = controllable_args['object_width_n']['value']
output_table[:,:, output_table_icol__object_spacing ] = controllable_args['object_spacing']['value']
samples = None
if isinstance(controllable_args['range']['value'], float):
guides = [ f"arrow nohead dashtype 3 from {controllable_args['range']['value']},graph 0 to {controllable_args['range']['value']},graph 1" ]
else:
guides = [ f"arrow nohead dashtype 3 from {r},graph 0 to {r},graph 1" for r in controllable_args['range']['value'] ]
guides.append(f"arrow nohead dashtype 3 from graph 0,first {args.observed_pixel_uncertainty} to graph 1,first {args.observed_pixel_uncertainty}")
title = args.title
if args.scan == "num_far_constant_Nframes_near":
if title is None:
title = f"Scanning 'far' observations added to a set of 'near' observations. Have {controllable_args['Ncameras']['value']} cameras, {args.Nframes_near} 'near' observations, at ranges {controllable_args['range']['value']}."
legend_what = 'Nframes_far'
elif args.scan == "num_far_constant_Nframes_all":
if title is None:
title = f"Scanning 'far' observations replacing 'near' observations. Have {controllable_args['Ncameras']['value']} cameras, {args.Nframes_all} total observations, at ranges {controllable_args['range']['value']}."
legend_what = 'Nframes_far'
elif args.scan == "Nframes":
if title is None:
title = f"Scanning Nframes. Have {controllable_args['Ncameras']['value']} cameras looking out at {controllable_args['range']['value']:.2f}m."
legend_what = 'Nframes'
elif args.scan == "Ncameras":
if title is None:
title = f"Scanning Ncameras. Observing {controllable_args['Nframes']['value']} boards at {controllable_args['range']['value']:.2f}m."
legend_what = 'Ncameras'
elif args.scan == "range":
if title is None:
title = f"Scanning the distance to observations. Have {controllable_args['Ncameras']['value']} cameras looking at {controllable_args['Nframes']['value']} boards."
legend_what = 'Range-to-chessboards'
elif args.scan == "tilt_radius":
if title is None:
title = f"Scanning the board tilt. Have {controllable_args['Ncameras']['value']} cameras looking at {controllable_args['Nframes']['value']} boards at {controllable_args['range']['value']:.2f}m"
legend_what = 'Random chessboard tilt radius'
elif args.scan == "object_width_n":
if title is None:
title = f"Scanning the calibration object density, keeping the board size constant. Have {controllable_args['Ncameras']['value']} cameras looking at {controllable_args['Nframes']['value']} boards at {controllable_args['range']['value']:.2f}m"
legend_what = 'Number of chessboard points per side'
elif args.scan == "object_spacing":
if title is None:
if args.scan_object_spacing_compensate_range_from:
title = f"Scanning the calibration object spacing, keeping the point count constant, and letting the board grow. Range grows with spacing. Have {controllable_args['Ncameras']['value']} cameras looking at {controllable_args['Nframes']['value']} boards at {controllable_args['range']['value']:.2f}m"
else:
title = f"Scanning the calibration object spacing, keeping the point count constant, and letting the board grow. Range is constant. Have {controllable_args['Ncameras']['value']} cameras looking at {controllable_args['Nframes']['value']} boards at {controllable_args['range']['value']:.2f}m"
legend_what = 'Distance between adjacent chessboard corners'
else:
# no --scan. We just want one sample
if title is None:
title = f"Have {controllable_args['Ncameras']['value']} cameras looking at {controllable_args['Nframes']['value']} boards at {controllable_args['range']['value']:.2f}m with tilt radius {controllable_args['tilt_radius']['value']}"
if args.extratitle is not None:
title = f"{title}: {args.extratitle}"
if samples is None:
legend = None
elif samples.dtype.kind == 'i':
legend = np.array([ f"{legend_what} = {x}" for x in samples])
else:
legend = np.array([ f"{legend_what} = {x:.2f}" for x in samples])
np.savetxt(sys.stdout,
nps.clump(output_table, n=2),
fmt = output_table_fmt,
header= output_table_legend)
plotoptions = \
dict( yrange = (0, args.ymax),
_with = 'lines',
_set = guides,
unset = 'grid',
title = title,
xlabel = 'Range (m)',
ylabel = 'Expected worst-direction uncertainty (pixels)',
hardcopy = args.hardcopy,
terminal = args.terminal,
wait = not args.explore and args.hardcopy is None)
if legend is not None: plotoptions['legend'] = legend
if args.set:
gp.add_plot_option(plotoptions,
_set = args.set)
if args.unset:
gp.add_plot_option(plotoptions,
_unset = args.unset)
gp.plot(uncertainty_at_range_samples,
output_table[:,:, output_table_icol__uncertainty],
**plotoptions)
if args.explore:
import IPython
IPython.embed()
sys.exit()
|