v9.1.1
Menu

Frequency Domain Filtering

hardFREEFourier TransformsFrequencyCV: Image Processing

Implement image filtering in the frequency domain using FFT.

Convolution in spatial domain = multiplication in frequency domain: F(f∗g)=F(f)⋅F(g)\mathcal{F}(f * g) = \mathcal{F}(f) \cdot \mathcal{F}(g)

Algorithm:

  1. Compute FFT of image: F=F(I)F = \mathcal{F}(I)
  2. Create frequency domain filter HH (same size as image)
  3. Multiply: G=F⋅HG = F \cdot H
  4. Inverse FFT: Ifiltered=F−1(G)I_{filtered} = \mathcal{F}^{-1}(G)

Common filters:

  • Low-pass: Keep center (low frequencies) → blur
  • High-pass: Keep edges (high frequencies) → sharpen
  • Band-pass: Keep ring of frequencies

Mask definition (centred spectrum):

  • After shifting, the zero frequency sits at (cy,cx)=(⌊h/2⌋,⌊w/2⌋)(c_y, c_x) = (\lfloor h/2 \rfloor, \lfloor w/2 \rfloor). Let d(u,v)d(u, v) be the Euclidean distance of frequency (u,v)(u, v) from that centre and Dmax=cy2+cx2D_{max} = \sqrt{c_y^2 + c_x^2}.
  • The radius is r=cutoff⋅Dmaxr = \text{cutoff} \cdot D_{max}. The mask is 1 where:
    • 'lowpass': d≤rd \le r
    • 'highpass': d≥rd \ge r
    • 'bandpass': 0.5 r≤d≤1.5 r0.5\,r \le d \le 1.5\,r
  • and 0 elsewhere. Return the real part of the inverse transform, same shape as the input.

Example

Input
image = 64×64 image with edges
filter_type = 'lowpass'
cutoff = 0.1
Output
Blurred image (edges smoothed)
Reasoning
  • Input: a concrete 64×6464 \times 64 edge image: every row is 32 zeros then 32 values of 255. The transform treats the image as periodic, so the right border wrapping to the left is a second edge.
  • Spectrum: all rows are identical, so only the centre row (zero vertical frequency) is non-zero. Its centre is 255⋅2048=522240255 \cdot 2048 = 522240; the edge adds odd harmonics of shrinking size (332602332602, 111224111224, 6716667166 at offsets ±1,±3,±5\pm1, \pm3, \pm5).
  • Mask: the centre is (32,32)(32, 32), so Dmax=322+322≈45.25D_{max} = \sqrt{32^2 + 32^2} \approx 45.25 and r=0.1⋅45.25≈4.53r = 0.1 \cdot 45.25 \approx 4.53. Only 69 of 4096 frequencies survive; on the centre row that is the mean plus harmonics 1 and 3.
  • Output: Blurred image (edges smoothed). It is still 64×6464 \times 64 with mean 127.5127.5 (the centre frequency survives), but the 0→255 jump becomes a ramp: −10.2,26.2,80.5,143.4,203.4,249.6-10.2, 26.2, 80.5, 143.4, 203.4, 249.6 across columns 26–36.
  • Notice the overshoot to about −25.2-25.2 and 280.2280.2, outside [0,255][0, 255]: a sharp circular cut rings. A smaller cutoff blurs more.

Constraints

  • image: 2D grayscale array
  • filter_type: 'lowpass', 'highpass', or 'bandpass'
  • cutoff: Cutoff frequency (0-1, fraction of max frequency)
  • Return: Filtered image (real part)

Learn this in

More from CV: Image Processing

solution.py

Test results

Sign in to run your code against the test cases.