S7 · Lesson 45 · Two-dimensional signals and images
2D convolution and the 2D DFT, separable filters, edge detection, spatial aliasing and cost
An image is a two-dimensional sequence x[m, n], and nearly everything from the one-dimensional course carries over: convolution, frequency response, the DFT and its fast algorithm, sampling and aliasing. What changes is the cost, since every operation now runs over millions of pixels, and the structure: many useful 2D filters are separable, the product of a row filter and a column filter, which turns a K × K convolution into two passes of K. Blur, sharpening and edge detection in cameras, displays and vision systems are built on these few ideas.
- The curves are the multiplications per pixel for filtering with a K × K kernel: directly, ; separably, 2K; and by 2D FFT on a 1024 × 1024 image, about 84 whatever K is.
- Small kernels are cheapest directly or separably; past about K = 9 an arbitrary kernel is cheaper by FFT, and a separable one stays cheaper, at 2K, until K exceeds 42.
- Gaussian blurs and many edge detectors are separable, which is why they run in real time on video.
Predict
A 7 × 7 Gaussian blur runs on 1080p video (1920 × 1080) at 60 frames a second. About how many multiplications per second does it take directly, and separably?
The video carries 124.4 million pixels a second. Directly each needs 49 multiplications, 6.1 × 10⁹ a second; the Gaussian is separable, so a 7-tap row pass and a 7-tap column pass need 14, 1.7 × 10⁹. Symmetry halves both again in practice.
Name it
Two-dimensional systems
A 2D LTI system is a 2D convolution, , with frequency response over spatial frequencies in radians per pixel (horizontal and vertical).
- Separable kernels, , filter rows then columns: 2K multiplications per pixel instead of , and .
- The 2D DFT, , is separable too: 1D FFTs along every row, then every column, about complex multiplications for an N × N image. Circular convolution, zero padding and fast convolution all work as in 1D.
Common kernels
- Box and Gaussian blurs (lowpass); the Gaussian is separable and has no ringing.
- Sobel edge detector: (a vertical smoothing times a horizontal difference) and its transpose ; the edge strength is .
- Laplacian responds to curvature; subtracting a fraction of it sharpens (unsharp masking does similarly with a blur).
Spatial sampling
A sensor samples space at one sample per pixel, so spatial frequencies above 0.5 cycles per pixel alias: a fine stripe pattern at u cycles per pixel appears at |u − round(u)|, producing moiré. Cameras put optical low-pass filters in front of the sensor, and shrinking an image by 2 needs a lowpass at 0.25 cycles per pixel first, exactly as decimation does in 1D.
Worked example
A 31 × 31 filter is applied to a 1024 × 1024 image. Compare direct convolution with 2D FFT convolution.
Show the solution
- Direct. 961 multiplications per pixel, about in all.
- One FFT. A 1024 × 1024 FFT takes about complex multiplications, 42 million real ones.
- The whole convolution. Forward and inverse plus a pointwise product of 4 per pixel come to about 88 million, eleven times fewer. Zero padding against wraparound adds a little.
- The kernel. The kernel's own FFT is computed once and reused.
Answer about multiplications directly against about 88 million by FFT, eleven times fewer.
Worked example
A vertical edge: the 3 × 3 neighborhood's left column is 10, middle 50 and right 90. What do the Sobel operators give?
Show the solution
- . It weights the right column +1, +2, +1 and the left −1, −2, −1:
- . It compares the top and bottom rows, which are equal: 0.
- Strength and direction. The edge strength is 320 and its direction horizontal (across the edge).
- Why 1–2–1. The 1–2–1 weighting smooths along the edge, so the detector is less sensitive to noise than a plain difference.
Answer = 320 and = 0: an edge of strength 320 pointing horizontally, across the edge.
Worked example
A fabric pattern of 0.7 cycles per pixel is photographed without an optical low-pass filter. What pattern appears?
Show the solution
- Alias. cycles per pixel.
- What appears. A coarse stripe pattern that isn't in the fabric at all, the moiré seen on fine shirts on television.
- Why it stays. Once sampled it can't be removed, since 0.3 cycles per pixel is a perfectly valid frequency.
Answer a moiré stripe pattern at 0.3 cycles per pixel.
Check
A separable K = 9 kernel filters 1280 × 2160 video at 60 frames per second.
(a)
How many billion multiplications a second does direct filtering need?
(b)
And separable filtering?
Check
An N × N image with N = 1024 is transformed by a separable radix-2 2D FFT.
(a)
About how many complex multiplications does it take?
(b)
How many is that per pixel?
Check
An arbitrary kernel can be applied by FFT at about 84 multiplications per pixel.
(a)
Above what kernel size K is the FFT cheaper than direct convolution?
(b)
Above what K is it cheaper than separable filtering?
Check
A pattern of 0.7 cycles per pixel is sampled by a sensor without an optical low-pass filter.
(a)
At what spatial frequency does it appear, in cycles per pixel?
Check
A 3 × 3 neighborhood has every left-column pixel 0, middle 40, right 150.
(a)
What does the Sobel give?
(b)
What does give?
Check
A 1024 × 768 image is shrunk by 2 in each direction.
(a)
Below what spatial frequency, in cycles per original pixel, must the lowpass pass?
(b)
How many pixels does the result have?
Check
Why can a Gaussian blur be run on 4K video in real time when an arbitrary 15 × 15 filter struggles?
Check
A K × K kernel filters N × N images at f images per second.
Step 1
How many multiplications a second does direct filtering need?
Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.
Step 2
If the kernel is separable?
Step 3
By what factor is separable filtering cheaper?
Check
Write conv2d(img, ker): the "valid" 2D convolution (no padding; output smaller by K − 1 each way) of nested lists, and separable(img, col, row): the same result for the kernel col ⊗ row computed as a row pass then a column pass.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Explain why
Why does a camera need an optical low-pass filter in front of its sensor, rather than fixing aliasing in software afterward?
The sensor samples the image at one value per pixel, and any detail finer than half a cycle per pixel folds back to a lower spatial frequency in the samples. Once that has happened, the false coarse pattern is indistinguishable from real image content at that frequency, so no processing of the samples can tell which is which or remove it. The only cure is to remove the too-fine detail before sampling, which in a camera means slightly blurring the light reaching the sensor, exactly as an analog anti-aliasing filter precedes an ADC. Cameras with very high pixel counts sometimes omit it, accepting occasional moiré for extra sharpness.
Practice
Keep practicing
9 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.