Non-Linear Least Squares Overview

Non-linear least squares (NLLS) is the mathematical framework underlying all refinement in calibration-rs. After linear initialization provides approximate parameter estimates, NLLS minimizes the reprojection error to achieve sub-pixel accuracy.

Problem Formulation

Objective: Minimize the sum of squared residuals:

where is the parameter vector and is the stacked residual vector.

In camera calibration, a typical residual is the reprojection error: the difference between an observed pixel and the predicted projection:

where is the camera projection function, and the parameters include intrinsics , distortion , and poses .

Gauss-Newton Method

The Gauss-Newton method exploits the least-squares structure. At the current estimate , linearize the residuals:

where is the Jacobian.

Substituting into the objective and minimizing with respect to :

gives the normal equations:

The matrix is the Gauss-Newton approximation to the Hessian ( when residuals are small).

Update:

Levenberg-Marquardt Method

Gauss-Newton can diverge when the linearization is poor (far from the minimum) or when is singular. Levenberg-Marquardt (LM) adds a damping term:

The damping parameter interpolates between:

  • : Pure Gauss-Newton (fast convergence near minimum)
  • : Gradient descent with small step (safe far from minimum)

Trust Region Interpretation

LM is a trust region method: controls the size of the region where the linear approximation is trusted.

  • If the update reduces the cost: accept the step, decrease (expand trust region)
  • If the update increases the cost: reject the step, increase (shrink trust region)

Convergence Criteria

LM terminates when any of:

  • Cost threshold:
  • Absolute decrease:
  • Relative decrease:
  • Maximum iterations:
  • Parameter change:

Sparsity

In bundle adjustment problems, the Jacobian is sparse: each residual depends on only a few parameter blocks (one camera intrinsics, one distortion, one pose). The matrix has a block-arrow structure that can be exploited by sparse linear solvers:

  • Sparse Cholesky: Efficient for well-structured problems
  • Sparse QR: More robust when the normal equations are ill-conditioned

calibration-rs uses sparse linear solvers through the tiny-solver backend.

Cost Function vs. Reprojection Error

The optimizer minimizes the cost . The commonly reported mean reprojection error is:

These are related but not identical: the cost weights large errors quadratically, while the mean error weighs them linearly. calibration-rs reports both: the cost from the solver and the mean reprojection error computed post-optimization.

With Robust Losses

When a robust loss is used, the objective becomes:

The normal equations are modified to incorporate the loss function's weight:

where is the iteratively reweighted diagonal matrix. See Robust Loss Functions for details.