2D DFT: Image example #2

Load gray-scale image

img = ColorToGrayScale[ReadImage["part128x128.ppm"]] ; scale = 3 ;

Display image

ShowImage[img, scale] ;

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

DFT of image

DFT

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_93.gif]

Phase DFT

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

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

DFT of image with frequencies rearranged

DFT

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_98.gif]

Phase DFT

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

[Graphics:../HTMLFiles/index_100.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_104.gif]

Take inverse DFT of constant-magnitude image DFT

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

[Graphics:../HTMLFiles/index_106.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_111.gif]

Apply filter and view resulting filtered image

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

[Graphics:../HTMLFiles/index_113.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_116.gif]

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

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


Created by Mathematica  (February 5, 2004)