A baseball stadium photographed with a fisheye lens: the whole stadium fits in a circle and straight lines bend
Photo: Arturo Pardavila III, CC BY 2.0, Wikimedia Commons

Computer Vision, lecture 2

Image formation

How a camera turns light into a grid of numbers

Last time's question

A straight road drawn by a pinhole camera: the two edges and the centre line meet at one point on the horizon. Poles that are equally far apart look closer together further away

Why do the two sides of a straight road meet at one point?

The path of a ray of light

Diagram: light from a lamp hits a surface, bounces off, goes through the camera lens and lands on the sensor
Szeliski (2022), Fig. 2.14
  1. Geometry: where does a point land in the picture?
  2. Lenses: how does a lens collect light?
  3. Light and colour: how bright and what colour?
  4. Numbers: how does the camera turn light into numbers?

Part 1: Geometry

Where does a point in the world land in the picture?

The pinhole camera

Side view of a pinhole camera: an arrow of height X at distance Z in front of a small hole makes an upside-down arrow of height x on a screen at distance f behind the hole
An old engraving of a camera obscura: a dark room with a small hole, and a picture of the garden outside on the wall
Camera obscura, 1755. James Ayscough, public domain, via Wikimedia Commons

Two similar triangles: , so (and ).

Why parallel lines meet

A point on a line going in direction :

  • The end point depends only on the direction , not on the start .
  • So all lines with the same direction meet at one point: the vanishing point.
  • Lines parallel to the picture () stay parallel.

Other camera models

Six drawings of the same 3D object seen with different camera models, including orthographic and perspective
Szeliski (2022), Fig. 2.7

Orthographic: . We simply drop the distance. This works well when the depth of the object is small compared with how far away it is.

From metres to pixels: the matrix K

A picture uses pixels, and pixel (0, 0) is at the top left:

With homogeneous coordinates this is one matrix multiplication:

Where the camera is: R and t

A world point first moves into the camera's view, then it is projected:

  • (rotation) and (translation, a shift): the extrinsics, 6 numbers
  • : the intrinsics, 4 numbers ()
  • : the 3×4 camera matrix

Try it (pairs, 4 minutes)

A camera has pixels and principal point . The camera is at and looks along .

  1. Where does the point (in metres) land?
  2. Where does land? Why?
  3. How far away is a 1.8 m tall person who looks 180 pixels tall?

Field of view

A pinhole triangle: half the angle of view, the focal length f, and half the sensor width W/2
Szeliski (2022), Fig. 2.10
Chart: the field of view goes down from about 104 degrees at 14 mm to about 10 degrees at 200 mm

Part 2: Lenses

A pinhole makes a perfect picture, but lets in almost no light.

Why we need lenses

Four versions of the same cat photo: a tiny hole gives a sharp but very dark and noisy picture; a small hole gives a dim, slightly blurry picture; a large hole gives a bright but very blurry picture; a lens gives a bright and sharp picture
Cat photo: Caleb Woods, CC0, via Wikimedia Commons

Focus and depth of field

A thin lens with focal length 100 mm makes a sharp picture of an object 5 m away on a screen 102 mm behind the lens. Points at other distances make a small blurry circle c
Szeliski (2022), Fig. 2.19

Thin lens rule:

Only one distance is perfectly sharp. A smaller opening makes the blur circle smaller, so more distances look sharp. This range is the depth of field.

Lenses are not perfect

Three photos showing lens distortion: a straight view with barrel distortion, the same view with pincushion distortion, and a fisheye view of a room that covers almost 180 degrees
Szeliski (2022), Fig. 2.13
  • Radial distortion: straight lines bend outwards (barrel) or inwards (pincushion). We describe it with and remove it after calibration.
  • Vignetting: the corners are darker. Chromatic aberration: colours focus at slightly different places.

Part 3: Light and colour

How bright, and what colour?

Surfaces reflect light in different ways

A white marble statue of a woman's head: smooth shading on the cheek and neck, small bright shiny spots, and dark folds
Szeliski (2022), Fig. 2.16
  • Matte (diffuse): light spreads evenly. Brightness depends on the angle between the light and the surface.
  • Shiny (specular): a bright spot that moves when you move.
  • Shadows and folds: little light gets in.

