Fourier for Images
Seeing straight/Learn/Fourier

What an image's spectrum means, and why it finds the tilt

The OpenCV tutorial, the HIPR2 Fourier page and the DSP StackExchange thread, explained section by section, with the missing equations restored, three things to play with, and a note on how each idea shows up in jdeskew.

OpenCV-Python: Fourier TransformHow to compute an image's spectrum, filter it, and pad for speed
Open tutorial
SECTION 1

Theory: an image is a signal in two directions

  • A sine wave sin(2π f t) has a spectrum with one spike at f, the winding frequency where the centre of mass jumps.
  • Once a signal is sampled into N values, its spectrum repeats every N bins, so the DFT shows one period of N frequencies.
  • An image is sampled along x and y, so the 2-D DFT winds it in both directions at once. Each point (u, v) asks how much of a stripe pattern with u cycles across and v cycles down the image contains.
  • Slow changes (sky, skin) are low frequencies near the centre. Fast changes (edges, fine text, noise) are high frequencies far from it.
SECTION 2

FFT in NumPy

f = np.fft.fft2(img)                 # complex array, one value per stripe pattern
fshift = np.fft.fftshift(f)          # move zero frequency (DC) from the corner to the centre
spectrum = 20*np.log(np.abs(fshift)) # magnitude only, on a log (dB) scale
  • Each complex value is a centre of mass: its length says how much of that pattern is present, its angle (phase) where the pattern's crests sit.
  • fftshift puts the average brightness (DC) in the middle so the picture is symmetric and readable.
  • abs keeps the magnitude. Tilt is about which patterns exist, not where they sit, so jdeskew drops the phase.
  • log because DC is millions of times larger than the rest.
  • A larger output size makes NumPy pad with zeros. Keep that in mind for section 5.
SECTIONS 3 AND 4

Filtering in the spectrum, and why hard masks ring

Zero the centre (high-pass) and only edges survive. Keep only the centre (low-pass) and the image blurs. A hard-edged mask causes ripples (ringing), because a sharp box in one domain is a sinc in the other. A Gaussian mask has soft edges and does not ring. Try it below on a synthetic test image.

Filter
Mask edge
Radius
14
Input, 128 × 128.
log |FFT|; the dimmed area is removed.
After the inverse FFT.
SECTION 4

The same in OpenCV

dft = cv.dft(np.float32(img), flags=cv.DFT_COMPLEX_OUTPUT)    # 2 channels: real, imaginary
mag = cv.magnitude(dft_shift[:,:,0], dft_shift[:,:,1])        # sqrt(re² + im²)

Same maths. OpenCV wants float32, returns real and imaginary parts as two channels, and runs about 3× faster than NumPy here.

SECTION 5

Padding to an optimal size

nrows = cv.getOptimalDFTSize(342)    # 360: a product of 2s, 3s and 5s
nimg = cv.copyMakeBorder(img, 0, bottom, 0, right, cv.BORDER_CONSTANT, value=0)   # about 4× faster
SECTION 6

Why Laplacian and Sobel are high-pass filters

Take the FFT of a small kernel and you see what it lets through. Mean and Gaussian are bright at the centre (low-pass, blur). Laplacian is dark at the centre (high-pass, edges). Sobel x and Sobel y pass frequencies along one axis (edges in one direction). The reason: convolving an image with a kernel multiplies their spectra (the convolution theorem). Our structure-tensor baseline is built from Sobel gradients.