Week 04 · Camera geometry and calibration
Use reprojection error to diagnose an incorrect pinhole camera.
← Week 3 · Subject contents · Week 5 →
Use reprojection error to diagnose an incorrect pinhole camera.
Work through the problem
With camera-frame point (X,Y,Z), a pinhole model gives u=fₓX/Z+cₓ and v=fᵧY/Z+cᵧ. Points farther away project closer to the principal point for the same lateral displacement. A focal length of 800 pixels, X=1, Z=4, and cₓ=320 gives u=520 pixels.
When camera-frame points are known, regress u against X/Z and v against Y/Z to recover focal lengths and offsets. This simplified lab assumes known pose and no lens distortion. In full calibration, estimate more parameters from several views and inspect reprojection errors on held-out points.
Run the Python lab
import numpy as np
p = np.array([[1.,0,4],[0,1,5],[-1,-1,2],[2,-1,6],[.5,2,7],[1,2,3]])
xy = p[:,:2]/p[:,2:]
pixels = xy*[800,780]+[320,240]
fit = [np.linalg.lstsq(np.c_[xy[:4,i],np.ones(4)],pixels[:4,i],rcond=None)[0] for i in range(2)]
for name, params in [("Fit",np.array(fit)),("Bad",np.array([[700,320],[700,240]]))]:
projection = xy[4:]*params[:,0]+params[:,1]
rmse = np.sqrt(np.mean(np.sum((projection-pixels[4:])**2,axis=1)))
print(name,params.round(1).tolist(),"RMSE pixels",round(rmse,3))
assert np.allclose(fit,[[800,320],[780,240]])
Output from the lab
Fit [[800.0, 320.0], [780.0, 240.0]] RMSE pixels 0.0
Bad [[700, 320], [700, 240]] RMSE pixels 47.587
Practice
- Why can a single point at the principal axis tell you little about focal length?
Check your reasoning
- If X=Y=0, the projection stays at (cₓ,cᵧ) regardless of focal length.
← Week 3 · Subject contents · Week 5 →
Further reading · Szeliski · Computer Vision: Algorithms and Applications