One pixel number mixes light, material, shape and viewpoint.

Colour is three numbers

Two drawings: mixing red, green and blue light makes cyan, magenta, yellow and white; mixing cyan, magenta and yellow inks makes red, green, blue and black
Szeliski (2022), Fig. 2.28
  • Light has many wavelengths (colours of the rainbow), from about 400 to 700 nm.
  • Our eyes have three types of colour cells, so we see only three numbers' worth.
  • Cameras copy this with three filters: R, G, B. Screens add light (left). Printers remove light with ink (right).

Inside the camera

Diagram of the steps inside a camera: light goes through the lens, aperture and shutter; the sensor, gain and converter make RAW data; then demosaicing, noise removal and sharpening, white balance, a brightness curve and compression make a JPEG file
Szeliski (2022), Fig. 2.23

One colour per pixel: the Bayer filter

Left: a 4 by 4 grid of red, green and blue filters, with two greens for each red and blue. Right: the same grid, with the two missing colours at each pixel written in lower case as guesses
Szeliski (2022), Fig. 2.31
  • Each pixel measures only one colour. There are two greens for each red and blue.
  • The two missing colours (lower case, right) are guessed from the neighbours.

Rebuilding colour: demosaicing

Three zoomed views of the edge of a red umbrella: each pixel coloured by its one filter, the single number stored for each pixel in grey, and the full-colour picture rebuilt from it
Umbrella photo: keith ellwood, CC BY 2.0, via Wikimedia Commons

Gamma: the numbers are not the light

Curves of output against input: the camera curve goes up quickly and then flattens; the screen curve does the opposite; a dashed straight line shows "no change"

Stored number ≈ light. More numbers are used for dark tones, where our eyes notice small differences.

Part 4: From signal to numbers

Sampling: how many pixels?

The same cat photo at 960 by 640, 120 by 80, 40 by 26 and 20 by 13 pixels, all shown at the same size; the small ones look like big blocks
Cat photo: Caleb Woods, CC0, via Wikimedia Commons

Quantization: how many levels?

The same grey cat photo with 256, 16, 4 and 2 brightness levels. With 4 and 2 levels, smooth areas become flat patches with false edges
Cat photo: Caleb Woods, CC0, via Wikimedia Commons

Colour spaces

A person with a red umbrella shown as colour, as separate R, G and B channels, as hue, saturation and value channels, and as a mask that keeps only the red umbrella
Umbrella photo: keith ellwood, CC BY 2.0, via Wikimedia Commons

HSV separates hue (which colour) from saturation (how strong) and value (how bright).

Live: finding a colour

import cv2
img = cv2.imread("red-umbrella.jpg")        # BGR, uint8
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)  # H is 0..179 (!)
h, s, v = cv2.split(hsv)

red = ((h <= 8) | (h >= 168)) & (s > 120) & (v > 50)
print(red.mean())                           # 0.028 -> about 3% of the photo

cv2.imwrite("mask.png", red.astype("uint8") * 255)

Compression changes the numbers

Three crops of the same flower photo saved as JPEG with lower and lower quality: the lowest quality shows square blocks and smeared colour
Szeliski (2022), Fig. 2.34

JPEG throws away detail we probably will not notice: blocks, ringing near edges, smeared colour.

Summary

  • Geometry: . Far things look small. Parallel lines meet at vanishing points. Distance is lost. .
  • Lenses: they collect light, but bring focus, depth of field and distortion.
  • Light and colour: one pixel mixes light, material, shape and viewpoint. Colour is three filtered numbers.
  • Inside the camera: Bayer filter → demosaicing → white balance → gamma → JPEG. The file is not raw light.
  • Numbers: sampling (how many pixels), quantization (how many levels), colour space (how to describe colour).

Exit ticket

  1. A point is 10 m away and appears 50 pixels from the centre of the picture. Where would it appear if it were 20 m away, in the same direction?
  2. Why do phone cameras have two green pixels for every red and blue one?
  3. Name two reasons why a pixel number is not a direct measure of the light from that point.

Next lecture: pictures as signals. Histograms, frequency, and why fine stripes can turn into big false patterns.