Fourier Transform in Images

The Fourier transform rewrites a signal as a weighted sum of sinusoids. The building block is , with amplitude , angular frequency and phase . Low frequencies encode coarse structure, and high frequencies encode fine detail. For a 1D signal the transform and its inverse are

Each value is a complex number. Its magnitude is the amplitude at frequency , and its angle is the phase. Since , the pair weights a cosine and a sine of the same frequency, which together form one shifted sinusoid. For samples, the discrete Fourier transform (DFT) is a change of basis with an invertible matrix .

In 2D, each frequency is a grating of sinusoidal stripes. The stripes run perpendicular to the vector , and the distance from the origin sets how fine they are. The standard display is np.log(np.abs(np.fft.fftshift(np.fft.fft2(im)))). Here fftshift moves zero frequency, the DC term that measures mean brightness, to the center, and the log compresses the huge range of magnitudes. Vertical stripes give two bright dots on the horizontal frequency axis. A strong vertical edge gives a bright line along the horizontal frequency axis, perpendicular to the edge. Natural photos concentrate their energy near the center. A local change in one domain causes a global change in the other. The phase carries most of the visible structure, so an image rebuilt from one photo’s amplitude and another photo’s phase looks like the second photo.

The convolution theorem turns filtering into multiplication. With denoting the Fourier transform,

Convolution in the spatial domain is multiplication in the frequency domain. A Gaussian of standard deviation transforms to a Gaussian of standard deviation in . A box transforms to a sinc, and a sinc to a box. Keeping only the center of the spectrum is a low-pass filter, removing the center is a high-pass filter, and keeping a ring is a band-pass filter. JPEG applies a related transform, the discrete cosine transform, to blocks (2.9. JPEG Compression).

Two Frequency Conventions

The transform slide uses angular frequency in radians per unit length, with and a factor in the inverse. The transform-pairs slide uses ordinary frequency in cycles per unit length, , so . That slide labels the Gaussian pair . The width is exact in . In , the transformed Gaussian has standard deviation . Both versions say that a wider kernel has a narrower spectrum. In NumPy, index of fft2 along an axis of length means cycles across the image, or cycles per pixel.

Phase Formula on the Slide

The slide writes and . The ratio cannot distinguish from , and the on the amplitude absorbs that ambiguity. The usual convention keeps and uses , which is what np.abs and np.angle return.

Intuition

Some image structure changes slowly, such as the shading across a wall. Some changes fast, such as hair or printed text. The Fourier transform separates the two by rewriting the image in a basis of sinusoids of every frequency. Fourier claimed in 1807 that any function can be written as a sum of sines and cosines. A square wave that alternates between and , for example, equals . The first term sets the coarse shape, and each added term sharpens the corners. Human vision also responds to frequency. Dalí’s painting Gala Contemplating the Mediterranean Sea, which at 30 meters becomes the portrait of Abraham Lincoln (1976) shows Gala up close and Lincoln’s face from far away. Hybrid images use the same effect.

The frequency view also simplifies filtering. A convolution mixes every pixel with its neighbors, which is hard to reason about pixel by pixel. In the frequency domain it multiplies each frequency by one number, the filter’s gain. A blur then becomes a choice of which frequencies to keep. In 2D, every frequency is a grating of stripes. The diagram shows the two spectrum patterns that appear most often, a sinusoidal grating and a single edge.

\usepackage{tikz}
\begin{document}
\definecolor{peak}{HTML}{D9483B}
\definecolor{mid}{HTML}{8A8A8A}
\begin{tikzpicture}[x=1cm,y=1cm,>=latex,font=\small]
% vertical stripes image
\foreach \k in {0,...,59} {
  \pgfmathsetmacro{\shade}{40+40*cos(24*(\k+0.5))}
  \fill[black!\shade] (\k*0.05,0) rectangle (\k*0.05+0.05,3);
}
\draw[mid] (0,0) rectangle (3,3);
\node[above] at (1.5,3.05) {sinusoidal stripes $f(x,y)$};
\draw[->,mid] (0,-0.25) -- (3,-0.25) node[right] {$x$};
% its spectrum
\begin{scope}[xshift=5cm]
\fill[black] (0,0) rectangle (3,3);
\draw[->,mid] (0,1.5) -- (3.25,1.5) node[right] {$u$};
\draw[->,mid] (1.5,0) -- (1.5,3.25) node[above] {$v$};
\fill[white] (1.5,1.5) circle (2.2pt);
\fill[peak] (2.3,1.5) circle (2.2pt) node[above] {$+u_0$};
\fill[peak] (0.7,1.5) circle (2.2pt) node[above] {$-u_0$};
\node[white,below right] at (1.5,1.5) {\tiny DC};
\node[above] at (1.5,3.4) {$\log|F(u,v)|$ after fftshift};
\end{scope}
% vertical edge image
\begin{scope}[yshift=-4.4cm]
\fill[black!75] (0,0) rectangle (1.5,3);
\draw[mid] (0,0) rectangle (3,3);
\node[above] at (1.5,3.05) {one vertical edge};
\draw[->,mid] (0,-0.25) -- (3,-0.25) node[right] {$x$};
\end{scope}
\begin{scope}[xshift=5cm,yshift=-4.4cm]
\fill[black] (0,0) rectangle (3,3);
\draw[->,mid] (1.5,0) -- (1.5,3.25) node[above] {$v$};
\foreach \u in {0.1,0.2,...,1.4} {
  \fill[peak,opacity={0.25+0.5*(1.5-\u)/1.5}] (1.5+\u,1.5) circle (1.6pt);
  \fill[peak,opacity={0.25+0.5*(1.5-\u)/1.5}] (1.5-\u,1.5) circle (1.6pt);
}
\fill[white] (1.5,1.5) circle (2.2pt);
\draw[->,mid] (3,1.5) -- (3.25,1.5) node[right] {$u$};
\node[peak,below] at (1.5,1.1) {line along the $u$ axis};
\end{scope}
\draw[->,thick,mid] (3.3,1.5) -- (4.7,1.5) node[midway,above] {FT};
\draw[->,thick,mid] (3.3,-2.9) -- (4.7,-2.9) node[midway,above] {FT};
\end{tikzpicture}
\end{document}

