Search

Search papers and lessons

Pinhole Camera Model

Level 2 · 9 min read

A camera model that maps a 3D point to an image point by dividing its sideways position by its depth and scaling by the focal length.

On this page

  • Colour = topic mix
  • Linear algebra
  • Computer vision
  • Discrete math
  • Sign in to see your progress

TL;DR

A pinhole camera forms an image by letting light from each scene point pass through one small opening, so every point lands on a flat sensor at a position set by where it sits relative to that opening. The model says the image position is the point's sideways offset divided by its depth, times a focal length. In homogeneous coordinates that division becomes one matrix product followed by a single divide.

Where you'll see it: camera calibration, structure from motion, two-view geometry, and the projection step in every 3D renderer.

Intuition

Put a sheet of paper a short distance behind a pinhole in a dark box, facing a sunlit window. An upside-down picture of the window appears on the paper. Each point of the window sends light in a straight line through the hole, and that light lands on one spot. Light from the top of the window ends up at the bottom of the paper, which is why the image is inverted.

This is the camera obscura, used for centuries before photographic film. The pinhole model is the same picture drawn with geometry. Put the hole, which is the centre of projection, at the origin. The sensor sits a distance ff (the focal length) behind it. A point at sideways offset XX and depth ZZ lands at a height on the sensor that scales like X/ZX / Z. The farther a point is, the smaller its image. That single fact is the whole perspective effect, and it is why parallel railway tracks appear to meet at a vanishing point.

Why build this model at all? It turns a 3D question ("where does this object appear?") into a 2D one, and it lets a program run the process backwards: given where things appear in images, estimate where they are in the scene.

1234567802468depth Zimage offset u
  • X = 1, f = 2
  • X = 2, f = 2
With focal length 2, the image offset falls as 1 over depth. Doubling the depth of the point at X = 1 halves its image offset, from 0.5 to 0.25.
flowchart LR
  W["3D point (X, Y, Z)"] --> R["Straight ray through the centre of projection"]
  R --> I["Image point (fX/Z, fY/Z)"]

The math

Setup

Place the camera's centre of projection at the origin, with the optical axis along the zz-axis. The image plane is the plane z=fz = f, where f>0f \gt 0 is the focal length. (Real sensors sit behind the centre. Placing the image plane in front avoids an upside-down image in the formulas and is the usual convention.) A world point written in camera coordinates is X=(X,Y,Z)⊤\mathbf{X} = (X, Y, Z)^\top, and Z>0Z \gt 0 means the point is in front of the camera.

Definition: Pinhole projection

A camera with focal length ff maps a point (X,Y,Z)(X, Y, Z) with Z>0Z \gt 0 to the image point

u=fXZ,v=fYZ.u = f\frac{X}{Z}, \qquad v = f\frac{Y}{Z}.

Where does the formula come from? The ray from the origin to the point is the set of points t (X,Y,Z)t\,(X, Y, Z) for t>0t \gt 0. It meets the image plane when its zz-coordinate equals ff, so tZ=ftZ = f, which gives t=f/Zt = f/Z. Substituting gives the point (fX/Z, fY/Z, f)(fX/Z,\ fY/Z,\ f) on the plane. Its first two coordinates are the image point.

Projection as one matrix

Matrix multiplication is linear, and a linear map cannot divide one coordinate by another. So no ordinary matrix maps (X,Y,Z)(X, Y, Z) straight to (fX/Z,fY/Z)(fX/Z, fY/Z). The fix is to use homogeneous coordinates: write the 3D point as (X,Y,Z,1)(X, Y, Z, 1), and write the image point as a triple (u′,v′,w)(u', v', w) that stands for (u′/w, v′/w)(u'/w,\ v'/w).

(u′v′w)=(f0000f000010)(XYZ1)=(fXfYZ)\begin{pmatrix} u' \\ v' \\ w \end{pmatrix} = \begin{pmatrix} f & 0 & 0 & 0 \\ 0 & f & 0 & 0 \\ 0 & 0 & 1 & 0 \end{pmatrix} \begin{pmatrix} X \\ Y \\ Z \\ 1 \end{pmatrix} = \begin{pmatrix} fX \\ fY \\ Z \end{pmatrix}

Dividing by w=Zw = Z recovers u=fX/Zu = fX/Z and v=fY/Zv = fY/Z. The matrix is 3×43 \times 4 because the input is a 3D point and the output is a 2D image point.

Pixels: the intrinsic matrix

A real sensor counts in pixels, and its optical axis may not hit the exact middle. Collect these details in the intrinsic matrix

K=(fxscx0fycy001).K = \begin{pmatrix} f_x & s & c_x \\ 0 & f_y & c_y \\ 0 & 0 & 1 \end{pmatrix}.

Here fxf_x and fyf_y are the focal lengths measured in pixels, (cx,cy)(c_x, c_y) is the principal point (where the optical axis meets the sensor), and ss is the skew, which is zero on almost every modern camera. With s=0s = 0, multiplying KK by (X,Y,Z)⊤(X, Y, Z)^\top and dividing by the third entry gives u=fxX/Z+cxu = f_x X/Z + c_x and v=fyY/Z+cyv = f_y Y/Z + c_y.

Placing the camera in the world

The point usually starts in world coordinates. A rotation matrix RR and a translation vector t\mathbf{t} move it into camera coordinates: Xc=RXw+t\mathbf{X}_c = R\mathbf{X}_w + \mathbf{t}. The full model is

p∼K(Rt)(Xw1),\mathbf{p} \sim K \begin{pmatrix} R & \mathbf{t} \end{pmatrix} \begin{pmatrix} \mathbf{X}_w \\ 1 \end{pmatrix},

