A camera is a 3 by 4 matrix. It sends each 3D point to a pixel and throws the depth away. Photograph a flat checkerboard from two or more angles, and you can recover the camera's hidden numbers.
Choose how many photos of the board to take. Watch the recovered focal lengths and image centre close in on the truth. Then we build, piece by piece, the pinhole, the matrix K, the pose, lens distortion, calibration and the ray back out.
You need a matrix times a vector and a little Python. We build the rest from zero.
Lesson 8 of 15 in our follow-along of Stanford AA 274A, Principles of Robot Autonomy I. Every idea is re-taught here in our own words and devices. Before: Lesson 7, Sensors. Next: Lesson 9, Features and SfM.
Photos of the board
Illustrative synthetic camera: fx 520, fy 500, cx 330, cy 235 px, no skew, a 640 by 480 image. The board has 9 by 6 corners, 30 mm apart. The solver is Zhang's method from Chapter 7, with zero skew assumed.
Chapter 0
Slide two points along one line of sight and watch them land on the same pixel
Hold up your thumb at arm's length. Now bring it close to your eye. It can cover the same patch of view both times. A photo of it cannot tell you which.
A camera records the direction to each point, not its distance. Every point on one line from the lens out to infinity lands on the same pixel. That line is a ray. The depth along it is gone.
The near and far points sit on one ray through the pinhole. Slide each one. Watch where each lands on the image plane.
Illustrative toy: focal length 500 px, ray slope 0.3 (the point rises 0.3 m for each metre of depth).
Work the number. A soda can 0.12 m tall stands 0.4 m away. A billboard 9 m tall stands 30 m away. Both have height over depth 0.3. With a focal length of 500 px, each is 500 × 0.3 = 150 px tall in the image.
A robot needs both directions of this map. Forward: where does a 3D point land in the image? Backward: what does a pixel say about the world? This lesson builds both, then measures the camera's own numbers by calibration.
The symptom is a one-camera robot that misjudges range: late for a big object far away, early for a small one close by. To measure it, compare its range guess with a LiDAR or a tape. The fix is extra information: a second view, motion or a known size (Chapter 8).
Chapter 1
Drag a point in front of a pinhole and watch its ray cross the image plane
Light through a tiny hole in a dark box paints an image on the far wall. That box is the pinhole camera: no lens, one point that every ray passes through.
Put the origin at the pinhole, the camera centre. The camera looks along its Z axis, the optical axis. X points right and Y points down, so images count rows from the top. The real image forms behind the hole, upside down. We draw an equivalent upright image plane in front of the hole, at a distance called the focal length.
Drag the pink point up, down, nearer or farther. Then change the focal length and watch the image plane move.
Illustrative toy. We draw the image plane to scale with f: 400 px sits at 0.8 of the way to the first tick.
Where does the ray cross the plane? Call the point's height Y and its depth Z, in metres, in the camera's frame. Call the focal length f. Call the crossing height on the image plane y. Two triangles share the angle at the pinhole, so y / f = Y / Z. From above, the same rule links the sideways offset X to its image coordinate x.
x equals f times capital X, divided by capital Z.
Divide the offset X by the depth Z. That ratio is the direction of the point. Multiply it by f to get x. The same rule gives y from Y.
Take f = 500 and a point at X = 2, Y = 1, Z = 5. Then x = 500 × 2 / 5 = 200 and y = 500 × 1 / 5 = 100. Move the point to (4, 2, 10), twice as far on the same ray: x = 200 and y = 100 again.
The divide by Z is perspective. Double the depth and the image shrinks by half. It is also the step that loses depth: only the ratios X / Z and Y / Z survive.
The symptom is a ghost: a point behind the camera shows up in the image. With Z = −5, the formula still returns a pixel, mirrored. To measure it, count points with Z ≤ 0 that your code projects. The fix is a guard: reject every point with Z ≤ 0 before you divide.
Chapter 2
Turn the four knobs of K and watch a grid of points move in the image
The pinhole gives x and y in metres from the optical axis. A camera reports pixel coordinates, counted from the top-left corner. Two changes bridge the gap.
Call the pixel column u and the pixel row v. Call the focal length across, in pixels, fx, and the column of the principal point cx. X and Z are the point's offset and depth in the camera frame, as before.
u equals f-x times X over Z, plus c-x.
Take the direction X / Z, scale it by fx, then shift it by cx. The row works the same way: v = fy × Y / Z + cy.
A 640 by 480 camera with fx = fy = 500 and (cx, cy) = (320, 240). The point (2, 1, 5) gives u = 500 × 0.4 + 320 = 520 and v = 500 × 0.2 + 240 = 340. It lands right of and below the centre, as X > 0 and Y > 0 say it should.
The five numbers fx, fy, cx, cy and the skew γ go in one 3 by 3 matrix, the intrinsic matrix K. Skew couples u to Y when the pixel rows are not square to the columns. On real cameras it is about 0.
Read K by rows. Row one scales X by fx and shifts it by cx. Row two does the same for Y. Row three keeps the depth for the divide in Chapter 4. These are intrinsic numbers: they belong to the lens and sensor, so the camera carries them wherever it goes.
Nine pink points sit on a grid 5 m in front of the camera. The yellow point is (2, 1, 5). Move each knob and watch the grid stretch or slide.
Illustrative toy. Grid points at X = −1.5, 0, 1.5 m and Y = −1, 0, 1 m, all at Z = 5 m. Points that fall off the 640 by 480 frame turn red.
The symptom is a small, steady error in every direction the robot computes. A common cause is to assume the principal point is the exact image centre. The real one can sit several pixels away. To measure it, calibrate and compare (cx, cy) with (320, 240). The fix is to use the calibrated values.
Chapter 3
Orbit a camera around a point and read the point in the camera's own frame
So far each point came in the camera's frame. A robot's map, its goals and a checkerboard on the wall live in a fixed world frame. Before we project a world point, we must say where it is relative to the camera. That needs the camera's pose: which way it faces and where it stands.
Call the point's coordinates in the world frame PW and in the camera frame PC. Call the 3 by 3 rotation matrix R: it turns world directions into camera directions. Call the 3-vector t the translation: the world origin, written in camera coordinates.
P-C equals R times P-W, plus t.
Turn the world point P-W into the camera's axes with R. Then add t to slide it so the camera centre is the origin. The result P-C is the point as the camera sees it.
A camera stands at world (4, 0, 0) and faces down the world's −X axis. Its rows are R = [(0, 0, 1), (0, 1, 0), (−1, 0, 0)] and t = (0, 0, 4). Take P-W = (3, 1, 0). R times P-W = (0, 1, −3). Add t: P-C = (0, 1, 1). The point is 1 m in front of the camera, centred, 1 m low.
Seen from above. The teal camera stays 4 m from the world origin and faces it. Turn the orbit, and drag the pink point. The read-out gives the point in the camera frame.
Illustrative toy in the world X and Z plane, with Y = 0. At 90° the camera is the worked example: centre (4, 0, 0), facing −X.
R and t are the camera's extrinsic parameters. They change every time the camera moves. K does not. Spin a camera on a tripod around a table: K stays fixed and [R | t] changes for every shot.
The symptom is a camera that seems to sit in the wrong place, often mirrored. The usual cause is to read t as the camera's position. The camera centre in world coordinates is C = −R⊤t. In the example that gives (4, 0, 0), not (0, 0, 4). To measure it, compute −R⊤t and compare it with where the camera stands. The fix is to name every pose by its two frames, for example "world to camera".
Chapter 4
Slide a point along its ray and watch three numbers grow while the pixel stays put
The pixel rule divides by Z, and a matrix can only multiply and add. So we use homogeneous coordinates: give each point one extra number. Scaling all the numbers by any nonzero amount names the same point. The pixel (u, v) becomes (u, v, 1), which equals (2u, 2v, 2).
Multiply K by the camera-frame point (X, Y, Z). Call the three results a, b and w. The bottom row of K copies the depth, so w = Z.
The pixel u, v equals a over w, and b over w.
Multiply K by the point to get a, b and w. Divide a and b by the last entry w. The matrix did the linear part; the divide did the perspective.
Take K with fx = fy = 500 and (320, 240), times (2, 1, 5). Then a = 1,000 + 1,600 = 2,600, b = 500 + 1,200 = 1,700 and w = 5. So u = 2,600 / 5 = 520 and v = 1,700 / 5 = 340, as in Chapter 2.
The point moves along the ray through (2, 1, 5). Then drag the depth below zero.
The camera is K with fx = fy = 500 and (cx, cy) = (320, 240). The point is (0.4 Z, 0.2 Z, Z).
Along one ray, a, b and w all scale by one number, and the divide cancels it. A pixel names a ray, not a point.
Now chain the pose. Call the world point with an extra 1 P̃W, and the homogeneous pixel p̃ = (a, b, w). Put R and t side by side as one 3 by 4 block [R | t].
p-tilde equals K times R-bar-t, times P-tilde-W.
First [R | t] moves the world point P-tilde-W into the camera frame. Then K turns it into a homogeneous pixel p-tilde. Divide by w. The product P = K[R | t] is the projection matrix, 3 by 4.
Use K from above and the pose from Chapter 3. P has rows (−320, 0, 500, 1280), (−240, 500, 0, 960) and (−1, 0, 0, 4). The world point (−1, 1, 0, 1) gives p-tilde = (1600, 1700, 5). Divide by 5: pixel (320, 340).
P has 12 entries, but its scale cancels in the divide: 11 are free. That matches 5 in K, 3 angles in R and 3 in t.
The symptom is a phantom pixel. The world point (6, 1, 0) gives p-tilde = (−640, 20, −2), and the divide returns (320, −10) for a point behind the camera. To measure it, log w before the divide. The fix is to keep only points with w > 0.
Chapter 5
Bend a straight grid into a barrel or a pincushion with one number
A pinhole sends light in straight lines. A real lens bends it, most of all near the edges of the image. Straight lines in the world then show up as curves. This is radial distortion: each pixel moves toward or away from a centre, along the radius.
| Kind | What a pixel does | A square looks | Typical lens |
|---|---|---|---|
| Barrel | moves inward, more at the edge | bulging out, like a barrel | wide angle, fisheye |
| Pincushion | moves outward, more at the edge | pinched in at the sides | telephoto |
Name the parts. The distortion centre (uc, vc) is usually the principal point. The ideal pinhole pixel is (u, v). Its squared distance from the centre is r² = (u − uc)² + (v − vc)². The distortion factor is k. The pixel the camera records is (ud, vd).
u-d equals one plus k r-squared, times u minus u-c, plus u-c.
Take the offset u minus u-c. Scale it by 1 + k r-squared, which grows with distance from the centre. Add u-c back. The row v works the same way.
Centre (320, 240), k = 1.0 × 10−6, ideal pixel (620, 440). The offsets are 300 and 200, so r² = 90,000 + 40,000 = 130,000. The scale is 1 + 0.13 = 1.13. So u-d = 1.13 × 300 + 320 = 659 and v-d = 1.13 × 200 + 240 = 466: 46.9 px outward, a pincushion. With k = −1.0 × 10−6 the scale is 0.87 and the pixel moves in to (581, 414).
The faint grid is the ideal pinhole image. The blue grid is what the lens records. Drag k below zero for barrel, above zero for pincushion.
Centre (320, 240) in a 640 by 480 image. The yellow dot follows the ideal pixel (620, 440). One-parameter model; real calibrations often add a second factor for r to the fourth.
A pixel near the centre barely moves. (330, 250) has r² = 200, so with k = 1.0 × 10−6 it moves by 0.003 px. Distortion lives at the edges. To undo it, you invert the scale. There is no closed form, so you iterate a few times or use a lookup table.
The symptom is good results near the image centre and bad ones near the corners. A ray from an uncorrected corner pixel points the wrong way. To measure it, photograph a straight edge near the border and check how far its points stray from a line. The fix is to calibrate k and undistort every pixel before you use it.
Chapter 6
Add known 3D points one by one and watch the recovered camera settle
A new camera comes with rough numbers for K at best. Calibration measures them from correspondences: known 3D points paired with their pixels. A classic target is two checkerboard walls at right angles.
Write the unknown 3 by 4 matrix P by rows: m1, m2, m3. A point P̃W seen at (u, v) obeys u = (m1 · P̃W) / (m3 · P̃W). Multiply out the bottom: u (m3 · P̃W) − m1 · P̃W = 0, and the same for v with m2. Both are linear in the 12 entries of P. This is the direct linear transformation (DLT).
Stack the 12 entries in a column m, and two rows per point into A, 2n by 12. With noise, only m = 0 solves A m = 0, and it means nothing. So fix the length of m at 1 and make A m as small as you can.
m-star is the m that makes the length of A times m smallest, with the length of m held at one.
Among all m of length 1, keep the one that A sends closest to zero. That is the last right singular vector of A, the direction A stretches least.
A tiny case: A has rows (3, 0), (0, 1) and (0, 1). The unit vector m = (1, 0) gives A m = (3, 0, 0), squared length 9. The unit vector m = (0, 1) gives (0, 1, 1), squared length 2. The SVD returns (0, 1), the smaller of the two.
P has 11 free numbers and each point gives 2 equations: at least 6 points. P's left 3 by 3 block equals K R, and an RQ decomposition splits it into that upper-triangular K and rotation R. Then t = K−1 times P's fourth column.
Blue dots: corners in the fit. Purple crosses: where the recovered camera puts them. Rings: 10 corners kept back to check. Add points, raise the noise, then use one wall.
Illustrative toy: a true camera with fx = fy = 500, (320, 240), turned 20° and 6 m from a two-wall rig of 50 corners. The solver is the DLT above, with the data scaled first for stable numbers.
The fit error is the pixel gap on the fitted points. The check error is the gap on points the fit never saw. With 1 px noise, a typical run looks like this:
| Points used | Fit error | Check error |
|---|---|---|
| 8 | 0.7 px | 2.6 px |
| 20 | 1.2 px | 0.9 px |
| 40 | 1.3 px | 0.5 px |
The symptom is a wild camera from points that all lie on one wall. A plane leaves many m that fit equally well. To measure it, compare the two smallest singular values of A: if they are close, nothing pins the answer. Use a rig with depth, or Zhang's method.
Chapter 7
Guess the focal length and watch the board's two axes bend away from a right angle
A two-wall rig is hard to build; a printed checkerboard is easy. Zhang's method (Zhengyou Zhang, 2000) calibrates from several photos of one flat board at different tilts. OpenCV's calibrateCamera uses it for flat targets.
Put the board on the world plane ZW = 0. The third column of R then multiplies zero and drops out. Each photo becomes a 3 by 3 homography H that sends board points (X, Y, 1) to pixels. Up to scale, H = K [r1 r2 t]. Here r1 and r2 are the board's axes in the camera frame. The DLT of Chapter 6 finds each H from 4 or more corners.
Call the first two columns of H h1 and h2. Undo the lens: K−1h1 and K−1h2 are r1 and r2 times one scale. So they meet at a right angle, with equal length. Call B = K−⊤K−1 the "lens undo, squared". It holds only K.
h-one transposed, times B, times h-two, equals zero.
Undo the lens on h-one and h-two. Take the dot product of the two results. It must be zero, because the board's axes are at right angles. A second rule, h-one-B-h-one = h-two-B-h-two, says the axes have equal length.
With made-up numbers: the device's photo 1, true K (fx 520, fy 500). The undone axes meet at 90.0°, so the product is 0. Guess 75% of the focal lengths (fx 390, fy 375). The axes now meet at 95.1° and their lengths differ by 4%. That guess breaks both rules.
One panel per photo. Purple arrows: the board's axes after undoing your guessed K. Slide until all three meet at 90°, then try face-on photos.
Illustrative synthetic camera (fx 520, fy 500, cx 330, cy 235). The guess scales fx and fy together and keeps the true principal point.
Count. B is symmetric with 6 entries and a free scale: 5 unknowns. Each photo gives 2 equations, and zero skew gives 1 more. So 1 photo gives 3, too few; 2 photos give 5, just enough. More photos average out the noise.
The symptom is a focal length that jumps between runs. A common cause is photos taken face-on. Switch the device to face-on: every guess gives 90° and equal lengths, so the photos say nothing about the focal length. To measure it, check the spread of fx over repeated calibrations. The fix is to tilt the board 20° to 45° in several directions, and to fill the image.
Chapter 8
Turn on a second camera and watch two rays pin a point in depth
Calibration gave us K, R and t. Can we now take a pixel back to its 3D point? No. P is 3 by 4, so it has no inverse. The divide by w threw the depth away, and no calibration can bring back a number that was never recorded.
We can recover the ray. Call the pixel (u, v) and write it as (u, v, 1). Undo the intrinsics with the inverse matrix K−1. Call the result d, the ray's direction in the camera frame.
d equals K-inverse times the column u, v, one.
Subtract the principal point from the pixel and divide by the focal lengths. With zero skew, that is all K-inverse does. Every point λ times d, with λ > 0, lands on this pixel. The depth λ is unknown.
K with fx = fy = 500 and (320, 240), pixel (520, 340). d = ((520 − 320) / 500, (340 − 240) / 500, 1) = (0.4, 0.2, 1). At λ = 5 the point is (2, 1, 5), the one from Chapter 1. At λ = 10 it is (4, 2, 10). Both give the same pixel.
| Way to get depth back | What it adds |
|---|---|
| Stereo | a second calibrated camera a known distance away; intersect the two rays |
| Structure from motion | one moving camera; match features across frames (Lesson 9) |
| Depth from focus | refocus until sharp; the lens formula gives the distance |
| A prior | a known size or a known ground plane fixes λ |
Seen from above. Camera A alone knows only a ray. Turn on camera B, drag the point and change the gap between the cameras. The shaded patch is where 1 px of noise lets the point hide.
Illustrative toy: two parallel cameras with f = 500 px. The patch spans the rays half a pixel either side of each true ray, so the disparity is off by up to 1 px.
Two rays from a known gap cross at the point. This is triangulation. The gap is the baseline. With a 0.3 m baseline, a point 5 m away and 1 px of noise, depth moves by about 0.17 m. Double the distance and that error grows about four times.
The symptom is depth that jitters for far objects. The baseline is too short for the range. To measure it, watch the spread of the depth of a still target over many frames. The fix is a wider baseline, or a closer target.
Chapter 9
Orbit a camera around a cube and trace one corner from metres to pixels
Every piece now runs at once. A cube stands at the world origin. A camera circles it. Each corner goes world, then [R | t], then camera frame, then K and the divide, then distortion, then the drawn pixel. The trace under the device follows the yellow corner through every step.
Left: the world from above. Right: the image. Orbit the camera, move it in or out, change f, and switch the lens.
Illustrative toy: a 2 m cube and a 640 by 480 image with (cx, cy) = (320, 240). The camera sits 1 m above the cube's centre and aims at it. Barrel and pincushion use k = ∓0.8 × 10−6.
Things to try. Raise f: the cube grows and corners leave the frame, a narrower view. Move in close: near corners grow much faster than far ones, the divide by Z at work. Switch to barrel: straight edges bow, most at the image border.
Chapter 10
Keep the terms, the numbers, the code and the next steps in one place
A camera maps a world point to a pixel with P = K[R | t], then the lens bends it. K holds 5 numbers that belong to the camera. [R | t] holds 6 that change as it moves. Calibration recovers them from known points; a flat board needs two or more tilted photos. Going back, a pixel gives only a ray.
| Term | Rule | Number to remember |
|---|---|---|
| Pinhole | x = f X / Z | f 500, (2, 1, 5): (200, 100) |
| Pixels | u = fx X / Z + cx | (2, 1, 5): pixel (520, 340) |
| Pose | PC = R PW + t, centre −R⊤t | (3, 1, 0) to (0, 1, 1) |
| Homogeneous | (u, v) = (a / w, b / w) | (2600, 1700, 5): (520, 340) |
| Projection | P = K[R | t], 3 by 4 | 11 free numbers |
| Distortion | ud = (1 + k r²)(u − uc) + uc | k 10−6: corner moves 46.9 px |
| DLT | min ‖A m‖, ‖m‖ = 1 | at least 6 points, not coplanar |
| Zhang | h1⊤B h2 = 0, h1⊤B h1 = h2⊤B h2 | 2 photos with zero skew, 3 without |
| Back-projection | d = K−1(u, v, 1) | (520, 340): ray (0.4, 0.2, 1) |
python import numpy as np def project(K, R, t, P_w, k=0.0): P_c = R @ P_w + t # world -> camera frame if P_c[2] <= 0: return None # behind the camera a, b, w = K @ P_c # homogeneous pixel u, v = a / w, b / w # the perspective divide cx, cy = K[0, 2], K[1, 2] s = 1 + k * ((u - cx)**2 + (v - cy)**2) # radial distortion return s * (u - cx) + cx, s * (v - cy) + cy def back_project(K, u, v): return np.linalg.inv(K) @ np.array([u, v, 1.0]) # a ray; depth unknown # Calibration from board photos (Zhang's method, then refinement): # import cv2 # rms, K, dist, rvecs, tvecs = cv2.calibrateCamera(obj_pts, img_pts, (640, 480), None, None)
Based on Stanford AA 274A, Principles of Robot Autonomy I. This page re-teaches the course's lesson on camera models and calibration in our own words, devices and toy numbers. Zhang's method is from Zhengyou Zhang, "A Flexible New Technique for Camera Calibration", IEEE TPAMI, 2000.
Now press Present or Teach and explain from memory: the pinhole, K, the pose, the divide, distortion, the DLT and Zhang's two rules. Then go back to the calibration bench, take 1 photo, and say why it cannot solve.