Search

Search papers and lessons

Image Pyramid

Level 2 · 11 min read

An image pyramid is a stack of copies of one picture, each blurred and shrunk to half the width and height of the one before, so a program can look at the scene at many scales.

On this page

  • Colour = topic mix
  • Probability
  • Discrete math
  • Calculus
  • Linear algebra
  • Signal processing
  • Architectures
  • ReadReady to readNot yet

TL;DR

An image pyramid is a stack of versions of one picture. Level 0 is the original. Each next level is blurred and then shrunk to half the width and height, so the top of the stack is a tiny copy of the whole scene. Programs use it to find things of different sizes, and to search coarse to fine, because a small copy is cheap to scan.

Builds on: Digital Image, Convolution, Gaussian (Normal) Distribution, Nyquist–Shannon Sampling Theorem · Leads to: Local Image Features, Object Detection, Feature Pyramid Networks for Object Detection

Where it's used

Multi-scale detection. Early face and object detectors ran a fixed-size window over a pyramid of resized copies of the photo. A window 24 pixels wide can only fit objects about 24 pixels wide. A copy shrunk by two makes a 48-pixel object fit that window, so one detector covers many sizes.

Matching across zoom levels. Photo-stitching and image-search systems compare Local Image Features in two pictures taken at different distances. They match at a coarse level first, which finds the rough alignment cheaply, then refine at full resolution.

Blending and compression. Burt and Adelson's Laplacian pyramid blends two photos seamlessly by mixing each level separately. Coarse shapes blend over a wide area and fine edges over a narrow one, so a seam is hard to see.

Feature pyramids in modern networks. Feature Pyramid Networks (2017) build the same stack out of a convolutional network's feature maps instead of pixels. Feature Pyramid Networks for Object Detection shows how this lets one detector find objects of all sizes in a single pass.

How it works

Take one row of eight pixels that alternates dark and bright: 0, 8, 0, 8, 0, 8, 0, 8. Build the next level, which should have four pixels.

flowchart LR
  A["Level 0: 8 pixels"] --> B["Blur with weights 1, 2, 1 over 4"] --> C["Keep every second pixel"] --> D["Level 1: 4 pixels"]
  1. Shrink naively. Keep every second pixel: positions 0, 2, 4, 6. You get 0, 0, 0, 0. Check: the row's average is 4, but the shrunken row is black. The pattern has been lost.
  2. Blur first. Replace each pixel by a weighted average of itself and its two neighbours, with weights 1, 2, 1 divided by 4. For the bright pixel at position 1, the neighbours are both 0, so the value is (0 + 2·8 + 0) / 4 = 4. At the ends, copy the edge pixel as its own neighbour. The result is 2, 4, 4, 4, 4, 4, 4, 6. Check: the middle values all sit at 4, the average.
  3. Shrink. Keep positions 0, 2, 4, 6 of the blurred row. You get 2, 4, 4, 4. Check: half the pixels, and the grey level of the original average.
  4. Repeat for level 2. Blur 2, 4, 4, 4 with the same weights and copying at the ends. You get 2.5, 3.5, 4, 4. Keep positions 0 and 2: 2.5, 4. Check: the level has two values from four.
  5. Stack the levels. Level 0 has 8 pixels, level 1 has 4, level 2 has 2. In two dimensions the same work is done along rows, then along columns, so each level has a quarter of the pixels of the one below.

The blur is what makes step 2 work. It is a low-pass filter: it keeps slow changes and weakens fast ones.

00.10.20.30.40.500.20.40.60.81frequency f (cycles per original pixel)H(f)
  • blur response
The 1, 2, 1 blur keeps slow changes (f near 0) and removes the dark-bright alternation at f = 1/2 completely. At f = 1/4, the highest frequency a half-size grid can hold, it still keeps half.

The math

Definition: Gaussian pyramid
Level 0 is the image itself, G0=IG_0 = I. Level ll is made from level l−1l-1 by blurring with weights ww (non-negative, summing to 1) and then keeping every second sample in each direction: Gl(i,j)=∑m∑nw(m,n) Gl−1(2i+m,  2j+n)G_l(i, j) = \sum_{m} \sum_{n} w(m, n)\, G_{l-1}(2i + m,\; 2j + n).

The formula is the one-dimensional walkthrough written in two dimensions, with a bell-shaped set of weights in place of 1, 2, 1. Real systems use a longer kernel such as the five-tap binomial [1,4,6,4,1]/16[1, 4, 6, 4, 1]/16, which is close to a Gaussian.

Step 1 in symbols: the blur

For a row aa, with weights 14,12,14\frac14, \frac12, \frac14 on the left neighbour, the pixel and the right neighbour, and the end pixel copied at each edge:

b(i)=14 a(i−1)+12 a(i)+14 a(i+1)b(i) = \frac14\, a(i-1) + \frac12\, a(i) + \frac14\, a(i+1)

Step 2 in symbols: keep every second sample

