The idea: scale the waves, not the pixels
Any image can be written as a sum of 2-D waves of different speeds and directions. The 2-D discrete Fourier transform (DFT) finds how much of each wave is in the image: that list is the spectrum F. To filter, multiply every value of F by a number H(u, v) and transform back. A low-pass filter keeps the slow waves and blurs; a high-pass filter keeps the fast waves and leaves the edges; a notch filter removes a few chosen waves.
The page uses a 16×16 image: an 8×8 square of brightness 200 on a background of 60. That is small enough to compute every one of the 256 DFT values exactly in the browser. The five panels are the image f, the spectrum |F|, the filter H, the product G = F·H and the output g. Below them, row 8 of f and g is drawn as a profile, so you can read the actual numbers.
The 2-D DFT is rows, then columns
For an M×N image the DFT is F(k, l) = Σr Σc f(r, c) · e−2πi(kr/M + lc/N). The double sum splits: first do a 1-D DFT of every row, then a 1-D DFT of every column of that result. This is why the 2-D transform costs about 2N times a 1-D one, and why any 1-D FFT can be reused for images (see Fourier Series and the FFT for the 1-D DFT and the FFT itself).
F(0, 0) is the plain sum of all pixels, the DC term: 256 × 95 = 24 320 for the square. The values come out with (0, 0) at the top-left corner. Swapping the four quadrants (fftshift) moves it to the centre, so the distance D(u, v) = √(u² + v²) from the centre is the frequency: near the centre the waves change slowly, at the edges they change every pixel. Spectra are shown on a log scale, log(1 + |F|), because the DC is many times bigger than the rest (Demo 1: the spectrum of a square).
| what | value for the square |
|---|---|
| DC, F(0, 0) | 24 320 (71.1 % of all energy) |
| |F(±1, 0)|, |F(±3, 0)|, |F(±5, 0)| | 5740.9, 2015.9, 1347.0 |
| |F(±2, 0)|, |F(±4, 0)|, |F(±6, 0)| | 0: an 8-wide box in 16 pixels has no even harmonics |
| non-zero values | 81 of 256: u and v each 0 or odd, a 9×9 lattice |
Energy is the sum of |F|². By Parseval's theorem it equals 256 times the sum of the squared pixels, so "energy kept" after filtering is simply Σ|G|² / Σ|F|².
Low-pass: ideal vs Gaussian
The ideal low-pass filter keeps every frequency with D ≤ D0 and drops the rest: H is a sharp disc. The Gaussian low-pass H = exp(−D² / (2D0²)) has no edge: 1 at the centre, 0.61 at D = D0, fading out.
Both blur the square, but they differ at its edges. With D0 = 3 (Demo 2: ideal low-pass, D0 = 3: ringing, Demo 3: Gaussian low-pass, D0 = 3: no ringing):
| D0 = 3 | ideal low-pass | Gaussian low-pass |
|---|---|---|
| frequencies passed | 29 of 256 | all, weighted |
| energy kept | 95.4 % | 91.8 % |
| output range (input 60 … 200) | 32.0 … 215.5 | 59.97 … 200.05 |
| overshoot / undershoot | 15.5 / 28.0 | both under 0.1 |
This ringing is the same overshoot as the Gibbs effect of a Fourier series: cutting the series off sharply leaves ripples next to every jump. The inverse DFT of a sharp disc is a sinc-like pattern with negative side lobes, and multiplying by H is the same as convolving the image with that pattern. The Gaussian's inverse is again (nearly) a Gaussian with no real negative lobes, so it only averages: on this grid the overshoot is under 0.1. It keeps less energy at the same D0, yet looks cleaner. Try D0 = 2, 4 and 6 with the D0 menu: the ideal filter rings at every size.
High-pass: edges
A high-pass filter is 1 − low-pass. It removes the DC, so the output has mean 0 and is drawn with grey = 0, dark = negative, light = positive. What is left are the places where the image changes: the border of the square. On row 8 with D0 = 3 the Gaussian high-pass gives −37.3 just outside the edge, +37.3 just inside, and 0.0 in the flat parts (Demo 5: Gaussian high-pass, D0 = 3: clean edges). The ideal high-pass finds the edge too (−37.4 | +45.6) but rings: the flat background at column 0 reads 20.3 instead of 0 (Demo 4: ideal high-pass, D0 = 3). Edge detectors usually work in space with small kernels instead; see Canny Edge Detection.
Notch: removing a repeating pattern
A pattern that repeats across the whole image, such as scan lines or mains-hum stripes, is one wave, so in the spectrum it is just a pair of bright dots. The second image adds 40·cos(πc/2) to every row: a wave that repeats every 4 columns, so it sits at (u, v) = (±4, 0) with |F| = 5120 each. A notch filter sets H = 0 on those two dots and 1 everywhere else. The square happens to have exactly 0 at (±4, 0), so the output is the clean square again, to within 1e-13 (Demo 6: a notch filter removes the stripes).
A low-pass with D0 = 3 removes the stripes too, since D = 4 is outside the circle, but it blurs and rings the square exactly as in Demo 2: the output is the same to 1e-13 (Demo 7: low-pass on the stripes blurs the square too). The notch keeps 94.1 % of the energy, the low-pass 89.7 %.
The convolution theorem
Multiplying spectra is the same as convolving images: if g = f ⊛ h (circular convolution), then G = F · H with H the DFT of the kernel h padded to the image size. The page checks it on a 3×3 box blur, nine weights of 1/9 (Demo 8: convolution = multiplication (3×3 box)). The kernel is padded to 16×16 with its centre at pixel (0, 0), so its cells wrap around the corners. Its DFT is real, H(u, v) = (1 + 2cos(2πu/16)) · (1 + 2cos(2πv/16)) / 9: 1 at the centre, −1/3 at the far edges. The two ways agree on every pixel, for example pixel (4, 4) = (4·200 + 5·60) / 9 = 122.22, with a largest difference of about 1e-13 (rounding). For the sliding-window view of the same operation see Convolution.
| in space | in frequency | |
|---|---|---|
| work for one N×N image, k×k kernel | N² · k² multiply-adds | 3 FFTs (about N² log N each) + N² products |
| wins when | the kernel is small (3×3, 5×5) | the kernel is large, or H is designed directly |
| edges of the image | pad as you like | wraps around unless the image is padded first |
See also JPEG and the DCT, which drops high frequencies of 8×8 blocks to save space, Sampling and Aliasing for why the highest frequency is half the sample rate, and Histogram Equalization for filters that look at one pixel only.
See also FIR and IIR filters, which low-pass filters a 1-D signal one sample at a time with a windowed sinc, a moving average and a feedback filter.
What the page leaves out
Real images are hundreds of pixels wide and use the FFT, not direct sums. To avoid the wrap-around of circular convolution, images are padded to twice their size before filtering, and often multiplied by (−1)r+c instead of shifting the spectrum. The page also leaves out Butterworth filters (between ideal and Gaussian), band-pass and band-reject rings, homomorphic filtering, the phase of F (the page only shows |F|, but the phase holds most of the shape), and colour images.