where ∼\sim means equal up to a nonzero scale factor. The 3×43 \times 4 matrix P=K [R∣t]P = K\,[R \mid \mathbf{t}] has 11 degrees of freedom: 5 from KK, 3 from the rotation and 3 from the translation. The twelfth entry is absorbed by the scale.

Worked examples

Example
A point moves away from the camera

Use a focal length f=2f = 2 in normalised units, and a point with X=1X = 1 and Y=0.5Y = 0.5.

  1. At depth Z=4Z = 4: u=2⋅1/4=0.5u = 2 \cdot 1 / 4 = 0.5 and v=2⋅0.5/4=0.25v = 2 \cdot 0.5 / 4 = 0.25.
  2. Move the point to depth Z=8Z = 8: u=2⋅1/8=0.25u = 2 \cdot 1 / 8 = 0.25 and v=2⋅0.5/8=0.125v = 2 \cdot 0.5 / 8 = 0.125.

Interpretation: doubling the depth halves both image coordinates. The image shrinks in inverse proportion to depth.

Example
A 640 by 480 camera seeing a point one unit to the side

Take a camera with fx=fy=800f_x = f_y = 800 pixels, zero skew and principal point (320,240)(320, 240). Its pose is a rotation R=Rz(90∘)=(0−10100001)R = R_z(90^\circ) = \begin{pmatrix} 0 & -1 & 0 \\ 1 & 0 & 0 \\ 0 & 0 & 1 \end{pmatrix} with translation t=(0,0,5)\mathbf{t} = (0, 0, 5). The world point is Xw=(1,0,0)\mathbf{X}_w = (1, 0, 0).

  1. Move to camera coordinates: RXw=(0,1,0)R\mathbf{X}_w = (0, 1, 0), so Xc=(0,1,0)+(0,0,5)=(0,1,5)\mathbf{X}_c = (0, 1, 0) + (0, 0, 5) = (0, 1, 5).
  2. Apply KK to Xc\mathbf{X}_c: the first entry is 800⋅0+320⋅5=1600800 \cdot 0 + 320 \cdot 5 = 1600, the second is 800⋅1+240⋅5=2000800 \cdot 1 + 240 \cdot 5 = 2000, and the third is 55.
  3. Divide by the third entry: (1600/5, 2000/5)=(320,400)(1600 / 5,\ 2000 / 5) = (320, 400).

Interpretation: the point lands in the middle column and 400 pixels down, in the lower half of the 480-row image. The rotation and translation did the placing, and KK turned the result into pixel units.

Tradeoffs and failure modes

The pinhole model is an idealisation, but it is the first-order model for almost every camera. The table lists where it breaks and what to do.

SituationWhat goes wrongBetter choice
Wide-angle or cheap lensStraight lines bend near the image edge (radial distortion)Add distortion coefficients and calibrate them
Scene depth varies little compared with its distanceDepth changes barely move the image, so perspective adds littleWeak-perspective or orthographic camera
Point with Z≤0Z \le 0 (behind the camera)The division flips sign or blows up, so the formula is meaninglessDiscard or clip such points before projecting
Real lens with a finite apertureDepth-dependent blur; a pinhole gives sharp everythingThin-lens model, or accept the error
Rolling shutter or fast motion during exposureEach image row sees a slightly different camera poseModel a pose that changes with time

A real pinhole lets in too little light, which is why real cameras use lenses. A lens is still well described by the pinhole model once distortion and focus are accounted for.

History

A Chinese text from the Mohist school, from the 5th to 4th century BCE, describes an inverted image formed through a small hole. Ibn al-Haytham's Book of Optics (early 11th century) explained that light travels in straight lines and used a pinhole to study the image of the sun. The camera obscura spread through Europe in the 16th century as a drawing aid. Leon Battista Alberti's De pictura (1435) gave the rules of linear perspective, which describe a scene by its projection onto a plane. That is the same geometry as the pinhole.

The homogeneous coordinates that make the projection a matrix come from 19th-century projective geometry. The matrix form of the camera model is standard in computer vision. Hartley and Zisserman's Multiple View Geometry in Computer Vision (2000) is the reference most people learn it from.

Check yourself

QuestionRecall: what property of light does the pinhole model rely on?Show answer
Light from each scene point travels in a straight line. The ray from a point to the image passes through the single centre of projection, so each scene point lands on one image spot.
QuestionComputation: a camera with focal length f=3f = 3 sees a point at (X,Y,Z)=(2,−1,6)(X, Y, Z) = (2, -1, 6). Where does it land on the image?Show answer
u=3⋅2/6=1u = 3 \cdot 2 / 6 = 1 and v=3⋅(−1)/6=−0.5v = 3 \cdot (-1) / 6 = -0.5. The image point is (1,−0.5)(1, -0.5), in the same units as ff.
QuestionWhy: why can no ordinary matrix perform the pinhole projection directly, and how do homogeneous coordinates fix this?Show answer
Matrix multiplication is linear, and a linear map cannot divide one coordinate by another, but the projection divides by depth ZZ. Homogeneous coordinates keep ZZ as an extra entry and postpone the division to one final step. The projection then becomes a single matrix product followed by that divide.
QuestionApplied: a 640 by 480 camera has fx=fy=800f_x = f_y = 800 pixels, zero skew and principal point (320,240)(320, 240). A point is at camera coordinates (−0.3,0.1,4)(-0.3, 0.1, 4). Where does it land?Show answer
u=800⋅(−0.3)/4+320=−60+320=260u = 800 \cdot (-0.3) / 4 + 320 = -60 + 320 = 260 and v=800⋅0.1/4+240=20+240=260v = 800 \cdot 0.1 / 4 + 240 = 20 + 240 = 260. The pixel is (260,260)(260, 260): left of the centre column and just below the centre row. The left shift comes from the negative XX, and the downward shift from the positive YY.

Sources