v9.1.1
Menu

Total Bundle Cost

mediumPROBundle AdjustmentOptimizationCV: Image Alignment and Stitching

You are given multiple observations (2D point measurements) of 3D points from different cameras, and need to compute the total bundle adjustment cost.

The total cost is the sum of squared reprojection errors across all observations:

C=∑i,j∥π(RjXi+tj)−xij∥2C = \sum_{i,j} \|\pi(R_j X_i + t_j) - x_{ij}\|^2

Where:

  • XiX_i is the i-th 3D point
  • Rj,tjR_j, t_j are the j-th camera's rotation and translation
  • xijx_{ij} is the observation of point i in camera j
  • π\pi is the pinhole projection with focal length ff: π([X,Y,Z])=(fXZ, fYZ)\pi([X, Y, Z]) = \left(f \frac{X}{Z},\ f \frac{Y}{Z}\right)

This is the objective function that bundle adjustment minimizes.

Example

Input
observations = [(0, 0, [0, 0])]
points = [[0, 0, 10]]
cameras = [[0, 0, 0]]
focal = 1
Output
0.0
Reasoning
  • Inputs: observations holds one measurement: camera 0 saw point 0 at pixel (0,0)(0, 0). points places point 0 at X0=(0,0,10)X_0 = (0, 0, 10) (on the optical axis); cameras gives camera 0 the translation t0=(0,0,0)t_0 = (0, 0, 0); focal is f=1f = 1.
  • Camera frame: with identity rotation, R0X0+t0=(0+0, 0+0, 10+0)=(0,0,10)R_0 X_0 + t_0 = (0+0,\ 0+0,\ 10+0) = (0, 0, 10).
  • Projection: divide by depth and scale by focal length: u=f⋅XZ=1⋅010=0u = f \cdot \frac{X}{Z} = 1 \cdot \frac{0}{10} = 0 and v=1⋅010=0v = 1 \cdot \frac{0}{10} = 0, so the predicted pixel is (0,0)(0, 0).
  • Residual: (0−0)2+(0−0)2=0(0 - 0)^2 + (0 - 0)^2 = 0. With a single observation the sum has one term, so C=0C = 0, returned as the float 0.0 (rounded to 4 decimal places).
  • Notice: a point at (10,10,10)(10, 10, 10) would project to (1,1)(1, 1), and an observation of it at (0,0)(0, 0) would add 12+12=21^2 + 1^2 = 2. Each observation adds its own squared pixel distance.

Constraints

  • observations: list of (camera_idx, point_idx, [u, v]) tuples
  • points: list of [X, Y, Z] for each 3D point
  • cameras: list of [tx, ty, tz] translation for each camera
  • focal: focal length
  • Assume identity rotation for all cameras
  • Return total cost rounded to 4 decimal places

Learn this in

More from CV: Image Alignment and Stitching

PRO

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test results

Sign in to run your code against the test cases.