3.12 Display spectrum of 2D image

3.12.1 Mathematica
3.12.2 Matlab

3.12.1 Mathematica

img=Import["ExampleData/lena.tif"]; 
Image[img,ImageSize->300]
 

pict

ImageDimensions[img]
 
{150,116}
 
(*see how many channels*) 
ImageChannels[img]
 
3
 
data=ImageData[img];  (*get data*) 
{nRow,nCol,nChannel}=Dimensions[data]
 
{116,150,3}
 
(*look at each channel*) 
Map[Image[data[[All,All,#]],ImageSize->100]&, 
         Range[1,nChannel]]
 

pict

(*get channel 2 to FFT but center it first*) 
d = data[[All,All,2]]; 
d = d*(-1)^Table[i+j,{i,nRow},{j,nCol}]; 
 
(*make FFT,center, view spectrum and phase*) 
fw = Fourier[d,FourierParameters->{1,1}]; 
(*adjust for better viewing as needed*) 
fudgeFactor = 100; 
abs = fudgeFactor * Log[1+Abs@fw]; 
Labeled[Image[abs/Max[abs], 
       ImageSize->300], 
       Style["Magnitude spectrum", 18]]
 

pict

arg = Arg@fw; 
Labeled[Image[arg/Max[arg], 
        ImageSize->300], 
        Style["Phase spectrum", 18]]
                                                                                    
                                                                                    
 

pict

3.12.2 Matlab

close all; clear all; 
%image in same folder. 
img = imread('lena.tiff','tif'); 
imagesc(img)
 

pict

img = fftshift(img(:,:,2)); 
F = fft2(img); 
figure; 
imagesc(100*log(1+abs(fftshift(F)))); 
colormap(gray); 
title('magnitude spectrum');
 

pict

figure; 
imagesc(angle(F));  colormap(gray); 
title('phase spectrum');
 

pict