| 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 valid-intrinsics region |
| 12 |
|
| 13 |
SYNOPSIS |
| 14 |
|
| 15 |
$ mrcal-show-valid-intrinsics-region --write-image --image image.png left.cameramodel |
| 16 |
Wrote image-valid-intrinsics-region.png |
| 17 |
|
| 18 |
Given a camera model (or models), this tool displays the valid-intrinsics |
| 19 |
region(s). All the given models MUST contain a valid-intrinsics region. Empty |
| 20 |
regions are handled properly. |
| 21 |
|
| 22 |
If an image is given, the region is rendered overlaid onto the image. |
| 23 |
|
| 24 |
If --points then we also read x,y points from STDIN, and plot those too. |
| 25 |
|
| 26 |
By default, we use gnuplotlib to make an interactive plot. Alternately, pass |
| 27 |
--write-image to annotate a given image, and write the new image on disk. |
| 28 |
|
| 29 |
--write-image is not supported together with --points. |
| 30 |
|
| 31 |
''' |
| 32 |
|
| 33 |
|
| 34 |
import sys |
| 35 |
import argparse |
| 36 |
import re |
| 37 |
import os |
| 38 |
|
| 39 |
def parse_args(): |
| 40 |
|
| 41 |
parser = \ |
| 42 |
argparse.ArgumentParser(description = __doc__, |
| 43 |
formatter_class=argparse.RawDescriptionHelpFormatter) |
| 44 |
|
| 45 |
parser.add_argument('--write-image', |
| 46 |
action='store_true', |
| 47 |
help='''By default I make a plot. If --write-image is |
| 48 |
given, I output an annotated image instead. The image to |
| 49 |
annotate is then required, and is given in --image''') |
| 50 |
parser.add_argument('--force', '-f', |
| 51 |
action='store_true', |
| 52 |
default=False, |
| 53 |
help='''With --write-image we refuse to overwrite any existing images. Pass --force to |
| 54 |
allow overwriting''') |
| 55 |
parser.add_argument('--points', |
| 56 |
action='store_true', |
| 57 |
help='''If given, I read a set of xy points from STDIN, and include them in the plot. |
| 58 |
This applies ONLY if not --write-image''') |
| 59 |
parser.add_argument('--title', |
| 60 |
type=str, |
| 61 |
default = None, |
| 62 |
help='''Extra title string for the plot''') |
| 63 |
parser.add_argument('--hardcopy', |
| 64 |
type=str, |
| 65 |
help='''Write the output to disk, instead of making an interactive plot''') |
| 66 |
parser.add_argument('--terminal', |
| 67 |
type=str, |
| 68 |
help=r'''gnuplotlib terminal. The default is good almost always, so most people don't |
| 69 |
need this option''') |
| 70 |
parser.add_argument('--set', |
| 71 |
type=str, |
| 72 |
action='append', |
| 73 |
help='''Extra 'set' directives to gnuplotlib. Can be given multiple times''') |
| 74 |
parser.add_argument('--unset', |
| 75 |
type=str, |
| 76 |
action='append', |
| 77 |
help='''Extra 'unset' directives to gnuplotlib. Can be given multiple times''') |
| 78 |
parser.add_argument('--image', |
| 79 |
type=str, |
| 80 |
required = False, |
| 81 |
help='''Image to annotate. Used both with and without --write-image''') |
| 82 |
parser.add_argument('models', |
| 83 |
type=str, |
| 84 |
nargs='+', |
| 85 |
help='''Input camera model(s)''') |
| 86 |
|
| 87 |
return parser.parse_args() |
| 88 |
|
| 89 |
args = parse_args() |
| 90 |
|
| 91 |
# arg-parsing is done before the imports so that --help works without building |
| 92 |
# stuff, so that I can generate the manpages and README |
| 93 |
|
| 94 |
|
| 95 |
if args.write_image: |
| 96 |
if args.image is None: |
| 97 |
print("--write-image NEEDS an image to annotate", |
| 98 |
file=sys.stderr) |
| 99 |
sys.exit(1) |
| 100 |
|
| 101 |
if args.title is not None or \ |
| 102 |
args.hardcopy is not None or \ |
| 103 |
args.terminal is not None or \ |
| 104 |
args.set is not None or \ |
| 105 |
args.unset is not None: |
| 106 |
print("--title and --hardcopy and --terminal and --set and --unset are only valid without --write-image", |
| 107 |
file=sys.stderr) |
| 108 |
sys.exit(1) |
| 109 |
|
| 110 |
if args.points: |
| 111 |
print("Currently --points is implemented ONLY if not --write-image", |
| 112 |
file=sys.stderr) |
| 113 |
sys.exit(1) |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
import mrcal |
| 119 |
import numpy as np |
| 120 |
|
| 121 |
def openmodel(f): |
| 122 |
try: |
| 123 |
return mrcal.cameramodel(f) |
| 124 |
except Exception as e: |
| 125 |
print(f"Couldn't load camera model '{f}': {e}", |
| 126 |
file=sys.stderr) |
| 127 |
sys.exit(1) |
| 128 |
|
| 129 |
models = [openmodel(modelfilename) for modelfilename in args.models] |
| 130 |
|
| 131 |
if any( m.valid_intrinsics_region() is None for m in models ): |
| 132 |
print("Not all given models have a valid-intrinsics contour! Giving up", |
| 133 |
file=sys.stderr) |
| 134 |
sys.exit(1) |
| 135 |
|
| 136 |
if args.write_image: |
| 137 |
|
| 138 |
# this function stolen from mrcal-reproject-image. Please consolidate |
| 139 |
def target_image_filename(filename_in, suffix): |
| 140 |
|
| 141 |
base,extension = os.path.splitext(filename_in) |
| 142 |
if len(extension) != 4: |
| 143 |
print(f"imagefile must end in .xxx where 'xxx' is some image extension. Instead got '{filename_in}'", |
| 144 |
file=sys.stderr) |
| 145 |
sys.exit(1) |
| 146 |
filename_out = f"{base}-{suffix}{extension}" |
| 147 |
if not args.force and os.path.exists(filename_out): |
| 148 |
print(f"Target image '{filename_out}' already exists. Doing nothing, and giving up. Pass -f to overwrite", |
| 149 |
file=sys.stderr) |
| 150 |
sys.exit(1) |
| 151 |
return filename_out |
| 152 |
|
| 153 |
def target_image_filename(filename_in, suffix): |
| 154 |
|
| 155 |
base,extension = os.path.splitext(filename_in) |
| 156 |
if len(extension) != 4: |
| 157 |
print(f"imagefile must end in .xxx where 'xxx' is some image extension. Instead got '{filename_in}'", |
| 158 |
file=sys.stderr) |
| 159 |
sys.exit(1) |
| 160 |
filename_out = f"{base}-{suffix}{extension}" |
| 161 |
return filename_out |
| 162 |
|
| 163 |
|
| 164 |
imagefile_out = target_image_filename(args.image, 'valid-intrinsics-region') |
| 165 |
image_out = mrcal.load_image(args.image) |
| 166 |
for m in models: |
| 167 |
mrcal.annotate_image__valid_intrinsics_region(image_out, m) |
| 168 |
mrcal.save_image(imagefile_out, image_out) |
| 169 |
sys.stderr.write("Wrote {}\n".format(imagefile_out)) |
| 170 |
|
| 171 |
else: |
| 172 |
|
| 173 |
points = None |
| 174 |
if args.points: |
| 175 |
points = np.loadtxt(sys.stdin) |
| 176 |
|
| 177 |
plotkwargs_extra = {} |
| 178 |
if args.set is not None: |
| 179 |
plotkwargs_extra['set'] = args.set |
| 180 |
if args.unset is not None: |
| 181 |
plotkwargs_extra['unset'] = args.unset |
| 182 |
|
| 183 |
plot = mrcal.show_valid_intrinsics_region( \ |
| 184 |
models, |
| 185 |
cameranames = args.models, |
| 186 |
image = args.image, |
| 187 |
points = points, |
| 188 |
hardcopy = args.hardcopy, |
| 189 |
terminal = args.terminal, |
| 190 |
title = args.title, |
| 191 |
**plotkwargs_extra) |
| 192 |
|
| 193 |
if args.hardcopy is None: |
| 194 |
plot.wait() |