2D DFT: Image example #3

Load gray-scale image

img = ReadImage["bakery256x256.ppm"] ; RowBox[{RowBox[{scale,  , =,  , 1.5}], ;}]

Display image

ShowImage[img, scale] ;

[Graphics:../HTMLFiles/index_121.gif]

DFT of image

fft = Fourier[img] ;

Magnitude DFT (frequencies radiate from the center from highest to lowest)

tmp = Log[1 + Abs[fft]] ; ShowImage[tmp, scale, Max[tmp]] ;

[Graphics:../HTMLFiles/index_124.gif]

Phase DFT

ShowImage[Arg[fft], scale, π, -π] ;

[Graphics:../HTMLFiles/index_126.gif]

DFT of image with frequencies rearranged

shift = Table[(-1)^(x + y), {x, 0, (Dimensions[img]//First) - 1}, {y, 0, (Dimensions[img]//Last) - 1 }] ; fft = Fourier[img * shift] ;

Magnitude DFT (frequencies radiate from the center from lowest to highest)

tmp = Log[1 + Abs[fft]] ; ShowImage[tmp, scale, Max[tmp]] ;

[Graphics:../HTMLFiles/index_129.gif]

Phase DFT

ShowImage[Arg[fft], scale, π, -π] ;

[Graphics:../HTMLFiles/index_131.gif]

How important is phase?

Take DFT (frequencies radiate from the center from lowest to highest)

fft = Fourier[img * shift] ;

Eliminate magnitude information

fftphase = Map[(#/Abs[#]) &, fft, {2}] ;

Magnitude DFT (constant)

ShowImage[Abs[fftphase], scale, 2] ;

[Graphics:../HTMLFiles/index_135.gif]

Take inverse DFT of constant-magnitude image DFT

ShowImage[Abs[InverseFourier[fftphase]]// Chop, scale, .01] ;

[Graphics:../HTMLFiles/index_137.gif]

Filtering in the frequency domain

Define 2D low-pass filter

LowPassFilter2D[dim_, cf_] := Module[{m, c, r}, m = ZeroMatrix[dim] ;  ... , y]] = If[(x - c)^2 + (y - c)^2 < r^2, 1, 0], {x, 1, dim}, {y, 1, dim}] ; m] ;

Take DFT (frequencies radiate from the center from lowest to highest)

fft = Fourier[img * shift] ;

Define low-pass filter

cf = .2 ; lpf = LowPassFilter2D[Dimensions[img]//First, cf] ;

Visualize low-pass filter

ShowImage[lpf, scale, 2] ;

[Graphics:../HTMLFiles/index_142.gif]

Apply filter and view resulting filtered image

tmp = Abs[InverseFourier[fft * lpf]] ; ShowImage[tmp, scale] ;

[Graphics:../HTMLFiles/index_144.gif]

Define high-pass filter

cf = .2 ; hpf = 1 - LowPassFilter2D[Dimensions[img]//First, 1 - cf] ;

Visualize high-pass filter

ShowImage[hpf, scale, 2] ;

[Graphics:../HTMLFiles/index_147.gif]

tmp = Abs[InverseFourier[fft * hpf]] ; ShowImage[tmp, scale, 30] ;

[Graphics:../HTMLFiles/index_149.gif]


Created by Mathematica  (February 5, 2004)