Skip to content
Tactiverge

Camera geometry and the pinhole model

How a 3D point becomes a pixel: the pinhole camera model, focal length in pixels, estimating distance from size, and why every camera needs calibrating.

Lesson 5 of 6 · 35 min · Self-paced · Free

From pixels back to the world

The last lesson ended with a centroid in pixels. A robot arm, though, moves in metres. To connect the two, you need a model of how a camera turns points in the world into pixels. The standard starting point is the pinhole camera model. It is simple enough to work with on paper, yet it underlies almost every real camera calculation, from measuring objects to stereo depth.

The pinhole camera

Imagine a closed box with a tiny hole in one side. Light from each point in the scene travels in a straight line through the hole and lands on the back wall, making an upside-down image. A real camera uses a lens instead of a hole, to gather more light, but for geometry the pinhole picture is remarkably accurate.

Set up coordinates with the hole at the origin. Z points straight out of the camera towards the scene, X points right, and Y points down (matching image rows). A point at (X, Y, Z) metres lands on the image at pixel position (u, v):

u = f · X / Z + cx
v = f · Y / Z + cy

Here f is the focal length measured in pixels, and (cx, cy) is the principal point, the pixel where the camera's central axis meets the image, usually close to the centre. The model's mathematics flips the image upright for you, so you do not need to worry about it being upside down.

The key feature is the division by Z. Things further away appear smaller and closer to the centre. That single division is what perspective is.

f = 600.0              # focal length, pixels
cx, cy = 320.0, 240.0  # principal point (image centre), pixels

def project(X, Y, Z):
    """Pinhole projection of a point in camera coordinates (metres)."""
    u = f * X / Z + cx
    v = f * Y / Z + cy
    return round(u, 1), round(v, 1)

print(project(0.10, 0.05, 1.0))   # 10 cm right, 5 cm down, 1 m away
print(project(0.10, 0.05, 2.0))   # same offset, twice as far
print(project(0.20, 0.10, 2.0))   # twice the offset, twice as far

Output:

(380.0, 270.0)
(350.0, 255.0)
(380.0, 270.0)

Work through the first case: 600 × 0.10 ÷ 1.0 = 60 pixels right of centre, so u = 320 + 60 = 380. Moving the same point twice as far away halves its offset from the centre. And the third line reveals something important: a point twice as far away and twice as far to the side lands on exactly the same pixel. Every point along a line through the pinhole projects to the same place. A single image loses depth. From one pixel alone, you cannot tell how far away something is.

What focal length in pixels means

Focal length is usually quoted in millimetres for a lens, but for calculations it is more convenient in pixels. It is linked to the field of view, the angle the camera can see. If the image is W pixels wide and the horizontal field of view is θ, then:

f = (W / 2) / tan(θ / 2)
import math

def focal_pixels(image_width_px, horizontal_fov_deg):
    half = math.radians(horizontal_fov_deg / 2)
    return (image_width_px / 2) / math.tan(half)

print(f"{focal_pixels(640, 60):.1f} px")
print(f"{focal_pixels(640, 90):.1f} px")

Output:

554.3 px
320.0 px

A wider field of view means a smaller focal length in pixels: each pixel covers a larger angle, so you see more of the scene in less detail. Choosing a camera is partly choosing this trade-off.

Distance from known size

If you know the real size of an object, you can recover the depth that a single image loses. An object of real height H at distance Z appears h pixels tall, where h = f · H / Z. Rearranging gives:

Z = f · H / h
f = 600.0          # focal length, pixels
H = 0.065          # real height of a tennis-ball-sized object, metres

for h_pixels in [78, 39, 20]:
    Z = f * H / h_pixels
    print(f"{h_pixels:3d} px tall -> about {Z:.2f} m away")

# How much does a 1-pixel mistake matter?
Z_true = f * H / 20
Z_off  = f * H / 19
print(f"1 px error at 20 px changes the estimate by {(Z_off - Z_true) * 100:.1f} cm")
print(f"Each pixel spans {Z_true / f * 1000:.1f} mm at {Z_true:.2f} m")

Output:

 78 px tall -> about 0.50 m away
 39 px tall -> about 1.00 m away
 20 px tall -> about 1.95 m away
1 px error at 20 px changes the estimate by 10.3 cm
Each pixel spans 3.2 mm at 1.95 m

The last two lines are the engineering lesson. When the object is small in the image, a mistake of just one pixel in measuring its height (easily caused by blur or a slightly wrong threshold) shifts the distance estimate by about 10 cm. Vision measurements get less precise with distance, and quickly. A gripper that needs to be within a centimetre should not trust this estimate from two metres away.

Why calibration matters

Everything above assumed you knew f, cx and cy exactly, and that the lens behaves like a perfect pinhole. Neither is true out of the box:

  • The focal length printed on a lens is nominal, and the conversion to pixels depends on the exact sensor. A few percent error in f gives a few percent error in every distance.
  • The principal point is rarely exactly at the image centre.
  • Real lenses bend straight lines, especially near the edges of wide-angle images. This lens distortion (barrel or pincushion) means pixels near the corners are not where the pinhole formula predicts.

Camera calibration measures these values. The usual method photographs a printed pattern with known geometry, often a checkerboard, from many angles. Because the true square size is known, software can solve for the focal length, principal point and distortion that best explain where the corners appear. OpenCV includes tools for this (it must be installed separately). A calibrated camera can then remove distortion from images and turn pixels into reliable directions in space.

Calibration only holds for the setup you calibrated. Changing the zoom or focus, or knocking the lens, means calibrating again.

Recap

  • The pinhole model maps (X, Y, Z) to pixels with u = f · X / Z + cx and v = f · Y / Z + cy.
  • Dividing by Z creates perspective, and means one image cannot reveal depth by itself.
  • Focal length in pixels is set by image width and field of view.
  • Known object size gives distance, Z = f · H / h, but precision falls quickly with distance.
  • Calibration measures the real focal length, principal point and lens distortion.

Project

Paper project: measure your own camera

You can do this with any phone or laptop camera, a ruler and Python.

  1. Tape a sheet of paper with a 10 cm line drawn on it to a wall. Photograph it face-on from 50 cm, 100 cm and 150 cm, measured with a tape measure.
  2. On a computer, zoom in on each photo and measure the line's length in pixels (most image viewers show the cursor position). Note the image width in pixels.
  3. For each photo, calculate f = h · Z / H. Do the three values agree? Average them.
  4. Use your f to predict the line's pixel length at 200 cm, then test the prediction.
  5. Estimate how much a one-pixel measuring error changes your distance estimate at each distance.

Deliverables

  • A table of distance, pixel length and calculated focal length
  • Your prediction for 200 cm and the measured result
  • A short note on which measurements you trust least, and why

Knowledge check

Check your understanding

  1. 1. With f = 500 px and cx = 320, a point at X = 0.2 m, Z = 2 m projects to which u?
  2. 2. Why can a single camera image not tell you how far away a point is?
  3. 3. An object 0.10 m tall appears 50 px tall through a camera with f = 600 px. About how far away is it?
  4. 4. A camera is swapped for one with a wider field of view but the same image width. What happens to f in pixels?
  5. 5. What does camera calibration with a checkerboard measure?

Go further

Camera geometry and the pinhole model — Computational Engineering — Tactiverge