Gaussian pyramid filtering¶
| Fact | Value |
|---|---|
| Also called | Gaussian pyramid, Pyramid low-pass, Reduce and expand |
| Status | Implemented and tested |
| Family | Wavelet and multiscale filters |
| Engine | Browser |
| Type | Linear, transform, reduce and expand |
| Output | Image |
| Operator shown as | transform (exact) |
| Support radius | global |
| Code | web/src/core/filters/multiscale/pyramid.ts, gaussianPyramidFilter |
| Operator code | web/src/core/filters/multiscale/pyramid.ts, gaussianPyramid |
| Channels | RGB, Brightness only |
Overview¶
Gaussian pyramid filtering shrinks the image to half size a few times and expands it back to full size. Each shrink step blurs a little and keeps every second pixel. Only the detail that the smallest level can hold survives, so the result is a low-pass image. It shows what one level of a Gaussian pyramid contains, and it is the first step toward the Laplacian pyramid.
How it works¶
- Blur the image with the 5 by 5 kernel w along rows and columns.
- Keep every second pixel in each direction, starting with the first.
- Repeat for the chosen number of levels.
- Expand the smallest level back to the size of the level below, again and again, until it has the size of the image.
Mathematical foundation¶
The formula shows the default parameter values.
where
- \(G_k\): Level k of the pyramid. G_0 is the image.
- \(w\): The 5-tap kernel [1, 4, 6, 4, 1] / 16
- \(L\): The number of levels
- \((i, j)\): Pixel position in a level
G_0 is the image and G_{k+1} is level k reduced. REDUCE filters level k with the 5-tap kernel w along rows and columns. It keeps every second pixel, starting with the first, so a level of n pixels gives ceil(n / 2). EXPAND goes back up. Each output pixel sums the coarse pixels that land on it, weighted by 2 w(m) along each axis. So a flat level stays flat. Burt and Adelson define w by one number a, with w(0) = a, w(1) = 1/4 and w(2) = 1/4 - a/2. This filter uses a = 0.375, which gives the binomial weights [1, 4, 6, 4, 1] / 16. The result is the top level G_L expanded L times, back to the size of the image. It is a low-pass image. The filter is linear, but not shift invariant. Moving the image by one pixel can change the result by more than a shift, since the pixels kept by REDUCE change.
A Gaussian pyramid holds the image at a series of sizes, each half as wide and half as tall as the one before. REDUCE blurs with a small kernel before it drops every second pixel. So most detail too fine for the smaller grid is removed first, which keeps aliasing low. EXPAND fills the dropped pixels back in by interpolation. Burt and Adelson chose the kernel w by a few rules. It is symmetric and adds up to 1. Also, every pixel of a level gives the same total weight to the level above. The filter is linear but not shift invariant. The worked example shows why.
Kernel or operator¶
The Inspector shows the stages of the pyramid as formulas. They are the REDUCE step with the kernel w = [1, 4, 6, 4, 1] / 16, the EXPAND step, and the number of levels.
Algorithm¶
- Set G_0 to the image.
- Reduce G_k to G_{k+1} for each level, with the border mode at the edges of each level.
- Expand the top level G_L back to the size of the level below, L times, until it has the size of the image.
Parameters¶
| Parameter | Default | Values | Meaning |
|---|---|---|---|
Levels levels |
2 | 1 to 8 levels | How many times the image is reduced. Each level halves the width and the height. The pyramid stops early when a level is one pixel. |
Border mode border |
Reflect 101 (default) | Reflect 101 (default), Reflect, Replicate, Constant (zero), Wrap | How pixels outside the image are filled when the window reaches past the edge. |
Channels channelScope |
RGB | RGB, Brightness only | Which part of the color image the filter changes. |
Implementation¶
gaussianPyramidFilter in web/src/core/filters/multiscale/pyramid.ts builds the levels with gaussianPyramid and expands the top level with expand. REDUCE and EXPAND run along rows, then columns. On a large image at low zoom, the preview runs on a smaller copy, and the number of levels drops by one for each halving of the size.
Worked example¶
| Input | x = 6 | x = 7 | x = 8 | x = 9 | x = 10 | x = 11 | x = 12 | x = 13 | x = 14 | x = 15 |
|---|---|---|---|---|---|---|---|---|---|---|
| 1 at x = 10 | 0.0078 | 0.0313 | 0.0938 | 0.2188 | 0.2969 | 0.2188 | 0.0938 | 0.0313 | 0.0078 | 0 |
| 1 at x = 11 | 0 | 0 | 0.0313 | 0.1250 | 0.2188 | 0.2500 | 0.2188 | 0.1250 | 0.0313 | 0 |
One level, on a line of 21 pixels that is 0 except for a single 1. The two inputs differ only by a shift of one pixel, but the outputs differ by more than a shift. The 1 at x = 10 sits on a pixel that REDUCE keeps. Its response reaches from x = 6 to x = 14, with a peak of 0.2969. The 1 at x = 11 falls between two kept pixels. Its response reaches only from x = 8 to x = 14, with a lower peak of 0.25. Both responses add up to 1.
Complexity¶
- Time: \(O(N)\)
- Memory: \(O(N)\)
N is the number of pixels. Each level has about a quarter of the pixels of the level below, so all levels together have about 4 N / 3.
Artifacts and limitations¶
Limitations
- Not shift invariant. The result depends on where the image falls in the pyramid grid.
- The blur size can only grow in steps of 2.
Artifacts
- Faint blocky or grid-like patterns at high level counts, from the repeated factor 2 sampling.
Tests¶
The reference builds the same REDUCE and EXPAND with NumPy, pixel by pixel, and covers 1 to 3 levels and three border modes. More tests check the level sizes, that a flat image stays flat, and that a shift of the input by one pixel changes the response by more than a shift.
web/src/core/filters/filters.test.tsweb/src/core/filters/run.test.tsweb/src/core/filters/multiscale/multiscale.test.ts
References¶
- Burt, P. J. and Adelson, E. H. The Laplacian pyramid as a compact image code. IEEE Transactions on Communications 31(4), 532 to 540, 1983. DOI 10.1109/TCOM.1983.1095851.