Exercise: the pinhole camera and calibration
Before this unit: Day 52: 3D graphics.
See also: Day 24: Keypoints and descriptors; Day 21: Hough transform; Day 23: FFT registration.
Goal
By the end of this exercise you should be able to:
- project a 3D point to a pixel by hand, and turn a pixel back into a ray,
- convert a focal length in millimetres into pixels and compute a field of view,
- say what radial distortion does to a point, and recognise barrel and pincushion,
- count equations and unknowns for the DLT and for planar calibration, and name the degenerate setups,
- read a picture of reprojection residuals: noise, or a missing model term?
- (math) derive the DLT equations, the camera centre and the planar calibration constraints.
Do the pen-and-paper part first, then the two demos.
Background
| Idea | What it means |
|---|---|
| Pinhole | u = f X/Z + c_x, v = f Y/Z + c_y in camera coordinates (z forward, y down) |
| Focal length in pixels | focal length / pixel pitch, e.g. 8 mm / 10 µm = 800 px |
| Intrinsics K | f_x, f_y, principal point (c_x, c_y), skew; fixed while zoom and focus are fixed |
| Extrinsics | rotation R, translation \mathbf t: camera coordinates = R\mathbf X + \mathbf t; centre \mathbf c = -R^\top\mathbf t |
| Camera matrix | P = K[R \mid \mathbf t], 3 \times 4, 11 degrees of freedom |
| Pixel → ray | from \mathbf c in the direction R^\top K^{-1}(u, v, 1)^\top |
| Radial distortion | r \to r(1 + k_1 r^2 + k_2 r^4) in normalised coordinates; k_1 < 0 barrel, k_1 > 0 pincushion |
| DLT | 2 linear equations per point in the 12 entries of P; \ge 6 points, not all on one plane |
| Homography | image of a plane, 3 \times 3, 8 degrees of freedom, \ge 4 points; planar calibration: 2 equations on K per pose |
| Reprojection error | pixel distance between the seen and the predicted position of a known point |
By hand
Unless stated otherwise: f = 800 px, (c_x, c_y) = (320, 240), no skew, an image of 640 \times 480 pixels, and the camera at the world origin looking down +z (R = I, \mathbf t = 0).
B1. Project the points (0.5, -0.25, 5), (0.5, -0.25, 10) and (0.4, 0.3, 2) (metres) to pixels. As a point moves away from the camera along a straight line through the camera centre, where does its image go? Which pixel never moves when only Z changes?
B2. The pixel (400, 200). (a) Compute K^{-1}(400, 200, 1)^\top. (b) Which 3D points land on this pixel? Check that one of your points from B1 is among them.
B3. (a) The horizontal and vertical field of view of this camera. (b) A camera has an 8 mm lens and pixels of 3.45 µm. What is f in pixels? (c) With 2 × 2 binning (pixels of 6.9 µm, an image half as wide), what happens to f in pixels and to the field of view?
B4. Radial distortion with k_1 = -0.1, k_2 = 0. (a) A point at normalised radius r = 0.4: where does it go? Barrel or pincushion? (b) How many pixels is that with f = 800? (c) The same with k_1 = +0.1. (d) Why does distortion matter more in the corners of the image than in the centre?
B5. Counting. (a) How many equations do 5 points give for the DLT? 6 points? How many unknowns are there? (b) Why do 6 points on one plane not determine P, even though there are enough equations? (c) How many points determine a homography? (d) Planar calibration gives 2 equations on K per pose. How many poses are needed for a K with 5 unknowns? With zero skew assumed?
B6. Four calibrations, described by their residual arrows (observed → predicted). For each: random noise, or a model problem? Which one? (a) Arrows in random directions, RMS 0.3 px. (b) All arrows point radially outwards, longest in the image corners, about 3 px there. (c) 71 arrows of about 0.3 px in random directions, and one arrow of 12 px. (d) A second calibration of the same camera after the lens was refocused gives a focal length 2 % larger.
B1. (0.5, -0.25, 5): u = 800 \cdot 0.5/5 + 320 = 400, v = 800 \cdot (-0.25)/5 + 240 = 200. (0.5, -0.25, 10): (360, 220). (0.4, 0.3, 2): (480, 360). Moving away along a line through the camera centre the image does not move at all (it is the same ray); moving away along a line parallel to the optical axis, as from Z = 5 to Z = 10 here, the image slides towards the principal point (320, 240), which is the only pixel that never moves.
B2. (a) \big((400 - 320)/800, (200 - 240)/800, 1\big) = (0.1, -0.05, 1). (b) All points s \cdot (0.1, -0.05, 1) with s > 0 (a ray from the origin). s = 5 gives (0.5, -0.25, 5), the first point of B1.
B3. (a) Horizontally 2\arctan(320/800) = 43.6°, vertically 2\arctan(240/800) = 33.4°. (b) 8\ \text{mm} / 3.45\ \mu\text{m} = 2319 px. (c) f halves to 1159 px, the image is half as wide in pixels, so the field of view is unchanged: the same sensor and lens see the same scene, only with coarser pixels.
B4. (a) 0.4 \cdot (1 - 0.1 \cdot 0.16) = 0.3936: inwards, barrel. (b) 800 \cdot 0.0064 = 5.1 px. (c) 0.4 \cdot 1.016 = 0.4064, outwards: pincushion. (d) The displacement is k_1 r^3: it grows with the cube of the distance from the centre, so it is tiny near the centre and largest in the corners.
B5. (a) 10 and 12 equations; P has 12 entries but only 11 unknowns (the scale is free), so 5 points are too few and 6 are enough. (b) If all points lie on one plane, the part of P that multiplies the coordinate perpendicular to that plane never appears in the equations: it can be anything, so a whole family of P fits. (c) 4 points (8 equations for 8 unknowns). (d) 3 poses (3 \cdot 2 = 6 \ge 5); with zero skew, 4 unknowns, so 2 poses.
B6. (a) Noise: a good calibration. (b) A pattern: a radial distortion that the model does not include (or underestimates); add k_1. (c) One outlier: a wrongly detected corner; remove it, then the RMS drops to about 0.3 px. (d) A model change: focusing moves the lens and changes the focal length (and distortion); the intrinsics are only fixed while zoom and focus are fixed.
Math questions
B7. (math) From \mathbf x \times P\mathbf X = 0 with \mathbf x = (u, v, 1)^\top and the rows \mathbf p_1^\top, \mathbf p_2^\top, \mathbf p_3^\top of P, derive the two equations of the DLT. Why is the third component of the cross product not a new equation?
B8. (math) Show that \mathbf c = -R^\top\mathbf t satisfies P\binom{\mathbf c}{1} = 0. Why is the centre the only point that does not project to a pixel?
B9. (math) For a plane at Z = 0, show that H = K[\mathbf r_1\ \mathbf r_2\ \mathbf t] and derive the two constraints \mathbf h_1^\top\omega\,\mathbf h_2 = 0 and \mathbf h_1^\top\omega\,\mathbf h_1 = \mathbf h_2^\top\omega\,\mathbf h_2 with \omega = K^{-\top}K^{-1}.
B7. P\mathbf X = (\mathbf p_1^\top\mathbf X, \mathbf p_2^\top\mathbf X, \mathbf p_3^\top\mathbf X)^\top. The cross product with (u, v, 1): \begin{pmatrix} v\,\mathbf p_3^\top\mathbf X - \mathbf p_2^\top\mathbf X \\ \mathbf p_1^\top\mathbf X - u\,\mathbf p_3^\top\mathbf X \\ u\,\mathbf p_2^\top\mathbf X - v\,\mathbf p_1^\top\mathbf X \end{pmatrix} = \mathbf 0. The first two rows are linear in \mathbf p = (\mathbf p_1, \mathbf p_2, \mathbf p_3): [\mathbf 0^\top, -\mathbf X^\top, v\mathbf X^\top]\,\mathbf p = 0 and [\mathbf X^\top, \mathbf 0^\top, -u\mathbf X^\top]\,\mathbf p = 0. The third row is -u times the first minus v times the second, so it adds nothing.
B8. P\binom{\mathbf c}{1} = K(R\mathbf c + \mathbf t) = K(-RR^\top\mathbf t + \mathbf t) = K(-\mathbf t + \mathbf t) = 0. A point projects to a pixel only if P\binom{\mathbf X}{1} has a non-zero third entry; the zero vector is no pixel at all (it is the “ray of all rays”). P has rank 3, so its null space is one-dimensional: the centre is the only such point.
B9. For \mathbf X = (X, Y, 0, 1): [\mathbf r_1\ \mathbf r_2\ \mathbf r_3\ \mathbf t]\mathbf X = X\mathbf r_1 + Y\mathbf r_2 + \mathbf t = [\mathbf r_1\ \mathbf r_2\ \mathbf t](X, Y, 1)^\top, so H \simeq K[\mathbf r_1\ \mathbf r_2\ \mathbf t]. Hence \mathbf h_1 = \lambda K\mathbf r_1, \mathbf h_2 = \lambda K\mathbf r_2 with one common factor \lambda, i.e. \mathbf r_k = K^{-1}\mathbf h_k/\lambda. The columns of a rotation are orthonormal: \mathbf r_1^\top\mathbf r_2 = 0 gives \mathbf h_1^\top K^{-\top}K^{-1}\mathbf h_2 = 0, and \mathbf r_1^\top\mathbf r_1 = \mathbf r_2^\top\mathbf r_2 gives \mathbf h_1^\top\omega\,\mathbf h_1 = \mathbf h_2^\top\omega\,\mathbf h_2 (\lambda^2 cancels). Both are linear in the 6 entries of the symmetric \omega.
Explore: the pinhole camera
A scene (a checkerboard floor of 2 m × 2 m, a box, three posts) seen from outside (left, drag to turn) and through the camera (right, 640 × 480 pixels). The camera always looks at the same point of the scene; sliders set the focal length, principal point, skew, its position (azimuth, elevation, distance), roll and distortion. Click a pixel to see its ray in 3D.
D1. At the start (f = 600 px), read the horizontal field of view. Check it with 2\arctan(320/f). Then double f: what happens to the field of view and to the image?
D2. Compare zooming and walking: set f = 1200 (the box gets bigger). Reset, and instead halve the distance. Describe the difference between the two pictures (look at the posts and the far edge of the board).
D3. Preset long lens from far away: f = 1500 px at distance 7.5. The board has about the same size as at the start. What changed? Which situation in microscopy is the preset nearly orthographic like?
D4. Preset a point behind the camera and tick the box. Where do the red crosses appear, and why is it wrong to draw them?
D5. Click the pixel (c_x, c_y) (the red circle). Read K^{-1}(u, v, 1). Where does that ray point? Move the principal point with the sliders and click it again.
D1. 2\arctan(320/600) = 56.1°, as shown. At f = 1200: 2\arctan(320/1200) = 29.9°; the image shows a smaller part of the scene, magnified 2×, without any change in perspective.
D2. Zooming scales the whole image: near and far things grow equally, the posts keep their lean. Walking closer makes near things grow more than far things: the near posts become much larger than the far ones and lean outwards more, the board’s far edge shrinks relative to the near edge. Perspective depends on where the camera is, not on the lens.
D3. The perspective flattens: the posts are nearly parallel, near and far squares of the board have almost the same size. That is a telecentric (orthographic) view, as in many microscopes: magnification does not depend on depth, so the camera is affine.
D4. Points behind the camera have Z < 0; dividing by a negative depth flips them through the principal point to the other side of the image, where they would appear as if in front. Every projection must check Z > 0 first (the graphics pipeline clips before the division for the same reason).
D5. K^{-1}(c_x, c_y, 1) = (0, 0, 1): straight along the optical axis, wherever the principal point is. Moving the principal point shifts the whole image; the ray of the principal point stays the optical axis.
Explore: calibrating a camera
A hidden camera (f = 820 px, principal point (335, 245), 640 \times 480 pixels) photographs a known target with seeded pixel noise and, optionally, radial distortion. 3D target: two checkerboard walls at right angles, 72 corners; click corners to leave them out. Flat board: 8 × 6 corners in 1–5 poses. Linear = the DLT (or homographies → K); refine = minimise the reprojection error (optionally with k_1, k_2). Red arrows: residuals, magnified. The bars are the singular values of the linear system.
E1. Preset 6 points, no noise. Is the estimate exact? Look at the smallest singular value. Now set the noise to 0.5 px (same 6 points). How far off is f_x, and what is the RMS reprojection error? Why is a small reprojection error no guarantee here?
E2. Preset noise 0.5 px, all 72 points. Read f_x, c_x, c_y and the RMS error. Press refine ×20. Does it change much? Compare the RMS with 0.5\sqrt 2.
E3. Preset unnormalised DLT. Compare the condition number and the estimate with the normalised one (tick the box). Which matters more here?
E4. Preset all points on one plane. How many singular values are (nearly) zero? Why can no K be found?
E5. Preset distortion ignored. Describe the residual pattern. Press refine ×20 without and then with refine k1, k2 too. Compare f_x, c_y, k_1 and the RMS each time.
E6. Preset flat board: 1, 2, 3 views. Set the number of views to 1, 2, 3 and 5. When does a K appear, and how close is it to the truth? Refine at 3 views.
E7. Preset parallel views. What happens at 2, 3 and 5 views? Explain with a sentence about focal length and distance.
E1. Exact: f_x = 820.0, the twelfth singular value is 0 (an exact null vector). With 0.5 px noise and the same 6 points: f_x = 759.3 (60 px off), yet the RMS reprojection error is only 0.25 px. Six points give 12 equations for 11 unknowns, so the model can nearly fit the noise itself; the small residual shows a good fit, not a good camera. Many points spread over the image are needed.
E2. Linear: f_x = 819.7, (c_x, c_y) = (332.5, 247.2), RMS 0.718 px. After refining: f_x = 820.1, RMS 0.720 px (slightly higher, because the refinement holds the skew at 0 while the DLT had a small non-zero skew). 0.5\sqrt 2 = 0.71: the residuals are as large as the noise, as they should be.
E3. Condition number 2.9 \cdot 10^4 unnormalised against 8.3 normalised; the estimates agree here (f_x = 819.6 against 819.7), because double precision copes with 10^4. The conditioning matters for larger images, more noise, or single-precision arithmetic; normalising costs nothing, so always do it.
E4. Three are zero, and a fourth is small (it would be zero without noise): the columns of P that multiply the coordinate perpendicular to the plane are undetermined. Any P in a 4-dimensional family fits, so no meaningful K can be extracted (the table shows nonsense or nothing).
E5. The arrows form a radial pattern, growing towards the edges. Linear: f_x = 954.7, c_y = 300.6, RMS 1.08 px. Refined without distortion: f_x = 957.2, RMS 1.08 px: the pinhole model cannot explain bent lines, so it compensates with a wrong focal length. With k_1, k_2: f_x = 819.8, c_y = 250.8, k_1 = -0.275 (true -0.3), RMS 0.43 px (\approx 0.3\sqrt 2).
E6. 1 view: only 3 equations for 5 unknowns (with the zero-skew row): no K. 2 views: f_x = 840 (zero skew assumed). 3 views: f_x = 832.6, and 833.3 after refinement; 5 views: 830.2. Within about 2 % of 820: with 0.5 px noise and moderate tilts, the focal length of a planar calibration is uncertain by about that much; more and more strongly tilted views help.
E7. 2 views: f_x \approx 3100; 3 views: no valid K at all; 5 views: f_x \approx 7300. Seen face-on, a board at distance Z photographed with focal length f looks the same as one at 2Z with 2f: the images cannot separate f from the distance. Tilting the board breaks the tie, because foreshortening depends on f.
Code
Write the algorithms of this unit yourself, in Rust. Setup, and how to run and check your code: coding exercises: getting started. Your code goes in multiview/src/cameras/exercise.rs. The conventions are those of the lecture: the camera looks down +z, x right, y down.
C1. project(cam, X): the pixel of a world point, or nothing if it is behind the camera:
(X, Y, Z) = R · X + t
if Z ≤ 0: not visible
u = f X / Z + cx, v = f Y / Z + cy
The checks are B1, and a rotated camera compared with P = K[R \mid \mathbf t].
C2. pixel_ray(cam, u, v): the centre \mathbf c = -R^\top\mathbf t and the direction R^\top\big((u - c_x)/f,\ (v - c_y)/f,\ 1\big). The checks are B2, and that points along the ray of a rotated camera project back onto the pixel.
C3. distort(x, y, k1, k2) and undistort(xd, yd, k1, k2): radial distortion of normalised coordinates, and its inverse by fixed-point steps:
distort: r² = x² + y², return (1 + k1 r² + k2 r⁴) · (x, y)
undistort: (x, y) = (xd, yd)
repeat 20 times:
r² = x² + y²
(x, y) = (xd, yd) / (1 + k1 r² + k2 r⁴)
The checks are B4 (a) and (c), and that undistort undoes distort.
C4. dlt(points, pixels): the camera matrix from n \ge 6 known points (slide “The math: the DLT”):
T, U = normalising transforms of the pixels and of the world points
for every point: X̂ = U X (4 entries), (û, v̂) = T (u, v)
add the rows ( 0ᵀ, −X̂ᵀ, v̂ X̂ᵀ ) and ( X̂ᵀ, 0ᵀ, −û X̂ᵀ ) to A
p̂ = the unit vector with the smallest |A p̂| # eigenvector of AᵀA, smallest eigenvalue
P̂ = p̂ as 3 rows of 4; P = T⁻¹ P̂ U
The normalising transforms are given. The check: from exact pixels of a hidden camera, with 72 points and with 6, every point reprojects exactly and P is the true matrix up to scale.
C5. decompose(P): split P into K, R and \mathbf t. The left 3\times3 block is M = KR; take its rows apart from the bottom up:
if det M < 0: P = −P
for i = 2, 1, 0:
rest = row i of M
for j > i: K[i][j] = (row i of M) · R_j, rest = rest − K[i][j] R_j
K[i][i] = |rest|, R_i = rest / K[i][i]
scale K and the last column of P so that K[2][2] = 1; t = K⁻¹ · (last column)
The check recovers f = 820, (c_x, c_y) = (335, 245), R and \mathbf t from -3P.
Then cargo run -p multiview -- cameras and open the pictures in out/cameras/. The program photographs 72 corners on two walls at right angles with a hidden camera (f = 820, principal point (335, 245)), calibrates it with your DLT in several situations and prints K and the RMS reprojection error. residuals-noise.png and residuals-distortion.png show the residual arrows (from the seen corner to where the estimated camera puts it, 30 times longer). It also distorts a checkerboard picture with k_1 = -0.3 and undistorts it again (checker-barrel.png, checker-undistorted.png).
C6. Compare the calibrations from 6 points (with and without noise) and from 72 points with noise. Six noisy points give a smaller RMS error than 72; why is that calibration still the worse one? What RMS do you expect for 72 points with noise \sigma = 0.5 px (slide “The math: the reprojection error”)?
C7. Compare the two residual pictures and the printed K. Which one shows noise, and which a missing model term? What does the DLT do with the distortion it cannot model?
C8. What happens with the 36 points on one wall? Explain with B5 (b).
The model solution is multiview/src/cameras/solution.rs in the download.
C6. Six exact points give the exact camera (f = 820.0, RMS 0). With 0.5 px noise the six points give f_x = 841.9, a principal point (354.2, 270.2) and a skew of -4.5, with an RMS of only 0.29 px: 12 equations for 11 unknowns, so the camera bends to fit the noise of these few points almost exactly. The small error measures the fit, not the camera. 72 points average the noise out: f_x = 819.7, f_y = 820.6, principal point (334.1, 246.4), RMS 0.73 px, close to \sigma\sqrt2 = 0.71 px.
C7. residuals-noise.png: short arrows in random directions, neighbours unrelated: noise. residuals-distortion.png (noise only 0.1 px): the arrows form a smooth pattern, neighbours point the same way, and they are longest at the outer corners of the target (top left, right): a model term is missing. The RMS is 0.81 px, eight times the noise. The DLT absorbs as much of the distortion as a pinhole camera can: f_x = 945 instead of 820 and a principal point (347, 179) far from (335, 245). checker-barrel.png shows the same distortion on a picture: straight edges bow outwards, most at the corners; checker-undistorted.png is straight again (black where the distorted picture had no data).
C8. The calibration breaks down: the printed K is “NaN” (not a number), nonsense. All points have Z = 0, so the third column of P never enters the equations: A has a whole family of null vectors (4 dimensions), and the one returned has nothing to do with the camera. Its left 3\times3 block is singular, so there is no K to extract.
Where this leads
The calibrated camera turns pixels into rays. The next unit takes two cameras: a point in one image constrains its match in the other to a line, and two matched rays give a 3D point. The reprojection error comes back as the objective of bundle adjustment, which refines many cameras and thousands of points at once.