Properties

(Reading a Spectrum) The center is the mean brightness, the distance from the center is the frequency, and the angle gives the orientation of the stripes.

The DFT value at is the sum of all pixel values. Synthetic images often have spectral values of exactly , so add a small constant such as before taking the log. For a real image , so every bright spot has a twin mirrored through the center. Rotating the image rotates the spectrum by the same angle. The DFT treats the image as periodic, so the jump from the right border back to the left border acts like a vertical edge. That jump, and the one from bottom to top, draw a bright horizontal and vertical cross through the center of most photo spectra. The highest frequency along each axis is cycles across the image, or half a cycle per pixel. This is the sampling limit from sampling theory, and it matters for anti-aliasing.

(Edges Make Perpendicular Lines) A straight edge puts a line of energy through the center of the spectrum, perpendicular to the edge.

A vertical edge varies only along , so all its energy lies on the axis. A step contains every frequency, with amplitudes that fall off roughly as , so the energy spreads into a line of many dots. A slanted edge gives a line at the perpendicular slant. The lecture shows a photo of a row of arches. Its vertical pillars produce a bright horizontal line in the spectrum, and its long horizontal edges produce a vertical line. Periodic textures, such as a grid of windows or a honeycomb, give a lattice of bright dots. The lattice spacing is inversely proportional to the texture’s period.

(Phase Carries Structure) An image rebuilt from one photo’s amplitude spectrum and another photo’s phase spectrum looks like the second photo.

The lecture swaps the phase spectra of a building facade and a toy dinosaur, and each result looks like the image whose phase it kept. The amplitude spectra of most photos look alike, bright at the center and fading with frequency. They say how much of each frequency is present but not where it is. The phase says where each sinusoid’s peaks fall, and an edge appears where many sinusoids peak at the same place. A shift of the image by pixels changes only the phase, since . The amplitude stays the same, so the amplitude spectrum carries no information about where things are.

(Local Versus Global) A change confined to one region of one domain spreads over the whole of the other domain.

Zeroing a wedge of the spectrum changes every pixel of the image. Removing a wedge around the horizontal frequency axis, for example, blurs all near-vertical edges. Changing one pixel adds a faint grating at every frequency, because a single impulse has a flat spectrum. Widths trade off the same way. A narrow Gaussian kernel has a wide spectrum, and a wide kernel has a narrow one. A filter cannot be both short in space and sharp in frequency. A hard circular low-pass mask in the frequency domain therefore becomes a sinc-like kernel in space, and it produces ripples beside edges.

(Convolution Theorem in Practice) Multiplying spectra is fast for large kernels, but the DFT version computes a circular convolution.

Direct convolution of an image with an kernel costs about operations. The FFT route costs on the order of , independent of the kernel size (2.9d Fast Fourier Transform). The product of two DFTs equals the DFT of the circular convolution, which wraps the signal around its ends. To get the ordinary full convolution, zero-pad both arrays to samples per side before transforming. Cross-correlation corresponds to multiplying by the complex conjugate of the kernel’s spectrum, which is how large template matching searches are computed.

(Change of Basis) The DFT expresses a signal in an orthogonal basis of complex sinusoids, and its inverse uses the conjugate transpose.

Row of holds for . The rows are orthogonal, and , where is the conjugate transpose. Each DFT coefficient is the inner product of the signal with one sinusoid, so it measures how much of that frequency the signal contains. Energy is preserved up to the factor , as 2.9e Parseval’s Theorem states. The 2D DFT applies the 1D DFT to every row and then to every column.

Examples

(Easy) A Fundamental and Its Third Harmonic

The lecture’s 1D example is . Take samples covering exactly periods of the first term.

(Medium) Spectra of Stripes and of an Edge

Three test images are built with column index and row index . The first is vertical stripes . The second is horizontal stripes . The third is a vertical edge, for and for .

(Hard) A Box Filter in the Frequency Domain

A row of pixels is blurred with the 3-tap box centered on each pixel, with wrap-around at the ends.

See Also