| 1 |
#!/usr/bin/env python3 |
| 2 |
|
| 3 |
r'''Reproject calibration images to show a chessboard-centered view |
| 4 |
|
| 5 |
SYNOPSIS |
| 6 |
|
| 7 |
analyses/mrcal-reproject-to-chessboard \ |
| 8 |
--image-directory images/ \ |
| 9 |
--outdir /tmp/frames-splined \ |
| 10 |
splined.cameramodel |
| 11 |
|
| 12 |
ffmpeg \ |
| 13 |
-r 5 \ |
| 14 |
-f image2 \ |
| 15 |
-export_path_metadata 1 \ |
| 16 |
-pattern_type glob \ |
| 17 |
-i "/tmp/frames-splined/*.jpg" \ |
| 18 |
-filter:v "drawtext=text='%{metadata\\:lavf.image2dec.source_basename}':fontcolor=yellow:fontsize=48" \ |
| 19 |
-y \ |
| 20 |
/tmp/reprojected-to-chessboard.mp4 |
| 21 |
|
| 22 |
Outliers are highlighted. This is useful to visualize calibration errors. A |
| 23 |
perfect solve would display exactly the same calibration grid with every frame. |
| 24 |
In reality, we see the small errors in the calibration, and how they affect the |
| 25 |
individual chessboard corner observations. The intent of this tool is to be able |
| 26 |
to see any unmodeled chessboard deformations. It's not yet clear if this tool |
| 27 |
can do that effectively |
| 28 |
|
| 29 |
''' |
| 30 |
|
| 31 |
|
| 32 |
import sys |
| 33 |
import argparse |
| 34 |
import re |
| 35 |
import os |
| 36 |
|
| 37 |
def parse_args(): |
| 38 |
|
| 39 |
parser = \ |
| 40 |
argparse.ArgumentParser(description = __doc__, |
| 41 |
formatter_class=argparse.RawDescriptionHelpFormatter) |
| 42 |
|
| 43 |
parser.add_argument('--image-path-prefix', |
| 44 |
help='''If given, we prepend the given prefix to the image paths. Exclusive with |
| 45 |
--image-directory''') |
| 46 |
|
| 47 |
parser.add_argument('--image-directory', |
| 48 |
help='''If given, we extract the filenames from the image paths in the solve, and use |
| 49 |
the given directory to find those filenames. Exclusive |
| 50 |
with --image-path-prefix''') |
| 51 |
parser.add_argument('--outdir', |
| 52 |
type=lambda d: d if os.path.isdir(d) else \ |
| 53 |
parser.error(f"--outdir requires an existing directory as the arg, but got '{d}'"), |
| 54 |
default='.', |
| 55 |
help='''Directory to write the output into. If omitted, |
| 56 |
we use the current directory''') |
| 57 |
|
| 58 |
parser.add_argument('--resolution-px-m', |
| 59 |
default = 1000, |
| 60 |
type = float, |
| 61 |
help='''The resolution of the output image in pixels/m. |
| 62 |
Defaults to 1000''') |
| 63 |
|
| 64 |
parser.add_argument('--force', '-f', |
| 65 |
action='store_true', |
| 66 |
default=False, |
| 67 |
help='''By default existing files are not overwritten. Pass --force to overwrite them |
| 68 |
without complaint''') |
| 69 |
|
| 70 |
parser.add_argument('model', |
| 71 |
type=str, |
| 72 |
help='''The input camera model. We use its |
| 73 |
optimization_inputs''') |
| 74 |
|
| 75 |
args = parser.parse_args() |
| 76 |
|
| 77 |
if args.image_path_prefix is not None and \ |
| 78 |
args.image_directory is not None: |
| 79 |
print("--image-path-prefix and --image-directory are mutually exclusive", |
| 80 |
file=sys.stderr) |
| 81 |
sys.exit(1) |
| 82 |
|
| 83 |
return args |
| 84 |
|
| 85 |
args = parse_args() |
| 86 |
|
| 87 |
# arg-parsing is done before the imports so that --help works without building |
| 88 |
# stuff, so that I can generate the manpages and README |
| 89 |
|
| 90 |
|
| 91 |
import numpy as np |
| 92 |
import numpysane as nps |
| 93 |
import cv2 |
| 94 |
import mrcal |
| 95 |
|
| 96 |
try: |
| 97 |
model = mrcal.cameramodel(args.model) |
| 98 |
except Exception as e: |
| 99 |
print(f"Couldn't load camera model '{args.model}': {e}", file=sys.stderr) |
| 100 |
sys.exit(1) |
| 101 |
|
| 102 |
optimization_inputs = model.optimization_inputs() |
| 103 |
if optimization_inputs is None: |
| 104 |
print("The given model MUST have the optimization_inputs for this tool to be useful", |
| 105 |
file=sys.stderr) |
| 106 |
sys.exit(1) |
| 107 |
|
| 108 |
object_height_n,object_width_n = optimization_inputs['observations_board'].shape[1:3] |
| 109 |
object_spacing = optimization_inputs['calibration_object_spacing'] |
| 110 |
|
| 111 |
# I cover a space of N+1 squares wide/tall: N-1 between all the corners + 1 on |
| 112 |
# either side. I span squares -1..N inclusive |
| 113 |
Nx = int(args.resolution_px_m * object_spacing * (object_width_n + 1) + 0.5) |
| 114 |
Ny = int(args.resolution_px_m * object_spacing * (object_height_n + 1) + 0.5) |
| 115 |
|
| 116 |
# shape (Nframes,6) |
| 117 |
rt_ref_frame = optimization_inputs['rt_ref_frame'] |
| 118 |
rt_cam_ref = optimization_inputs['rt_cam_ref'] |
| 119 |
ifcice = optimization_inputs['indices_frame_camintrinsics_camextrinsics'] |
| 120 |
observations = optimization_inputs['observations_board'] |
| 121 |
lensmodel = optimization_inputs['lensmodel'] |
| 122 |
intrinsics_data = optimization_inputs['intrinsics'] |
| 123 |
imagepaths = optimization_inputs.get('imagepaths') |
| 124 |
if imagepaths is None: |
| 125 |
print("The given model MUST have the image paths for this tool to be useful", |
| 126 |
file=sys.stderr) |
| 127 |
sys.exit(1) |
| 128 |
|
| 129 |
# shape (Ny,Nx,3) |
| 130 |
p_frame = \ |
| 131 |
mrcal.ref_calibration_object(object_width_n,object_height_n, |
| 132 |
object_spacing, |
| 133 |
x_corner0 = -1, |
| 134 |
x_corner1 = object_width_n, |
| 135 |
Nx = Nx, |
| 136 |
y_corner0 = -1, |
| 137 |
y_corner1 = object_height_n, |
| 138 |
Ny = Ny, |
| 139 |
calobject_warp = optimization_inputs['calobject_warp']) |
| 140 |
|
| 141 |
for i in range(len(ifcice)): |
| 142 |
|
| 143 |
iframe, icamintrinsics, icamextrinsics = ifcice[i] |
| 144 |
|
| 145 |
p_ref = \ |
| 146 |
mrcal.transform_point_rt(rt_ref_frame[iframe], p_frame) |
| 147 |
if icamextrinsics >= 0: |
| 148 |
p_cam = \ |
| 149 |
mrcal.transform_point_rt(rt_cam_ref[icamextrinsics], p_ref) |
| 150 |
else: |
| 151 |
p_cam = p_ref |
| 152 |
|
| 153 |
# shape (Ny,Nx,2) |
| 154 |
q = mrcal.project(p_cam, lensmodel, intrinsics_data[icamintrinsics]).astype(np.float32) |
| 155 |
|
| 156 |
imagepath = imagepaths[i] |
| 157 |
if args.image_path_prefix is not None: |
| 158 |
imagepath = f"{args.image_path_prefix}/{imagepath}" |
| 159 |
elif args.image_directory is not None: |
| 160 |
imagepath = f"{args.image_directory}/{os.path.basename(imagepath)}" |
| 161 |
|
| 162 |
try: |
| 163 |
image = mrcal.load_image(imagepath, |
| 164 |
bits_per_pixel = 24, |
| 165 |
channels = 3) |
| 166 |
except: |
| 167 |
print(f"Couldn't read image at '{imagepath}'", file=sys.stderr) |
| 168 |
sys.exit(1) |
| 169 |
|
| 170 |
image_out = mrcal.transform_image(image,q) |
| 171 |
|
| 172 |
# shape (Ny,Nx) |
| 173 |
weight = observations[i,:,:,2] |
| 174 |
mask_outlier = weight<=0 |
| 175 |
|
| 176 |
# Mark all the outliers |
| 177 |
for iy,ix in np.argwhere(mask_outlier): |
| 178 |
# iy,ix index corners. I need to convert the to pixels in my image |
| 179 |
# I have pixels = linspace(-1,object_width_n,Nx) |
| 180 |
# So: |
| 181 |
qx = (ix+1)*(Nx-1) / (object_width_n +1) |
| 182 |
qy = (iy+1)*(Ny-1) / (object_height_n+1) |
| 183 |
|
| 184 |
# Red circle around the outlier |
| 185 |
red = (0,0,255) |
| 186 |
cv2.circle(image_out, |
| 187 |
center = np.round(np.array((qx,qy))).astype(int), |
| 188 |
radius = 10, |
| 189 |
color = red, |
| 190 |
thickness = 2) |
| 191 |
|
| 192 |
imagepath_out = f"{args.outdir}/{os.path.basename(imagepath)}" |
| 193 |
if not args.force and os.path.exists(imagepath_out): |
| 194 |
print("File already exists: '{imagepath_out}'. Not overwriting; pass --force to overwrite. Giving up.", |
| 195 |
file=sys.stderr) |
| 196 |
sys.exit(1) |
| 197 |
mrcal.save_image(imagepath_out, image_out) |
| 198 |
print(f"Wrote '{imagepath_out}'") |