| 1 |
#!/usr/bin/python3 |
| 2 |
|
| 3 |
r'''Evaluate observed point distribution from stationary observations |
| 4 |
|
| 5 |
Synopsis: |
| 6 |
|
| 7 |
$ observe-pixel-uncertainty '*.png' |
| 8 |
Evaluated 49 observations |
| 9 |
mean 1-sigma for independent x,y: 0.26 |
| 10 |
|
| 11 |
$ mrcal-calibrate-cameras --observed-pixel-uncertainty 0.26 ..... |
| 12 |
[ mrcal computes a camera calibration ] |
| 13 |
|
| 14 |
mrgingham has finite precision, so repeated observations of the same board will |
| 15 |
produce slightly different corner coordinates. This tool takes in a set of |
| 16 |
images (assumed observing a chessboard, with both the camera and board |
| 17 |
stationary). It then outputs the 1-standard-deviation statistic for the |
| 18 |
distribution of detected corners. This can then be passed in to mrcal: |
| 19 |
'mrcal-calibrate-cameras --observed-pixel-uncertainty ...' |
| 20 |
|
| 21 |
The distribution of the detected corners is assumed to be gaussian, and |
| 22 |
INDEPENDENT in the horizontal and vertical directions. If the x and y |
| 23 |
distributions are each s, then the LENGTH of the deviation of each pixel is a |
| 24 |
Rayleigh distribution with expected value s*sqrt(pi/2) ~ s*1.25 |
| 25 |
|
| 26 |
THIS TOOL PERFORMS VERY LIGHT OUTLIER REJECTION; IT IS ASSUMED THAT THE SCENE IS |
| 27 |
STATIONARY |
| 28 |
|
| 29 |
''' |
| 30 |
|
| 31 |
from __future__ import print_function |
| 32 |
|
| 33 |
import argparse |
| 34 |
import re |
| 35 |
import sys |
| 36 |
|
| 37 |
def parse_args(): |
| 38 |
parser = \ |
| 39 |
argparse.ArgumentParser(description = __doc__, |
| 40 |
formatter_class=argparse.RawDescriptionHelpFormatter) |
| 41 |
parser.add_argument('--show', |
| 42 |
required=False, |
| 43 |
choices = ('geometry', 'histograms'), |
| 44 |
help='''Visualize something. Arguments can be: "geometry": show the 1-stdev ellipses |
| 45 |
of the distribution for each chessboard corner |
| 46 |
separately. "histograms": show the distribution of all |
| 47 |
the x- and y-deviations off the mean''') |
| 48 |
parser.add_argument('--mrgingham', |
| 49 |
type=str, |
| 50 |
default='', |
| 51 |
help='''If we're processing images, these are the arguments given to |
| 52 |
mrgingham. If we are reading a pre-computed file, this does |
| 53 |
nothing''') |
| 54 |
parser.add_argument('--num-corners', |
| 55 |
type=int, |
| 56 |
default=100, |
| 57 |
help='''How many corners to expect in each image. If this is wrong I will throw an |
| 58 |
error. Defaults to 100''') |
| 59 |
parser.add_argument('--imagersize', |
| 60 |
type = int, |
| 61 |
nargs = 2, |
| 62 |
help='''Optional imager dimensions: width and height. This is optional. If given, we |
| 63 |
use it to size the "--show geometry" plot''') |
| 64 |
parser.add_argument('input', |
| 65 |
type=str, |
| 66 |
help='''Either 1: A glob that matches images observing a stationary calibration |
| 67 |
target. This must be a GLOB. So in the shell pass in |
| 68 |
'*.png' and NOT *.png. These are processed by |
| 69 |
'mrgingham' and the arguments passed in with |
| 70 |
--mrgingham. Or 2: a vnlog representing corner |
| 71 |
detections from these images. This is assumed to be a |
| 72 |
file with a filename ending in .vnl, formatted like |
| 73 |
'mrgingham' output: 3 columns: filename,x,y''') |
| 74 |
|
| 75 |
return parser.parse_args() |
| 76 |
|
| 77 |
|
| 78 |
args = parse_args() |
| 79 |
|
| 80 |
|
| 81 |
# I do these after the arg-parsing so that --help can work without all of this |
| 82 |
# being available. That makes the manpage generation work better |
| 83 |
import numpy as np |
| 84 |
import numpysane as nps |
| 85 |
import os |
| 86 |
import vnlog |
| 87 |
|
| 88 |
|
| 89 |
corners_output_process = None |
| 90 |
if re.match('.*\.vnl$', args.input): |
| 91 |
pipe_corners_read = open(args.input, 'r') |
| 92 |
else: |
| 93 |
|
| 94 |
import subprocess |
| 95 |
args_mrgingham = 'mrgingham ' + args.mrgingham + ' ' + args.input |
| 96 |
|
| 97 |
sys.stderr.write("Computing chessboard corners by running:\n {}\n". \ |
| 98 |
format(args_mrgingham)) |
| 99 |
|
| 100 |
corners_output_process = subprocess.Popen(args_mrgingham, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) |
| 101 |
pipe_corners_read = corners_output_process.stdout |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
# shape (Nobservations,num_corners,2) |
| 106 |
points = np.zeros((0, args.num_corners, 2)) |
| 107 |
|
| 108 |
# shape (num_corners,2) |
| 109 |
points_here = np.zeros((args.num_corners, 2)) |
| 110 |
ipt = 0 |
| 111 |
|
| 112 |
|
| 113 |
path = None |
| 114 |
vnlparser = vnlog.vnlog() |
| 115 |
def finish_image(path_new): |
| 116 |
global points, points_here, path, ipt |
| 117 |
|
| 118 |
if path is not None: |
| 119 |
|
| 120 |
if ipt != args.num_corners: |
| 121 |
raise Exception(f"Unexpected num_points in image {path}. Expected {args.num_corners}, but got {ipt}") |
| 122 |
|
| 123 |
try: |
| 124 |
points = nps.glue(points, points_here, axis=-3) |
| 125 |
except: |
| 126 |
raise Exception("I assume that all image observations have the same number of points.\n" + \ |
| 127 |
"So far had {} points per image, but image '{}' has {} points per image". \ |
| 128 |
format(points.shape[-2], path, points_here.shape[-2])) |
| 129 |
|
| 130 |
path = path_new |
| 131 |
ipt = 0 |
| 132 |
|
| 133 |
for l in pipe_corners_read: |
| 134 |
vnlparser.parse(l) |
| 135 |
|
| 136 |
d = vnlparser.values_dict() |
| 137 |
if not d or d['x'] is None: |
| 138 |
continue |
| 139 |
|
| 140 |
if path != d['filename']: |
| 141 |
finish_image(d['filename']) |
| 142 |
|
| 143 |
|
| 144 |
# should look at decimation levels |
| 145 |
|
| 146 |
points_here[ipt][0] = float(d['x']) |
| 147 |
points_here[ipt][1] = float(d['y']) |
| 148 |
ipt += 1 |
| 149 |
|
| 150 |
finish_image('') |
| 151 |
|
| 152 |
if corners_output_process: |
| 153 |
sys.stderr.write("Done computing chessboard corners\n") |
| 154 |
if corners_output_process.wait() != 0: |
| 155 |
err = corners_output_process.stderr.read() |
| 156 |
raise Exception("mrgingham failed: {}".format(err)) |
| 157 |
|
| 158 |
if len(points) == 0: |
| 159 |
print("Received no target observations") |
| 160 |
sys.exit(1) |
| 161 |
|
| 162 |
@nps.broadcast_define( (('n','n'),), (5,) ) |
| 163 |
def ellipse_stats(M): |
| 164 |
l,v = np.linalg.eig(M) |
| 165 |
|
| 166 |
l = np.sqrt(l) |
| 167 |
if l[0] > l[1]: |
| 168 |
# ...0 is the major axis |
| 169 |
# ...1 is the minor axis |
| 170 |
r0 = l[0] |
| 171 |
r1 = l[1] |
| 172 |
v0 = v[:,0] |
| 173 |
v1 = v[:,1] |
| 174 |
else: |
| 175 |
# ...0 is the minor axis |
| 176 |
# ...1 is the major axis |
| 177 |
r1 = l[0] |
| 178 |
r0 = l[1] |
| 179 |
v1 = v[:,0] |
| 180 |
v0 = v[:,1] |
| 181 |
|
| 182 |
# angle between x axis and major axis |
| 183 |
th = np.arctan2(v0[1], v0[0]) |
| 184 |
|
| 185 |
rx,ry = np.sqrt(M[0,0]),np.sqrt(M[1,1]) |
| 186 |
return np.array((r0,r1,rx,ry,th)) |
| 187 |
|
| 188 |
points_mean = np.mean(points, axis=0) |
| 189 |
points_centered = points - points_mean |
| 190 |
all_dxy = nps.clump(points_centered, n=2) # shape: (N,2) |
| 191 |
sigma_dxy = np.std(all_dxy,axis=-2) |
| 192 |
|
| 193 |
# I throw out outliers before reporting the statistics. I do it in discrete |
| 194 |
# points: any point that has either an outliery x or an outliery y is thrown out |
| 195 |
idx_in = np.max(np.abs(all_dxy) - 4.0*sigma_dxy, axis=-1) < 0.0 |
| 196 |
all_dxy = all_dxy[idx_in, :] |
| 197 |
all_dxy -= np.mean(all_dxy, axis=-2) |
| 198 |
sigma_dxy = np.std(all_dxy,axis=-2) |
| 199 |
|
| 200 |
|
| 201 |
title = "Have {} observations, separate x,y stdev: ({:.2f},{:.2f}), joint x,y stdev: {:.2f}". \ |
| 202 |
format(points.shape[0], |
| 203 |
np.std(all_dxy[:,0]), |
| 204 |
np.std(all_dxy[:,1]), |
| 205 |
np.std(all_dxy.ravel())) |
| 206 |
print(title) |
| 207 |
|
| 208 |
if not args.show: |
| 209 |
sys.exit(0) |
| 210 |
|
| 211 |
|
| 212 |
import gnuplotlib as gp |
| 213 |
|
| 214 |
if args.show == 'geometry': |
| 215 |
C = np.mean( nps.outer(points_centered, points_centered), axis=0 ) |
| 216 |
rad_major,rad_minor,rad_x,rad_y,angle = nps.transpose(ellipse_stats(C)) |
| 217 |
|
| 218 |
_xrange = None |
| 219 |
_yrange = None |
| 220 |
_yinv = None |
| 221 |
if args.imagersize is not None: |
| 222 |
_xrange = [0, args.imagersize[0]-1] |
| 223 |
_yrange = [args.imagersize[1]-1, 0] |
| 224 |
else: |
| 225 |
_yinv = True |
| 226 |
|
| 227 |
gp.plot((points_mean[:,0], points_mean[:,1], 2*rad_major,2*rad_minor, angle*180.0/np.pi, |
| 228 |
dict(_with = 'ellipses', tuplesize=5, _legend='1-sigma: dependent x,y')), |
| 229 |
(points_mean[:,0], points_mean[:,1], 2*rad_x, 2*rad_y, |
| 230 |
dict(_with = 'ellipses', tuplesize=4, _legend='1-sigma: independent x,y')), |
| 231 |
(points[...,0].ravel(), points[...,1].ravel(), |
| 232 |
dict(_with = 'points')), |
| 233 |
square = 1, |
| 234 |
_xrange = _xrange, |
| 235 |
_yrange = _yrange, |
| 236 |
_yinv = _yinv, |
| 237 |
wait = 1) |
| 238 |
|
| 239 |
elif args.show == 'histograms': |
| 240 |
|
| 241 |
var_xy = np.var(all_dxy, axis=-2) |
| 242 |
|
| 243 |
binwidth = 0.02 |
| 244 |
|
| 245 |
from scipy.special import erf |
| 246 |
equations = [ '{k}*exp(-(x)*(x)/(2.*{var})) / sqrt(2.*pi*{var}) title "{what}-distribution: gaussian fit" with lines lw 2'. \ |
| 247 |
format(what = 'x' if i==0 else 'y', |
| 248 |
var = var_xy[i], |
| 249 |
k = all_dxy.shape[-2]*erf(binwidth/(2.*np.sqrt(2)*np.sqrt(var_xy[i]))) * np.sqrt(2.*np.pi*var_xy[i])) \ |
| 250 |
for i in range(2) ] |
| 251 |
|
| 252 |
gp.plot( nps.transpose(all_dxy), |
| 253 |
_legend=np.array(('x-distribution: observed ', 'y-distribution: observed')), |
| 254 |
histogram = 1, |
| 255 |
binwidth = binwidth, |
| 256 |
_with = np.array(('boxes fill solid border lt -1', 'boxes fill transparent pattern 1', )), |
| 257 |
equation_above = equations, |
| 258 |
title = title, |
| 259 |
wait = 1) |