The 2-D Fourier Transform
An image is a signal in two variables, . Its 2-D Fourier transform decomposes it into plane waves : stripes with a frequency and a direction. Smooth regions live near the center of the spectrum, edges and texture far out, and stripes in the image become bright dots in the spectrum. Every idea of the 1-D course carries over.
Definition
The 2-D DFT is separable: do 1-D FFTs along every row, then along every column. np.fft.fft2 does exactly that.
import numpy as np
rng = np.random.default_rng(0)
img = rng.normal(size=(8, 16))
rows_then_cols = np.fft.fft(np.fft.fft(img, axis=1), axis=0)
print("separable == fft2:", np.allclose(rows_then_cols, np.fft.fft2(img)))separable == fft2: True
Stripes become dots
A stripe pattern has exactly two nonzero DFT coefficients, at and . The direction of the dots is perpendicular to the stripes, and their distance from the center is the stripe frequency.
import numpy as np
M = N = 32
m, n = np.meshgrid(np.arange(M), np.arange(N), indexing="ij")
stripes = np.cos(2 * np.pi * (3 * m / M + 5 * n / N)) # 3 cycles down, 5 across
X = np.fft.fft2(stripes)
nz = np.argwhere(np.abs(X) > 1e-6)
print("nonzero coefficients at (row, col):", nz.tolist())
print("signed frequencies:", [[int(k if k <= M // 2 else k - M), int(l if l <= N // 2 else l - N)] for k, l in nz])
print("value there:", round(abs(X[3, 5]), 6), " = M*N/2 =", M * N // 2)nonzero coefficients at (row, col): [[3, 5], [29, 27]]
signed frequencies: [[3, 5], [-3, -5]]
value there: 512.0 = M*N/2 = 512
Where the energy of an image lives
Natural images have most of their energy at low spatial frequencies; their spectra fall off roughly like in power. A synthetic "image" with a smooth background and one sharp-edged square shows the split:
import numpy as np
M = 64
y, x = np.mgrid[0:M, 0:M] / M
img = 0.5 + 0.3 * np.sin(2 * np.pi * x) * np.cos(2 * np.pi * y) # smooth background
img[20:40, 24:44] += 0.5 # a bright square with sharp edges
F = np.fft.fftshift(np.fft.fft2(img))
ky, kx = np.mgrid[-M // 2:M // 2, -M // 2:M // 2]
radius = np.hypot(kx, ky)
E = np.abs(F) ** 2
for r in (2, 5, 10, 20):
print(f"energy within radius {r:2d} of the center: {100 * E[radius <= r].sum() / E.sum():6.2f} %")energy within radius 2 of the center: 97.66 %
energy within radius 5 of the center: 99.08 %
energy within radius 10 of the center: 99.57 %
energy within radius 20 of the center: 99.84 %
Almost all the energy sits within a few frequency units of the center; the remainder, out to the edges of the spectrum, encodes the square's sharp borders. Throwing away the outer part blurs the edges (Filtering Images); keeping it compactly is the idea of JPEG.
In software
np.fft.fft2,rfft2,fftshift(to put the zero frequency in the center for viewing),scipy.fft.fftnfor 3-D (CT, MRI volumes).- MRI scanners measure the 2-D (or 3-D) Fourier transform of the body directly ("k-space"); the image is its inverse FFT.
Exercises
Exercise 1: Separable cost
How many 1-D FFTs of length 512 does a 2-D FFT need?
Answer
Solution
512 along the rows and 512 along the columns. The total cost is about million butterfly operations, tiny for a computer.
Exercise 2: Rotation
If an image is rotated by 30 degrees, what happens to its 2-D Fourier transform's magnitude?
Answer
It rotates by the same 30 degrees.
Solution
The exponent is a dot product, which rotations preserve; so rotating rotates identically. (For the DFT of a finite image the edges spoil this slightly.)
Exercise 3: Horizontal stripes (code)
Make a image of stripes that vary only down the rows, with 4 cycles, and find its nonzero DFT coefficients.
Answer
and , that is in the row direction.
Solution
import numpy as np
m = np.arange(16)[:, None] * np.ones((1, 16))
img = np.cos(2 * np.pi * 4 * m / 16)
print(np.argwhere(np.abs(np.fft.fft2(img)) > 1e-6).tolist())[[4, 0], [12, 0]]
Test yourself
The 2-D DFT can be computed by…
- one long 1-D FFT of the flattened image
- 1-D FFTs along all rows, then all columns
- sorting the pixels
- a 3-D FFT
A pattern of diagonal stripes appears in the spectrum as…
- a horizontal line
- two dots on the line perpendicular to the stripes
- a uniform gray
- a ring
Sharp edges in an image contribute mainly to…
- the DC coefficient
- high spatial frequencies
- only the phase
- nothing