# The Solve: Verified Corners, Decentering Terms, Held-Out Images and a Target That Was Not Flat

> How a robot calibration cell got a 190 degree fisheye stereo camera from 1 to 2 px of error at the lens edge to 0.3 px on held-out images: every method tried with its numbers, corner refinement that drops what it cannot verify, why the lens model needs decentering terms, a bowed target, a non-central lens, bootstrap error bars, and a tested prompt for an independent solver.

Page: https://egocam.ai/robot-cell/the-solve/

:::note[A record of one build]
Every number on this page was measured on the cell of the [case study](https://egocam.ai/robot-cell/) unless a source is given. It is one cell, one robot and eleven cameras: read it as experience, not as a standard.
:::

This page is the part of the [case study](https://egocam.ai/robot-cell/) where the gate was not met and had to be earned. The stills come from [Recording and stills](https://egocam.ai/robot-cell/recording-and-stills/); the general theory is on [Camera Calibration](https://egocam.ai/calibration/camera-calibration/) and [Judging a Calibration](https://egocam.ai/calibration/judging-a-calibration/).

## The starting point: an established tool

The first calibrations used [TartanCalib](https://github.com/castacks/tartancalib), which is built on [Kalibr](https://github.com/ethz-asl/kalibr) and adds corner refinement for wide lenses. Before any real image, the whole chain was run on rendered images of the cell with a known lens. In our experience, with the tool's code as we built it in November 2026, that rehearsal met two faults:

| Fault | Cause | Fix |
|---|---|---|
| Crash in intrinsic initialisation: "DLT algorithm needs at least 6 points" | For wide-angle models the tool keeps only corners within 80 degrees of the axis and accepts a view with 4 of them; the pose solver it then calls needs 6 | Raise the guard from 4 to 6 points |
| Camera-IMU calibration crashes when no robust loss is set, which is the default | A constructor gained an argument in the C++ code and the Python caller was not updated | Pass the argument |

On the rendered images the tool then recovered the lens to 0.02 px and the baseline to 0.006 mm. In our experience, that number says the chain is wired correctly and nothing about real lenses.

## Every method tried on real images

Error at the edge is the median or mean in bands of angle from the lens axis, on a 190 degree lens.

| # | Data | Method | Result |
|---|---|---|---|
| 1 | 61 poses | TartanCalib, double sphere model | Whole-image residual 0.30 px; edge 1.2 to 2.2 px on 14 to 22 corners per band: almost no data at the edge |
| 2 | Path re-planned from a measured model of what gets detected | same | Edge corners from 19 to 36 per band up to 263 to 283; edge 0.5 to 0.8 px |
| 3 | same | Extended unified model; unified model with radial and tangential terms | The model barely changed the residual. Only the model with tangential terms gave a 60 mm baseline; the others gave 61.1 mm |
| 4 | 231 stills, two passes of 114 poses | TartanCalib, both passes together | Double sphere diverged to 2 to 4 px |
| 5 | same | Own bundle adjuster on the tool's corners | Held-out edge 0.8 to 1.6 px: the corners were the limit |
| 6 | same | Corners refined in a local undistorted view, failures kept | 0.6 to 1.4 px |
| 7 | same | Refined corners, failures dropped, Kannala-Brandt with tangential terms, no statistical rejection | **Held-out 0.29 / 0.26 px at 80 to 85 degrees, 0.29 / 0.34 at 85 to 90, 0.44 / 0.42 at 90 to 96**; baseline 60.12 mm |

> **Photo:** A rendered still from the cell model: the calibration target seen at the edge of a 190 degree fisheye lens. The tags are stretched and curved there, which is why corners found at the edge need refining. Because the picture is rendered, the true position of every corner is known. (Rendered for this case study)

> **Diagram:** Reprojection error at 80 to 96 degrees from the axis of a 190 degree fisheye, as the range over the 5 degree bands, at five stages of the case study. The first two rows are medians for one lens from the established tool's corners; the last three are means over both lenses on images held out of the fit. Re-planning the path for what the detector finds brought data to the edge; refining each corner in a local undistorted view, and dropping every corner that failed the check, brought the held-out error under the 0.5 px gate. Keeping failed corners unrefined was worse than dropping them.

## What each step taught

**Plan for what is detected.** The first path was planned for geometric coverage. Tags seen at a grazing angle near the lens edge do not decode, so the edge was empty. The planner got a measured limit: how far the target may be tilted to the line of sight at each angle from the axis.

**Refine each corner where it is, and drop what fails.** At the edge of a fisheye a tag is small and stretched. Each corner was refined in a small view undistorted with the current lens model, at the image's own scale, then checked to be a real crossing of two edges. A corner that failed was dropped. Keeping failed corners unrefined was worse than both alternatives. The refinement dropped 11 to 18 % of corners at every angle and 23 to 27 % at 90 to 96 degrees, so it did not single out the edge. The idea is from the TartanCalib paper; the implementation was our own.

**The "noisy lens" was the detector.** One lens showed two to three times the error of the other in every run and model. Its sharpness and pixel noise were the same as the other's. After refinement the difference was gone.

**Decentering terms decide the baseline.** In our experience, the lens model hardly changed the pixel error and decided the geometry. Models without tangential terms put the second lens 1.1 to 1.3 mm further out and 1.5 to 1.6 mm low, absorbing a slightly off-centre lens into the stereo transform. With them the baseline was 60.1 mm and the lenses level. A stereo gate is only meaningful with such a model.

**Judge on images the fit never saw.** One still in five was held out; after the fit only its target pose was solved, with the lenses frozen. All gates read these images.

## Three things found by looking at residuals

| What the residuals showed | What it was | Effect of solving for it |
|---|---|---|
| The mean residual of each target corner, over all images, formed a smooth pattern, the same in both lenses | The target was bowed by about 0.5 mm | Three shape terms for the target: held-out 0.277 / 0.250 to 0.262 / 0.231 px; the baseline had been biased by 0.035 mm |
| Error at the edge that depended on the distance to the target, which no richer central model removed | The lens is not central: its viewpoint moves 2.8 mm along the axis by 90 degrees | A viewpoint-shift term: 80 to 96 degrees at 0.27 / 0.30 px. A central model is exact only near the calibration distance: 1.4 to 2.7 px off at the edge for an object at 0.2 m |
| Two halves of one run agreed on the baseline to 0.006 mm | The halves share their poses | A bootstrap over stills gave 0.06 mm: ten times larger and the honest figure |

What limits the remaining 0.25 px is still open: 0.16 px repeats with the pose and 0.12 px is random, and an image-space correction map gained nothing on held-out stills.

## Speed

The full route (TartanCalib, then refinement and our solver) took about 17 minutes. Seeding the corner prediction from a previous camera of the same model took it to about 1 to 3 minutes with the same numbers.

In our experience, such a shortcut must be validated on other units before it is trusted. Seeded from another camera's lens, the first new unit kept 55 of 115 images in one lens, and a later one passed the checks on half its corners with a wrong baseline. The route for a new unit now detects tags first, fits that unit's own lenses to them, and only then predicts and refines.

## The prompt: corner refinement

- **Inputs:** the stills, the rough corners from a tag detector with their target coordinates, and an approximate lens model.
- **How to check:** per image, the distance between refined corners and the corners predicted from a refitted target pose falls in every band of angle, most at the edge; the share of corners dropped is similar at the edge and in the centre.

```text
Write the corner refinement step of a fisheye calibration pipeline and run it on the images here. A
tag detector has already found the corners of the calibration target's tags in each image, but only
roughly: near the edge of a fisheye a tag is small, stretched and curved, and the detector's corners
are off by a pixel or more there. The calibration is only as good as these corners, so each one is
to be re-located precisely, or thrown away.

In this directory:
- `images/`: grey-scale stills of an AprilGrid target (Kalibr layout: every tag corner touches a
  small black square, so each corner is the crossing point of two edges, like the inner corner of a
  checkerboard).
- `corners_rough.json`: per image the rough corners: `corner_id`, `uv` in pixels, and `P`, the same
  corner on the target in metres (target frame, Z = 0).
- `lens_approx.yaml`: an approximate model of the lens (an earlier calibration, good to a pixel or
  two), with its projection formula named. [Double sphere: for a point (x, y, z), d1 = |(x, y, z)|,
  zeta = xi d1 + z, d2 = sqrt(x^2 + y^2 + zeta^2), u = fx x / (alpha d2 + (1 - alpha) zeta) + cx,
  the same for v.]

Method (the idea is published in the TartanCalib paper: refine where the distortion has been taken
out locally):
1. Per image, fit the target's pose from the rough corners through the approximate lens model,
  robustly, so a few gross errors do not steer it. That gives a predicted position for every corner.
2. For each corner, build a small virtual pinhole view that looks straight along the ray of that
  corner, [31 x 31] pixels with a focal length chosen so that the patch has about the same pixel
  scale as the image has there (do not magnify: interpolation invents no information). Resample the
  image into that view through the lens model. In this view the crossing is two straight edges.
3. Refine the crossing point to sub-pixel precision in the patch (OpenCV's cornerSubPix with a
  search window of about [7] pixels half-width, or a saddle-point fit), starting from the rough
  corner; if that fails, start again from the predicted position.
4. Verify, and drop the corner unless ALL hold: the refinement converged; it lies within [3] pixels
  of the predicted position; the patch really shows a crossing (opposite quadrants alike,
  neighbouring quadrants different, enough contrast); the refinement window (not necessarily the
  whole patch) lies inside the image and inside the lens's image circle (find the circle from the
  black surround if it is not given). A corner that fails is dropped, never passed on unrefined: on
  our data, keeping failed corners at their rough positions was worse than either dropping them or
  not refining at all.
5. Map the refined point back to image pixels through the same lens model. The model is only used to
  make the local view and to undo it, so its own small errors cancel to first order.

Write `refine_corners.py` and `corners_refined.json`: per image `corner_id`, refined `uv`, `P`, for
the kept corners only; plus `refine_report.md`: corners in, kept, dropped by reason, and the share
dropped by angle from the lens axis in 10 degree bands. A refinement that drops far more at the edge
than in the centre is not refining the edge, it is deleting it: say so if you see it.

A check you can run without knowing the true positions: fit the target pose per image again from the
refined corners and report the mean distance between refined and predicted corners, by the same
angle bands, before and after. It should fall everywhere, most at the edge.

Then refine a second time from start positions moved by a random [0.7] pixels and report how far the
results move: that is the repeatability of the refinement itself. Whatever distance to the predicted
corners remains beyond it is the lens model's error, not the corners'.

Tell me which images or regions you would not trust, and which of your drop rules never fired on
this data (an untested rule).
```

## How the corner-refinement prompt was tested

A fresh AI session got an earlier wording of this prompt and 16 rendered fisheye stills of the target, made with the cell model through a known 190 degree lens, blurred and with noise. The rough corners it was given were the true ones displaced as a detector displaces them: 0.7 px on average near the axis, 2.4 px beyond 80 degrees, with a few gross errors. Its lens model was deliberately a few pixels off. It never saw the true positions; its file was scored afterwards.

| Angle from the axis | Rough corners, mean error | Its refined corners | Kept |
|---|---|---|---|
| 0 to 40 degrees | 0.66 px | 0.050 px | 100 % |
| 40 to 60 | 1.21 px | 0.055 px | 100 % |
| 60 to 70 | 1.60 px | 0.047 px | 98 % |
| 70 to 80 | 1.97 px | 0.050 px | 99 % |
| 80 to 95 | 2.42 px | 0.043 px | 100 % |

Our own refinement, the one in production, scores 0.056 px on the same images and keeps 92 %.

Two cautions. These are rendered images: clean print, even light, no smudge. Its verification rules never had to fire, so they are untested, and it said so itself; on real stills our refinement drops 11 to 27 % of corners, and that dropping is where the gain came from. And its own check, the distance to corners predicted from the refitted pose, stayed at 0.4 to 1.6 px beyond 80 degrees. It attributed that to the lens model it had been given, not to the corners, and the truth agrees: the corners there were good to 0.04 px. The prompt now asks for the repeatability test it used to tell the two apart.

## The prompt: an independent solver

- **Inputs:** refined corners per still and lens, with their target coordinates; the image size.
- **How to check:** against another tool on the same corners, compared in pixels; the held-out error by angle band; the baseline against the design with its bootstrap error bar.

```text
Write a stereo camera calibration solver in Python (numpy and scipy only) and run it on the data in
this directory. It will be the second, independent solver next to an established tool: two solvers
that agree on the same corners is the evidence that a calibration is right, and when they disagree
the data is the first suspect.

Data: `corners.json` = `{"cam0": [{"file", "uv": [[u, v], ...], "P": [[X, Y, Z], ...]}, ...],
"cam1": [...]}`. Each entry is one still image of one lens: `uv` are detected target corners in
pixels, `P` the same corners on the calibration board in metres (the board's own frame, Z = 0 on a
flat board). The same `file` name in cam0 and cam1 is the same instant, so the board has one pose
for both. Images are [1920 x 1200]. cam0 is the left lens. The lenses are fisheyes of roughly [130
to 190] degrees.

Model, per lens: Kannala-Brandt with four radial terms plus two decentering (tangential) terms:
theta = angle of the ray from the axis; d = theta (1 + k1 theta^2 + k2 theta^4 + k3 theta^6 + k4
theta^8); (mx, my) = d (x, y) / sqrt(x^2 + y^2); r2 = mx^2 + my^2; mx' = mx + 2 p1 mx my + p2 (r2 +
2 mx^2); my' = my + p1 (r2 + 2 my^2) + 2 p2 mx my; u = fx mx' + cx, v = fy my' + cy.
The decentering terms are not optional: on our lenses, models without them moved the second lens
more than a millimetre to absorb a slightly off-centre lens, and the baseline came out wrong while
the pixel error looked fine.

Unknowns: 10 intrinsics per lens, the pose of cam1 relative to cam0, and one board pose per still.

Requirements:
1. Start values without help from me: focal length from a coarse search (the value that makes the
  per-image pose fits best), principal point at the image centre, distortion zero, a board pose per
  image from a pose fit, the stereo transform from the images both lenses share.
2. Joint optimisation of everything (Levenberg-Marquardt; exploit the block structure or sparse
  Jacobian so it finishes in about a minute), with a robust loss so a few bad corners do not steer
  it.
3. Hold out every fifth still (sorted by file name: the 5th, 10th, ...) completely from the fit.
  After the fit, solve only the board pose of each held-out still, one pose shared by both lenses,
  with the lenses and the stereo transform frozen, and report the error on those with no rejection
  of corners. This number, not the fit residual, is the result.
4. Report: held-out mean and median error per lens in pixels; the same split by angle from the axis
  in 10 degree bands, with corner counts; baseline in millimetres and the rotation between the
  lenses in degrees; the intrinsics; how many corners each lens had and how they are spread over a
  16 x 10 grid of the image (so thin coverage is visible).
5. An uncertainty for the baseline from a bootstrap over stills (resample the stills, refit, at
  least 20 times; a warm start is fine). Do not report the difference between two halves of the same
  run as the uncertainty: halves that share their poses agree far better than the truth allows.
6. Write `camchain.yaml` (intrinsics per lens, the 4 x 4 transform that takes a point from cam0's
  frame to cam1's, units stated) and `report.md`.

Sanity checks to run and print: the mean residual vector per region of the image (a smooth pattern
means the model or the board is wrong, not the corners); and, for each board corner, its mean
residual across all images carried back onto the board's plane in millimetres (a smooth pattern
across the board means the board is not flat or not the size stated; in pixel space this hides).

Say how far from the axis the corners reach: beyond that angle the model is an extrapolation,
whatever the lens's nominal field of view.

Tell me the numbers, anything in the data that looks wrong, and what you would not trust.
```

## How the solver prompt was tested

A fresh AI session got an earlier wording of this prompt and the real refined corners of one camera from the cell: 60 stills of a 136 degree lens, about 5,000 corners per lens. It had no other code. It wrote the solver and ran it in one go; the whole run, with a bootstrap of 48 full refits, took 88 s on a 32-thread computer.

| | Its solver | Our solver, same corners, flat target |
|---|---|---|
| Held-out mean error, left / right | 0.350 / 0.377 px | 0.34 / 0.36 px (different held-out stills) |
| Baseline | 60.044 mm, bootstrap 0.073 mm | 60.044 mm |
| Rotation between the lenses | 0.396 degrees | 0.39 degrees |
| Focal length, left / right | 724.7 / 726.2 px | 724.8 / 726.5 px |

Compared in pixels, as the [gates](https://egocam.ai/robot-cell/gates/) require: rays out to 60 degrees projected through both calibrations differ by 0.025 px on average in the left lens and 0.042 px in the right, 0.13 px at most. Two solvers written independently agree to a tenth of the corner noise.

Without being told, it reported that the target does not match its stated geometry, by about 1 mm of bow or the equivalent in shear. That is the bowed target of the table above. It also pointed out that the corners stop at 62 degrees from the axis, so nothing beyond about 55 degrees is constrained on this lens.

Its notes asked for the decentering formula to be written out and for the held-out procedure to be exact; the prompt above now does both.

## Sources

- [TartanCalib: Iterative Wide-Angle Lens Calibration using Adaptive SubPixel Refinement of AprilTags](https://arxiv.org/abs/2210.02511) — Duisterhof, Hu, Teng, Kaess and Scherer, 2022.
- [Kalibr](https://github.com/ethz-asl/kalibr) — ETH Zurich ASL, software.
- [A Generic Camera Model and Calibration Method for Conventional, Wide-Angle, and Fish-Eye Lenses](https://doi.org/10.1109/TPAMI.2006.153) — Kannala and Brandt, IEEE TPAMI 2006.
- [mrcal: a tour of cross-validation](https://mrcal.secretsauce.net/tour-cross-validation.html) — Kogan, NASA/JPL, software documentation.
- [Why Having 10,000 Parameters in Your Camera Model Is Better Than Twelve](https://arxiv.org/abs/1912.02908) — Schops, Larsson, Pollefeys and Sattler, CVPR 2020; central and non-central generic models.
