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

S7 · Lesson 49 · Image processing lab

The 2D DFT by rows and columns, spatial aliasing, DCT block compression and separable Gaussian blur, in Python

Lessons 28 and 45 made claims about images that are easy to state and worth seeing: the 2D DFT is just 1D DFTs along rows and then columns; a pattern finer than half a cycle per pixel shows up somewhere else entirely; an 8 × 8 block of a smooth image survives being cut to a handful of DCT coefficients; and a Gaussian blur filtered as rows then columns is the same as the full 2D convolution. This lab checks each on small arrays.

  • The curve is the spatial frequency a sampled pattern appears at, against its true frequency, both in cycles per pixel: |u−round(u)|.
  • Below 0.5 cycles per pixel nothing changes; above it the apparent frequency folds back down, reaching zero at every whole cycle per pixel, where the pattern looks perfectly flat.
  • The lab's DFT finds exactly this folded frequency.

Predict

A stripe pattern at 53/64 cycles per pixel is sampled and analyzed with a 64-point DFT. At what frequency does its peak appear?

Name it

What the lab computes

  • 2D DFT: a 1D DFT of every row, then of every column of the result, compared with the direct double sum ∑m∑nx[m,n]e−j2π(km/M+ln/N).
  • Aliasing: sample cos(2πun) for n = 0 to N − 1 and find the strongest DFT bin among 0 to N/2.
  • Block compression: the orthonormal 8 × 8 DCT, X[k,l]=ckcl∑m∑nx[m,n]cos⁡πk(2m+1)16cos⁡πl(2n+1)16; keep the k largest coefficients, invert, and measure PSNR.
  • Gaussian blur: g[i]∝e−i2/2σ2 normalized to sum 1; the 2D kernel is g ⊗ g.

Check

Write dft1(x) and dft2(img): the 2D DFT of a list of rows, computed as row DFTs then column DFTs.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Write alias_freq(u, N=64): sample cos(2πun) for N samples and return the frequency (bin/N, cycles per pixel) of the strongest DFT bin between 0 and N/2.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Write dct2(b), idct2(X) (orthonormal, N × N) and psnr_keep(b, k): keep the k largest-magnitude DCT coefficients of block b, invert, and return the PSNR against b with a 255 peak.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Write gauss(K, sigma): a normalized 1D Gaussian of odd length K, conv2d(img, ker) ("valid" 2D convolution) and blur_sep(img, g): rows then columns with g.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Explain why

Why can a smooth image block be rebuilt well from just a few DCT coefficients?