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: .
- 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?
53/64 exceeds 0.5, so it aliases to 1 − 53/64 = 11/64. The DFT has no way to know the pattern was finer; once sampled, 11/64 cycles per pixel is what the pixels contain.
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 .
- 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, ; keep the k largest coefficients, invert, and measure PSNR.
- Gaussian blur: 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?
The DCT expresses the block as a weighted sum of two-dimensional cosine patterns of increasing frequency. A block whose brightness varies smoothly, as most regions of natural images do, is closely matched by a few low-frequency patterns: a constant, gentle gradients in each direction and a slight curvature. The coefficients of the high-frequency patterns, which describe fine detail and sharp edges, are small or zero. Keeping only the few large coefficients and discarding the rest therefore changes the pixels very little, which is what lets a compressor spend its bits on the coefficients that matter.