c(i)=b(2i)c(i) = b(2i)

Step 3 in symbols: the recursion

Let BB stand for the blur and DD for keeping every second sample. Then each level is

Gl=D(B(Gl−1))G_l = D\big(B(G_{l-1})\big)

Why the blur must come first

Measured in original pixels, a pattern that repeats every 1/f1/f pixels has frequency ff in cycles per pixel. The blur multiplies each such pattern by

H(f)=12+12cos⁡(2πf)H(f) = \frac12 + \frac12 \cos(2\pi f)

For the checkerboard, f=12f = \frac12, so H=12+12cos⁡(π)=0H = \frac12 + \frac12 \cos(\pi) = 0. The blur removes it exactly, which is why the blurred row in step 2 is flat at 4 in the middle. After keeping every second sample, the new grid can only hold frequencies up to 14\frac14 cycle per original pixel. For f=14f = \frac14, H=12+12cos⁡(π2)=12H = \frac12 + \frac12 \cos(\frac{\pi}{2}) = \frac12. The blur keeps it, but damps it, so it survives without folding back.

Sizes

If the photo has N×MN \times M pixels, level ll has about N2l×M2l\frac{N}{2^l} \times \frac{M}{2^l} pixels. Adding up the levels gives 1+14+116+⋯1 + \frac14 + \frac1{16} + \cdots, which approaches 43\frac43. A full pyramid costs about a third more memory than the image alone.

Worked examples

Example
1. A single edge, by hand

Start with the row a=[0,0,8,8]a = [0, 0, 8, 8]. Blur with weights 14,12,14\frac14, \frac12, \frac14, copying the end pixel at each edge.

  1. Pixel 0: the neighbours are a copy of itself (0) and pixel 1 (0). 14⋅0+12⋅0+14⋅0=0\frac14 \cdot 0 + \frac12 \cdot 0 + \frac14 \cdot 0 = 0.
  2. Pixel 1: left neighbour 0, itself 0, right neighbour 8. 14⋅0+12⋅0+14⋅8=2\frac14 \cdot 0 + \frac12 \cdot 0 + \frac14 \cdot 8 = 2.
  3. Pixel 2: left neighbour 0, itself 8, right neighbour 8. 14⋅0+12⋅8+14⋅8=6\frac14 \cdot 0 + \frac12 \cdot 8 + \frac14 \cdot 8 = 6.
  4. Pixel 3: left neighbour 8, itself 8, copied right neighbour 8. The value is 8.
  5. The blurred row is [0,2,6,8][0, 2, 6, 8]. Keep positions 0 and 2: the level-1 row is [0,6][0, 6].
  6. Interpretation: the sharp step from 0 to 8 has become a ramp. The bright sample at position 2 reads 6, not 8, because its dark neighbour pulled it down.
Example
2. The standard case: two levels of a step

Start with the row a=[0,0,0,8,8,8,8,8]a = [0, 0, 0, 8, 8, 8, 8, 8].

  1. Blur. Pixels 0 and 1 are 0. Pixel 2: 14⋅0+12⋅0+14⋅8=2\frac14 \cdot 0 + \frac12 \cdot 0 + \frac14 \cdot 8 = 2. Pixel 3: 14⋅0+12⋅8+14⋅8=6\frac14 \cdot 0 + \frac12 \cdot 8 + \frac14 \cdot 8 = 6. Pixel 4 and the pixels after it: 8. The blurred row is [0,0,2,6,8,8,8,8][0, 0, 2, 6, 8, 8, 8, 8].
  2. Keep positions 0, 2, 4, 6. Level 1 is [0,2,8,8][0, 2, 8, 8].
  3. Blur level 1 again. Pixel 0: 14⋅0+12⋅0+14⋅2=0.5\frac14 \cdot 0 + \frac12 \cdot 0 + \frac14 \cdot 2 = 0.5. Pixel 1: 14⋅0+12⋅2+14⋅8=3\frac14 \cdot 0 + \frac12 \cdot 2 + \frac14 \cdot 8 = 3. Pixel 2: 14⋅2+12⋅8+14⋅8=6.5\frac14 \cdot 2 + \frac12 \cdot 8 + \frac14 \cdot 8 = 6.5. Pixel 3: 8. The blurred row is [0.5,3,6.5,8][0.5, 3, 6.5, 8].
  4. Keep positions 0 and 2. Level 2 is [0.5,6.5][0.5, 6.5].
  5. Interpretation: eight values have become two. The edge has spread over more pixels at each level, and the coarse level still shows a dark-to-bright change, now as 0.5 and 6.5.
Example
3. A real photo: sizes and scale

