TExcelTAD
Home › Fourier Analysis & Signals › The 2-D Fourier Transform

The 2-D Fourier Transform

An image is a signal in two variables, f(x,y)f(x, y). Its 2-D Fourier transform decomposes it into plane waves e2πi(ux+vy)e^{2\pi i (u x + v y)}: 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

f^(u,v)=∬f(x,y) e−2πi(ux+vy) dx dy,X[k,l]=∑m=0M−1∑n=0N−1x[m,n] e−2πi(km/M+ln/N)\hat{f}(u, v) = \iint f(x, y)\, e^{-2\pi i (ux + vy)}\, dx\, dy, \qquad X[k, l] = \sum_{m=0}^{M-1}\sum_{n=0}^{N-1} x[m, n]\, e^{-2\pi i (km/M + ln/N)}

The 2-D DFT is separable: do 1-D FFTs along every row, then along every column. np.fft.fft2 does exactly that.

python · try it yourself
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 cos⁡(2π(k0m/M+l0n/N))\cos(2\pi(k_0 m/M + l_0 n/N)) has exactly two nonzero DFT coefficients, at (k0,l0)(k_0, l_0) and (−k0,−l0)(-k_0, -l_0). The direction of the dots is perpendicular to the stripes, and their distance from the center is the stripe frequency.

python · try it yourself
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 1/f21/f^2 in power. A synthetic "image" with a smooth background and one sharp-edged square shows the split:

python · try it yourself
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.fftn for 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 512×512512 \times 512 2-D FFT need?

Answer
Solution

512 along the rows and 512 along the columns. The total cost is about 2⋅512⋅512log⁡2512≈4.72 \cdot 512 \cdot 512 \log_2 512 \approx 4.7 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 ux+vyux + vy is a dot product, which rotations preserve; so rotating ff rotates f^\hat{f} identically. (For the DFT of a finite image the edges spoil this slightly.)

Exercise 3: Horizontal stripes (code)

Make a 16×1616 \times 16 image of stripes that vary only down the rows, with 4 cycles, and find its nonzero DFT coefficients.

Answer

(4,0)(4, 0) and (12,0)(12, 0), that is ±4\pm 4 in the row direction.

Solution
python · try it yourself
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…

  1. one long 1-D FFT of the flattened image
  2. 1-D FFTs along all rows, then all columns
  3. sorting the pixels
  4. a 3-D FFT

A pattern of diagonal stripes appears in the spectrum as…

  1. a horizontal line
  2. two dots on the line perpendicular to the stripes
  3. a uniform gray
  4. a ring

Sharp edges in an image contribute mainly to…

  1. the DC coefficient
  2. high spatial frequencies
  3. only the phase
  4. nothing