Inputs:observations holds one measurement: camera 0 saw point 0 at pixel (0,0). points places point 0 at X0=(0,0,10) (on the optical axis); cameras gives camera 0 the translation t0=(0,0,0); focal is f=1.
Camera frame: with identity rotation, R0X0+t0=(0+0,0+0,10+0)=(0,0,10).
Projection: divide by depth and scale by focal length: u=f⋅ZX=1⋅100=0 and v=1⋅100=0, so the predicted pixel is (0,0).
Residual:(0−0)2+(0−0)2=0. With a single observation the sum has one term, so C=0, returned as the float 0.0 (rounded to 4 decimal places).
Notice: a point at (10,10,10) would project to (1,1), and an observation of it at (0,0) would add 12+12=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
Bundle Adjustment Cost Computation: Background and Strategy
Background Knowledge
Bundle Adjustment Fundamentals
Bundle adjustment is a nonlinear optimization technique that refines camera parameters and 3D point positions by minimizing reprojection errors. The core idea is that when you project a 3D point onto a camera's image plane using the camera's pose (rotation and translation), the projected location should match the actual observed 2D point. The difference between predicted and observed locations is the reprojection error. Bundle adjustment simultaneously optimizes all camera poses and 3D points to minimize the sum of these errors across all observations.
The Reprojection Error
The reprojection error for a single observation measures how well a 3D point, when transformed by a camera's pose and projected onto the image plane, matches the observed 2D measurement. Mathematically, this involves:
Transforming the 3D point using the camera's rotation matrix Rj and translation vector tj: RjXi+tj
Projecting this transformed point onto the image plane using the projection function π (typically perspective projection)
Computing the Euclidean distance between the projected point and the observed 2D point xij
The total cost function sums the squared reprojection errors across all point-camera pairs, which is standard in least-squares optimization.
Why Squared Errors?
Squaring the errors emphasizes larger deviations and makes the function differentiable everywhere, enabling efficient gradient-based optimization methods like Levenberg-Marquardt. This formulation is the foundation for modern SLAM and Structure from Motion systems.
Algorithm/Approach
The solution follows a straightforward summation pattern:
Iterate over all observations (each pair of camera j and 3D point i)
Transform the 3D point using the camera's pose
Project the transformed point onto the image plane
Compute the reprojection error (difference between projected and observed 2D points)
Accumulate the squared error into the total cost
This is a direct computation problem—you're evaluating the objective function at given parameter values, not optimizing it. The key is correctly implementing the geometric transformations.
Step-by-Step Strategy
Step 1: Understand the Input Structure
Identify how 3D points, camera poses (rotation and translation), and 2D observations are represented
Determine the format of rotation matrices (3×3 matrices or other parameterizations like quaternions)
Understand the projection function π (typically perspective projection: π(p)=\frac{1}{p_z}[px,py]T for a point p=[px,py,pz]T)
PRO
Continue the full explanation
You're reading the free preview. Unlock the complete walkthrough, the code editor, test runner and reference solution with Premium.
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.