A camera turns each 3D point into a pixel by dividing by its depth. A post 2 m away and 8.5° to the right lands on column 440 of a 640-pixel image. Move it to 6 m along the same ray and it still lands on column 440. Only its height in the image changes, from 160 px to 53 px.
Slide the post deeper along its ray and watch the blue column stay put. Then turn the ray or change the lens. Then we build it piece by piece: the pinhole, the matrix K, field of view, the camera pose, the full projection, the ray back and lens distortion.
You need Lecture 1 (frames, rotation matrices and the pose TWB), similar triangles, a matrix times a vector and a little Python. We build the rest from zero.
Lecture 5 of 14 in our follow-along of MIT 16.485, Visual Navigation for Autonomous Vehicles (VNAV, taught by Luca Carlone). Every idea is re-taught here in our own words and devices. Previous: Lecture 4, trajectory optimisation. Next: Lecture 6, feature detection and tracking.
Illustrative camera: a 640 × 480 image, principal point (320, 240), no skew, no distortion. The post is 0.4 m tall. Exact pinhole maths; we checked the projections against OpenCV.
Chapter 0
Change the real size of a sign and watch its distance change while the image stays the same
A drone flies toward a sign. Its camera sees the sign 200 pixels wide. Is the sign 3 m away or 30 m away? A small sign close by and a big sign far away make exactly the same picture.
This is not a fault of the sensor. A camera maps 3D space onto a flat image, and that map throws one number away: the distance along each line of sight. Every 3D point on one line through the lens lands on the same pixel. That line is a ray: a half-line that starts at the lens and goes out into the scene.
The image on the right never changes: the sign fills 200 px. Choose how wide the sign is in the world. The top view on the left puts it at the only distance that makes that image.
Illustrative camera with a focal length of 800 px (Chapter 3). Exact pinhole maths: distance = 800 × width / 200.
Work the number. The camera stretches 1 m at 1 m distance to 800 pixels. A sign 0.75 m wide that fills 200 pixels must be 800 × 0.75 / 200 = 3.0 m away. A sign 7.5 m wide, a billboard, would be 30 m away. Without the real size, one image cannot tell them apart.
A robot must recover depth to build a map and to know how far it moved. So it must undo the camera's map, and to undo it, it must know the map exactly. This lecture writes that map down, number by number. Later lectures recover the lost depth from two views (Lecture 7) or from motion and an IMU (Lecture 11).
Symptom: a single-camera system reports the right direction to an object but a distance that is off by a constant factor.
Measure: compare its distances with a tape measure at two ranges. A fixed ratio means a scale error, not noise.
Fix: give it a known size, a second view, a known camera height over flat ground (Chapter 7) or another sensor.
Chapter 1
Move a point and change the focal length and watch similar triangles place its image
Picture a dark box with a tiny hole in one face. Light from the scene goes straight through the hole to the back face, one straight line per point. This is the pinhole camera.
Put the hole, the optical centre, at the origin of the camera frame C. As in Lecture 1, C is right-down-forward: x to the right of the image, y down, z out of the lens. The z axis is the optical axis. The image forms on a plane at distance f from the hole. That distance is the focal length, here in millimetres.
The real image on the back face is upside down. We use the virtual image plane at z = +f in front of the hole instead: same size, upright.
Call the point's coordinates in the camera frame Cp = (X, Y, Z), in metres. Z is its depth along the optical axis. Call the spot where its ray crosses the image plane (x, y), in millimetres from the axis.
Little x equals f times capital X over capital Z, and little y equals f times capital Y over capital Z.
Two triangles share the corner at the hole. The small one has height x and base f. The big one has height X and base Z. Same angles, so x / f = X / Z. Multiply by f. Do the same for y.
With made-up numbers: f = 4 mm and Cp = (0.3, −0.1, 2.0) m. Then x = 4 × 0.3 / 2.0 = 0.6 mm and y = 4 × (−0.1) / 2.0 = −0.2 mm. The image sits 0.6 mm right of the axis and 0.2 mm up, because y counts down.
Side view: the scene is to the right of the hole, the real image plane behind it. Move the pink point and change f. Choose which image plane to draw.
Illustrative side view, not to scale: we draw the box larger so the millimetres show. Exact formula y = f Y / Z.
The division by Z is the whole of perspective. Double the depth and the image shrinks to half. Double every coordinate, (0.6, −0.2, 4.0), and the image stays at 4 × 0.6 / 4.0 = 0.6 mm: Chapter 0's lost depth.
Symptom: a projected point flies off to a huge pixel value, or flips to the wrong side of the image.
Measure: the depth Z of that point in the camera frame. Near zero, the division explodes; below zero, the point is behind the camera.
Fix: drop points with Z below a small positive limit (0.1 m, say) before you divide.
Chapter 2
Scale a vector of three numbers and watch the image point it names stay fixed
Chapter 1 has a division in it, so it is not a matrix product. Lecture 1 chained rotations and shifts as 4 × 4 matrices. We want the projection in that chain too. The trick is to put the division off until the last step.
Describe an image point by three numbers (a, b, w) instead of two. Agree that they mean the point (a / w, b / w). These are homogeneous coordinates. Any multiple k(a, b, w) with k ≠ 0 names the same point, because k cancels in the division. Dividing by the last entry is called dehomogenising.
Capital Z times the column x, y, one equals the matrix with f, f, one on its diagonal times the column capital X, capital Y, capital Z.
Multiply the point (X, Y, Z) by the matrix. You get (f X, f Y, Z): no division yet. Then divide the first two entries by the third. That gives x = f X / Z and y = f Y / Z, the pinhole of Chapter 1.
With f = 4 mm and (X, Y, Z) = (0.3, −0.1, 2.0): the product is (1.2, −0.4, 2.0). Divide by 2.0: (0.6, −0.2) mm, as before. The point (0.6, −0.2, 4.0) gives (2.4, −0.8, 4.0): twice the vector, the same image point.
One slice of the picture: across is the first entry a, up is the last entry w. The yellow line w = 1 is the image line. Scale the vector and watch where its line crosses w = 1.
Exact maths with f = 4 mm and Z = 2 m. We draw the w axis in its own units so both the vector and the line fit.
Lift a 3D point the same way: write (X, Y, Z, 1), four numbers. Then a rotation, a shift and the projection are all matrix products, and a chain of them is one matrix. Chapter 6 builds that matrix. The division waits until the last step.
Symptom: pixel values come out in the hundreds of thousands, or scale with the distance of the point.
Measure: look at the third entry of the product. If it is not 1, the vector is still homogeneous.
Fix: divide the first two entries by the third before you use them as pixels.
Chapter 3
Turn the focal length, the principal point and the skew and watch a grid of points move across the image
The sensor does not report millimetres. It reports pixels, counted from the top-left corner: u is the column, v the row. Two steps take us there. First, divide the millimetres by the size of one pixel. Second, shift the origin from the optical axis to the corner.
Say one pixel is 5 µm wide, its pixel pitch. Then 0.6 mm is 600 / 5 = 120 pixels. The pixel where the optical axis pierces the sensor is the principal point (cx, cy), near the image centre. Combine the steps into the focal length in pixels, fx = f / pitch: 4 mm / 5 µm = 800 px.
Capital Z times the column u, v, one equals K times the column capital X, Y, Z. K has rows f-x, s, c-x; zero, f-y, c-y; and zero, zero, one.
Multiply the point by K. Divide the first two entries by the third. You get u = f-x X / Z + s Y / Z + c-x and v = f-y Y / Z + c-y. The f's stretch, c-x and c-y shift, s shears.
K with fx = fy = 800, c = (320, 240), s = 0, and Cp = (0.3, −0.1, 2.0). The product is (880, 400, 2). Divide by 2: u = 440, v = 200. With a skew of s = 8, u moves to 439.6. OpenCV's projectPoints gives the same (440, 200).
A 5 × 5 grid of pink points, 0.4 m apart, stands 3 m in front of the camera. Its blue images land on the 640 × 480 sensor. The warm cross is the principal point. Red points fall off the sensor.
Illustrative. cy stays at 240. We exaggerate the skew range to make the shear visible; real cameras have s near 0.
Where does K come from? From calibration: photograph a known pattern and solve for the numbers. Zhang's method (2000) does it from a few views of a flat checkerboard, and also fits the lens distortion of Chapter 8. AA 274A Lecture 8 works it through.
Symptom: after you resize every image to half size, all projections land in the wrong place.
Measure: compare K with the image size. A principal point near (320, 240) on a 320 × 240 image is a sign K was not updated.
Fix: scale fx, fy, cx and cy by the same factor as the image: 800 becomes 400, (320, 240) becomes (160, 120).
Chapter 4
Change the lens and the sensor and watch the camera's view widen or narrow
Which focal length should a drone carry? A short one sees a wide slice of the world in few pixels per degree. A long one sees a narrow slice in fine detail. The angle the image covers is the field of view, and it follows from K and the image size alone.
The image edge is cx pixels from the principal point. Take the ray that just reaches the edge. Its tangent is that pixel distance over the focal length in pixels. Call the image width W, in pixels, and the horizontal field of view FOVh. With the principal point in the middle, the half-width is W / 2.
FOV-h equals two times the arctangent of W over two f-x.
Half the image width W over f-x is the tangent of the half angle. Take its arctangent and double it. In millimetres it is the same sum: half the sensor width over f.
W = 640 and fx = 800: FOVh = 2 arctan(0.4) = 43.6°. The same in millimetres: a 3.2 mm wide sensor and f = 4 mm give 2 arctan(1.6 / 4) = 43.6°. Vertically, 2 arctan(240 / 800) = 33.4°.
A row of pink posts stands 5 m away, 1 m apart. The blue wedge is what the camera sees. Below, the 640-pixel image row: the posts it catches.
Illustrative sensors, both 640 px wide; principal point at the centre. Exact formula above.
Two consequences. Halve the focal length and the field of view does not double: fx = 400 gives 77.3°, not 87.2°, because the arctangent bends. And cropping is the same as zooming. Keep fx = 800 but use only the middle 320 columns: the view narrows to 22.6°. That is what fx = 1,600 gives on the full width.
Symptom: a 120° wide-angle camera, modelled as a pinhole, puts its edge points far from where they appear.
Measure: the field of view. Near 180° the tangent runs off to infinity, so a flat image plane cannot hold the edges.
Fix: keep the pinhole for moderate angles. For wider lenses use a fisheye model (an angle-based projection) instead.
Chapter 5
Move and turn the camera and watch the same world point get new camera coordinates
K wants the point in the camera frame. A map stores it in the world frame W, with z up. So first we move the point from W into C. The camera's pose in the world, from Lecture 1, is RWC and tWC: its orientation, and the position of its centre. They map C numbers to W numbers. We need the other way.
Invert the pose as Lecture 1 did: RCW = RWC⊤ and tCW = −RWC⊤ tWC. RCW and tCW are the camera's extrinsics: where it stands and looks, as a map from the world to the camera.
C-p equals R-C-W times W-p, plus t-C-W.
Turn the world point by R-C-W, then add t-C-W. The same as: subtract the camera position t-W-C from W-p, then read the difference along the camera's right, down and forward axes.
With made-up numbers: the camera sits at tWC = (0, 0, 1.5), level, looking along world x. Its axes in the world are right (0, −1, 0), down (0, 0, −1), forward (1, 0, 0). The point Wp = (2, −0.3, 1.6) minus the camera is (2, −0.3, 0.1). Along right, down, forward: Cp = (0.3, −0.1, 2.0). Here tCW = (0, 1.5, 0).
Top view of the world (grey axes, x up the page). The blue camera stands 1.5 m high. The pink point stays put. The warm arrows read the point along the camera's right and forward axes.
Exact maths. The camera is level at 1.5 m; yaw turns it about world z, positive counter-clockwise. The point is (2, −0.3, 1.6).
Look at tCW = (0, 1.5, 0). The camera is at height 1.5, yet tCW has no z part. It is the world origin seen from the camera: 1.5 m along the camera's y, which points down. To get the camera position back, use tWC = −RCW⊤ tCW = (0, 0, 1.5).
Symptom: the projected map slides the wrong way when the drone moves forward.
Measure: project a point straight ahead from two camera positions. If it moves away from the image centre as you approach it head-on, you used the pose, not its inverse.
Fix: projection needs RCW and tCW, world to camera. Invert the estimator's RWC, tWC first.
Chapter 6
Step one world point through the chain and watch it become a pixel
We have every piece. Extrinsics take a world point into the camera frame. K takes it to a homogeneous pixel. Dividing by the third entry gives the pixel. In homogeneous coordinates the first two steps are matrix products, so they merge into one 3 × 4 matrix: the projection matrix P.
Write the world point with a 1 on the end: Wp̃ = (XW, YW, ZW, 1), "W-p-tilde". Put RCW and tCW side by side as a 3 × 4 block, [RCW | tCW]. Call the scale left in the third entry λ, lambda.
Lambda times the column u, v, one equals K times the block R-C-W bar t-C-W, times W-p-tilde, which is P times W-p-tilde.
Add a 1 to the world point. Multiply by P. Divide the first two entries of the result by the third. That pixel is u, v; the third entry, lambda, is the depth.
The camera of Chapter 5 and K of Chapter 3 give the rows of P: (320, −800, 0, 0), (240, 0, −800, 1200), (1, 0, 0, 0). For Wp̃ = (2, −0.3, 1.6, 1) the product is (880, 400, 2). So λ = 2 m and the pixel is (440, 200). OpenCV's projectPoints agrees.
Pick a world point and press Step. Each press applies one stage. Watch the numbers change, and the pixel land on the image.
The camera of Chapter 5 (at (0, 0, 1.5), looking along world x) and K of Chapter 3. Exact maths, checked with cv2.projectPoints.
Point C is behind the camera. Its depth is λ = −2 m, yet the division still returns a pixel, (200, 200), inside the image. P cannot tell front from back: (u, v, 1) and −(u, v, 1) name the same pixel. Your code must check the sign of λ.
Symptom: points behind the drone appear in its rendered view, mirrored through the centre.
Measure: the third entry λ of P Wp̃ for each point.
Fix: keep only points with λ above a small positive limit before you divide.
Chapter 7
Pick a pixel and watch its ray leave the camera and meet the ground
Now run the chain backwards. A feature tracker hands you a pixel. Where in the world could it be? Anywhere on one ray.
Undo K and you get the ray's direction. Undo the pose and you get the ray in the world. The depth stays unknown.
Undoing K gives the normalised image coordinates (xn, yn): xn = X / Z and yn = Y / Z. They are the image of a pinhole with focal length 1, before any pixels. Call the unknown depth along the ray λ, lambda.
W-p of lambda equals t-W-C plus lambda times R-W-C times K-inverse times the column u, v, one.
Subtract c-x and c-y from the pixel and divide by the focal lengths: that is x-n, y-n. The camera-frame direction is (x-n, y-n, 1). Turn it into the world with R-W-C. Start at the camera centre t-W-C and walk lambda times that direction.
Pixel (440, 200): xn = 120 / 800 = 0.15 and yn = −40 / 800 = −0.05. With the camera of Chapter 5 the world direction is (1, −0.15, 0.05). At λ = 2: (0, 0, 1.5) + 2 × (1, −0.15, 0.05) = (2, −0.3, 1.6), point A again. cv2.undistortPoints gives the same (0.15, −0.05).
Side view: the camera stands 1.5 m above flat ground. Pick a pixel row. Its blue ray hits the ground at the purple point. Then switch to the box: the pixel shows the top of a 0.5 m box.
Exact maths with K of Chapter 3 and the level camera of Chapter 5; the column is fixed at u = 320 in this side view. The flat-ground guess solves ZW = 0 for λ.
One extra fact fixes the depth. If the point lies on flat ground, its world height is 0, so 1.5 + λ × (its z direction) = 0. Pixel (440, 400) has world direction (1, −0.15, −0.2), so λ = 1.5 / 0.2 = 7.5 m and the point is (7.5, −1.125, 0).
Symptom: a single-camera obstacle detector says a box is 6 m away when it is 4 m away.
Measure: which pixel you back-projected. The top of a 0.5 m box at 4 m lands on row 440, where the flat-ground guess says 6 m.
Fix: back-project only the point where the object touches the ground, or get depth from a second view.
Chapter 8
Turn the distortion coefficients and watch a straight grid bow into a barrel or a pincushion
A pinhole sends light in straight lines. A real lens bends it, more toward the edges. Straight edges in the world come out curved in the image. This is lens distortion. Ignore it and every pixel near the border sits a few pixels off, which wrecks a pose estimate.
The usual model is Brown–Conrady, the one OpenCV uses. It acts on the normalised coordinates of Chapter 7, before K. Call the ideal normalised point (x, y), its squared distance from the axis r² = x² + y², and the distorted point (xd, yd). The radial coefficients k1, k2 push points along the radius. The tangential coefficients p1, p2 model a lens that sits slightly tilted.
x-d equals x times one plus k-one r-squared plus k-two r-to-the-fourth, plus two p-one x y, plus p-two times r-squared plus two x-squared. y-d is the same, with the roles of x and y, and of p-one and p-two, swapped.
Find how far the ideal point x, y is from the axis, as r-squared. Scale the point by 1 + k-one r-squared + k-two r-to-the-fourth. Add the small tangential shifts with p-one and p-two. Then apply K to x-d, y-d to get the pixel.
With made-up coefficients k1 = −0.2, k2 = 0.05 and p = 0: the ideal corner (0.4, 0.3) has r² = 0.25. The factor is 1 − 0.05 + 0.003 = 0.953, so (xd, yd) = (0.381, 0.286). With K: pixel (625.0, 468.75), not (640, 480). The corner moved 18.75 px inward. Add p1 = 0.001, p2 = 0.002: (626.10, 469.48). cv2.projectPoints agrees.
Faint yellow: a straight grid as a pinhole would see it. Blue: where the lens puts it. The warm arrow shows how far the corner moves. Try a preset, then the sliders.
Exact OpenCV model (k3 = 0) with K of Chapter 3. The tangential preset exaggerates p to make it visible.
The model maps ideal points to distorted ones. To clean a measured pixel you need the reverse, and there is no closed form. Solve it by repeating x = xd / factor(x). From the corner above, one pass gives 0.3983, two give 0.3998 and five give 0.4000. cv2.undistortPoints does this for you.
Symptom: a distortion factor of four million, or pixel errors that grow with the square of the image size.
Measure: the units of r. In pixels, r² for a point 95 px out is 9,000, and k1 r² explodes.
Fix: compute r from normalised coordinates, (u − cx) / fx, never from raw pixels.
Chapter 9
Move the camera, change the lens and the distortion, and watch a cube's image respond
Here is the whole model. A pink 1 m cube stands on a ground grid. The camera's pose gives [RCW | tCW] (Chapter 5). K turns the camera points into pixels (Chapters 3 and 6). k1 bends them (Chapter 8). The page cuts every edge into small pieces and projects each one, so bent lines show as curves.
Try three things. Fly close until a corner leaves the image and turns red. Set k1 to −0.3 and watch the cube's straight edges bow. Then shorten fx to 400: the cube shrinks and the field of view grows to 77°.
Left or top: a view from above with the blue camera wedge and the pink cube. Right or bottom: the 640 × 480 image. The yellow border is the sensor; red corners fall off it.
Illustrative scene: the camera is level at 1.5 m and looks along world x; the cube's near face is at x = 0. Exact maths in your browser, the same as cv2.projectPoints with k1 only.
Watch the read-out as you fly. The cube's near top corner is the same world point the whole time. Only RCW, tCW and K change, and its pixel follows them through one matrix and one division.
Chapter 10
Keep the formulas, the conventions and the code side by side
A camera divides by depth, so each pixel is a whole ray. Homogeneous coordinates put the division last, so the chain is one matrix: P = K[RCW | tCW]. K holds the focal length in pixels, the principal point and the skew; with the image width it sets the field of view. The extrinsics move world points into the camera frame. K−1 and the pose send a pixel back out as a ray. A real lens adds Brown–Conrady distortion on the normalised coordinates, before K.
| Piece | Formula | Number on this page | Watch out |
|---|---|---|---|
| Pinhole | x = f X / Z, y = f Y / Z | f 4 mm, (0.3, −0.1, 2): (0.6, −0.2) mm | Z must be positive |
| Homogeneous | (a, b, w) means (a / w, b / w) | (1.2, −0.4, 2) and (2.4, −0.8, 4) agree | divide before use |
| Intrinsics | Z(u, v, 1) = K(X, Y, Z) | fx 800, c (320, 240): (440, 200) | rescale K with the image |
| Field of view | 2 arctan(W / 2fx) | 640, 800: 43.6° | pinhole fails near 180° |
| Extrinsics | Cp = RCW Wp + tCW | tCW = (0, 1.5, 0) | t is not the camera position |
| Projection | λ(u, v, 1) = P Wp̃ | (880, 400, 2): (440, 200), λ 2 | check λ > 0 |
| Back-projection | tWC + λ RWC K−1(u, v, 1) | (440, 400) hits ground at 7.5 m | depth needs one more fact |
| Distortion | x(1 + k1r² + k2r⁴) + tangential | corner (640, 480) to (625, 468.75) | r in normalised units |
python import numpy as np, cv2 K = np.array([[800., 0, 320], [0, 800, 240], [0, 0, 1]]) R_WC = np.array([[0., 0, 1], [-1, 0, 0], [0, -1, 0]]) # columns: right, down, forward in W t_WC = np.array([0., 0, 1.5]) # camera centre in W R_CW, t_CW = R_WC.T, -R_WC.T @ t_WC # extrinsics: world to camera P = K @ np.hstack([R_CW, t_CW[:, None]]) # 3 x 4 projection matrix def project(pW, k1=0., k2=0., p1=0., p2=0.): X, Y, Z = R_CW @ pW + t_CW # camera frame if Z <= 0.1: return None # behind or too close x, y = X / Z, Y / Z # normalised r2 = x*x + y*y; f = 1 + k1*r2 + k2*r2*r2 # Brown-Conrady xd = x*f + 2*p1*x*y + p2*(r2 + 2*x*x) yd = y*f + p1*(r2 + 2*y*y) + 2*p2*x*y return K[0,0]*xd + K[0,2], K[1,1]*yd + K[1,2] # pixel pW = np.array([2., -0.3, 1.6]) q = P @ np.r_[pW, 1]; print(q[:2] / q[2], q[2]) # [440. 200.] 2.0 rvec, _ = cv2.Rodrigues(R_CW) print(cv2.projectPoints(pW[None], rvec, t_CW, K, None)[0].ravel()) # [440. 200.] ray = R_WC @ np.linalg.inv(K) @ np.array([440, 400, 1.]) # back-project a pixel lam = -t_WC[2] / ray[2]; print(lam, t_WC + lam * ray) # 7.5 [7.5 -1.125 0.] D = np.array([-0.2, 0.05, 0.001, 0.002, 0]) # k1 k2 p1 p2 k3 uv = cv2.projectPoints(np.array([[0.4, 0.3, 1.]]), np.zeros(3), np.zeros(3), K, D)[0] print(uv.ravel(), cv2.undistortPoints(uv, K, D).ravel()) # [626.104 469.478] [0.4 0.3]
This lecture opens the perception half of the series. Lecture 4 planned a path for the drone; from here on we build the estimator that tells it where it is. Lecture 6 finds and tracks pixels, Lecture 7 relates the rays of two cameras, and Lecture 11 fuses them with an IMU. Each one starts from the projection on this page.
Now press Present or Teach. Explain from memory why one image loses depth and where x = f X / Z comes from. Say what each entry of K does and how fx sets the field of view. Say why tCW is not the camera position, and how to undistort a pixel. Then go back to the hero and say why the column stays at 440 while the post shrinks.