| 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'''Reports which input points lie within the valid-intrinsics region |
| 12 |
|
| 13 |
SYNOPSIS |
| 14 |
|
| 15 |
$ < points-in.vnl |
| 16 |
mrcal-is-within-valid-intrinsics-region --cols-xy x y |
| 17 |
camera.cameramodel |
| 18 |
> points-annotated.vnl |
| 19 |
|
| 20 |
mrcal camera models may have an estimate of the region of the imager where the |
| 21 |
intrinsics are trustworthy (originally computed with a low-enough error and |
| 22 |
uncertainty). When using a model, we may want to process points that fall |
| 23 |
outside of this region differently from points that fall within this region. |
| 24 |
This tool augments an incoming vnlog with a new column, indicating whether each |
| 25 |
point does or does not fall within the region. |
| 26 |
|
| 27 |
The input data comes in on standard input, and the output data is written to |
| 28 |
standard output. Both are vnlog data: a human-readable table of ascii text. The |
| 29 |
names of the x and y columns in the input are given in the required --cols-xy |
| 30 |
argument. The output contains all the columns from the input, with an extra |
| 31 |
column appended at the end, containing the results. The name of this column can |
| 32 |
be specified with --col-output, but this can be omitted if the default |
| 33 |
'is-within-valid-intrinsics-region' is acceptable. |
| 34 |
|
| 35 |
''' |
| 36 |
|
| 37 |
import sys |
| 38 |
import argparse |
| 39 |
import re |
| 40 |
import os |
| 41 |
|
| 42 |
def parse_args(): |
| 43 |
|
| 44 |
parser = \ |
| 45 |
argparse.ArgumentParser(description = __doc__, |
| 46 |
formatter_class=argparse.RawDescriptionHelpFormatter) |
| 47 |
parser.add_argument('--cols-xy', |
| 48 |
required=True, |
| 49 |
type=str, |
| 50 |
nargs=2, |
| 51 |
help='''The names of the columns in the input containing the x and y pixel |
| 52 |
coordinates respectively. This is required''') |
| 53 |
parser.add_argument('--col-output', |
| 54 |
required=False, |
| 55 |
type=str, |
| 56 |
default='is-within-valid-intrinsics-region', |
| 57 |
help='''The name of the column to append in the output. This is optional; a |
| 58 |
reasonable default will be used if omitted''') |
| 59 |
|
| 60 |
parser.add_argument('model', |
| 61 |
type=str, |
| 62 |
help='''Camera model''') |
| 63 |
|
| 64 |
return parser.parse_args() |
| 65 |
|
| 66 |
args = parse_args() |
| 67 |
|
| 68 |
# arg-parsing is done before the imports so that --help works without building |
| 69 |
# stuff, so that I can generate the manpages and README |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
import numpy as np |
| 76 |
import numpysane as nps |
| 77 |
import subprocess |
| 78 |
import mrcal |
| 79 |
import io |
| 80 |
|
| 81 |
try: |
| 82 |
model = mrcal.cameramodel(args.model) |
| 83 |
except Exception as e: |
| 84 |
print(f"Couldn't load camera model '{args.model}': {e}", file=sys.stderr) |
| 85 |
sys.exit(1) |
| 86 |
|
| 87 |
if model.valid_intrinsics_region() is None: |
| 88 |
print("The given model has no valid-intrinsics region defined!", |
| 89 |
file=sys.stderr) |
| 90 |
sys.exit(1) |
| 91 |
|
| 92 |
# I want to be able to add to any arbitrary existing columns, so I slurp. |
| 93 |
# Good-enough for now. |
| 94 |
in_file = sys.stdin.read() |
| 95 |
|
| 96 |
cmd = ('vnl-filter', '-p', '__linenumber=NR', '-p', f'^{args.cols_xy[0]}$', '-p', f'^{args.cols_xy[1]}$') |
| 97 |
try: |
| 98 |
ixy_text = subprocess.check_output(cmd, |
| 99 |
shell = False, |
| 100 |
encoding = 'ascii', |
| 101 |
input = in_file) |
| 102 |
except: |
| 103 |
print(f"Couldn't read columns {args.cols_xy} from the input", |
| 104 |
file = sys.stderr) |
| 105 |
sys.exit(1) |
| 106 |
|
| 107 |
ixy = nps.atleast_dims(np.loadtxt(io.StringIO(ixy_text)), -2) |
| 108 |
mask = mrcal.is_within_valid_intrinsics_region(ixy[..., 1:], model) |
| 109 |
|
| 110 |
|
| 111 |
fd_read,fd_write = os.pipe() |
| 112 |
os.set_inheritable(fd_read, True) |
| 113 |
|
| 114 |
pid = os.fork() |
| 115 |
|
| 116 |
if pid == 0: |
| 117 |
# Child. I write the mask back to the pipe. The parent will join it |
| 118 |
os.close(fd_read) |
| 119 |
|
| 120 |
with open(f"/dev/fd/{fd_write}", 'w') as f: |
| 121 |
np.savetxt(f, |
| 122 |
nps.transpose(nps.cat(ixy[...,0], mask)), |
| 123 |
fmt='%06d %d', |
| 124 |
header=f'__linenumber {args.col_output}') |
| 125 |
os.close(fd_write) |
| 126 |
sys.exit(0) |
| 127 |
|
| 128 |
# parent. join the input data, and the data coming in from the child |
| 129 |
os.close(fd_write) |
| 130 |
|
| 131 |
|
| 132 |
# I don't re-sort the input. NR was printed with %06d so it's already sorted. |
| 133 |
cmd = f"vnl-filter --skipcomments -p __linenumber='sprintf(\"%06d\",NR)',. | vnl-join -j __linenumber - /dev/fd/{fd_read} | vnl-filter -p \\!__linenumber" |
| 134 |
subprocess.run( cmd, |
| 135 |
shell = True, |
| 136 |
close_fds = False, |
| 137 |
encoding = 'ascii', |
| 138 |
input = in_file ) |
| 139 |
|
| 140 |
os.close(fd_read) |