Take a photo of 1024×7681024 \times 768 pixels.

  1. Level 0 has 1024×768=786,4321024 \times 768 = 786{,}432 pixels.
  2. Level 1 has 512×384=196,608512 \times 384 = 196{,}608 pixels.
  3. Level 2 has 256×192=49,152256 \times 192 = 49{,}152 pixels.
  4. Level 3 has 128×96=12,288128 \times 96 = 12{,}288 pixels.
  5. Levels 0 to 3 together hold 1,044,4801{,}044{,}480 pixels. Divided by the original count, that is 1.3281.328, close to 43\frac43.
  6. Scale: a face 96 pixels wide in the photo is 96 / 4 = 24 pixels wide at level 2. A 24-pixel window fits it there.

Try it: a face is 48 pixels wide in the original photo. At which level does a 24-pixel window fit it? Answer: level 1, because 48 / 2 = 24.

Common mistakes and tradeoffs

It is tempting to think that shrinking by keeping every second pixel is enough. In fact it aliases: the checkerboard in the walkthrough vanishes into black. Blur first, because the blur removes the detail that the smaller grid cannot represent.

It is tempting to think a pyramid is the same as resizing the photo with a smooth interpolation such as Bilinear Interpolation. In fact interpolation does not remove the high frequencies, so aliasing remains. A pyramid also builds each level from the one before, so the whole stack costs about a third more memory, while resizing from the original again for every scale costs a full pass each time.

It is tempting to think a pyramid makes a detector scale invariant by itself. In fact it only gives the detector each scale it searches. The detector must still recognise the object at that scale, and an object whose size falls between two levels is only seen at a factor of two off, which is coarse.

Use a pyramid when you need to search at many scales, match coarse to fine, blend images, or compress images in layers. Do not use it when a modern network already shrinks its own feature maps with strided convolution or pooling, as Max Pooling does. Feature pyramids inside the network reuse those maps and avoid rescaling the input for every scale. A Laplacian pyramid stores the detail that each blur removes, so the original image can be rebuilt exactly; a Gaussian pyramid does not.

History

In the 1970s and early 1980s, picture-processing researchers used hierarchical image structures to speed up search. In 1983 Peter Burt and Edward Adelson published the Laplacian pyramid as a compact image code. Their construction made the blurred, shrunken stack the standard way to hold an image at several scales, and it spread into compression and blending.

In 2004 David Lowe's SIFT features searched for keypoints across a scale space organised into octaves. Each octave halves the image, which is the same idea as a pyramid level. In 2017 Lin and colleagues' Feature Pyramid Networks moved the pyramid inside a convolutional network, building it from feature maps instead of pixels. That is how most modern detectors handle objects of many sizes. The idea caught on because it is simple and uses the same convolution found everywhere else in the pipeline.

Recap

  • An image pyramid is a stack of levels. Each level is blurred and then shrunk by two in each direction.
  • The blur is a low-pass filter, a weighted average with bell-like weights. It must come before shrinking, or the pattern aliases, as the Nyquist–Shannon Sampling Theorem predicts.
  • Each level has a quarter of the pixels of the one below. The whole stack costs about 4/3 of the original image.
  • Searching a stack of scales lets one fixed-size window find objects of many sizes.
  • Read next: Local Image Features for matching points across scales, Object Detection for the task that uses scales, and Feature Pyramid Networks for Object Detection for the pyramid built inside a network.

Check yourself

QuestionRecall: what does the blur do before a pyramid level is shrunk?Show answer
It removes detail finer than the new grid can show, that is, frequencies above 1/4 cycle per original pixel. Without it, those frequencies fold back as false patterns. This is aliasing.
QuestionCompute: the row a=[0,0,8,8]a = [0, 0, 8, 8] is blurred with weights 14,12,14\frac14, \frac12, \frac14, copying the end pixel at each edge, and then every second value is kept. What is the level-1 row?Show answer
The blurred row is [0,2,6,8][0, 2, 6, 8]. Keeping positions 0 and 2 gives [0,6][0, 6]. Check: pixel 1 is 14⋅0+12⋅0+14⋅8=2\frac14 \cdot 0 + \frac12 \cdot 0 + \frac14 \cdot 8 = 2, and pixel 2 is 14⋅0+12⋅8+14⋅8=6\frac14 \cdot 0 + \frac12 \cdot 8 + \frac14 \cdot 8 = 6.
QuestionWhy: why does a pyramid let one fixed-size detector find large objects?Show answer
Each level is a shrunken copy of the photo. A large object in level 0 becomes a smaller object at a higher level. A window that fits small objects therefore fits the large ones at the level where they have shrunk to its size.
QuestionApplied: a 2048×15362048 \times 1536 photo has a face 64 pixels wide. At which level, counting the photo as level 0, is the face 16 pixels wide, and how big is the image at that level?Show answer
Level 2, because 64 / 4 = 16. The image there is 2048/4=5122048 / 4 = 512 by 1536/4=3841536 / 4 = 384, so 512×384512 \times 384.
QuestionWhy not: why might a modern network skip building an image pyramid of its input?Show answer
Strided convolution and pooling already shrink the feature maps inside the network, and a feature pyramid reuses those maps. Shrinking the input again for every scale repeats work the network has already done.

Sources