S745 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

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, K2; 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?

Name it

Two-dimensional systems

A 2D LTI system is a 2D convolution, y[m,n]=∑k∑lh[k,l]x[m−k,n−l], with frequency response H(ejω1,ejω2) over spatial frequencies in radians per pixel (horizontal and vertical).

  • Separable kernels, h[k,l]=h1[k]h2[l], filter rows then columns: 2K multiplications per pixel instead of K2, and H=H1(ejω1)H2(ejω2).
  • The 2D DFT, X[k,l]=∑m∑nx[m,n]e−j2π(km/M+ln/N), is separable too: 1D FFTs along every row, then every column, about N2log2⁡N 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: Gx=[121][−101] (a vertical smoothing times a horizontal difference) and its transpose Gy; the edge strength is Gx2+Gy2.
  • Laplacian [0101−41010] 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
  1. Direct. 961 multiplications per pixel, about 109 in all.
  2. One FFT. A 1024 × 1024 FFT takes about 10242×10 complex multiplications, 42 million real ones.
  3. 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.
  4. The kernel. The kernel's own FFT is computed once and reused.

Answer about 109 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
  1. Gx. It weights the right column +1, +2, +1 and the left −1, −2, −1: 4×90−4×10=320
  2. Gy. It compares the top and bottom rows, which are equal: 0.
  3. Strength and direction. The edge strength is 320 and its direction horizontal (across the edge).
  4. 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 Gx = 320 and Gy = 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
  1. Alias. |0.7−1|=0.3 cycles per pixel.
  2. What appears. A coarse stripe pattern that isn't in the fabric at all, the moiré seen on fine shirts on television.
  3. 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 Gx give?

(b)

What does Gy 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°.

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?

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.