% Program to demonstrate importance of phase in images

% cd c:\jl\4F8_imageprocessing\imageprocessing3\imageProc3mat\imageproc3demo

% imagephasedemo2.m

clear,clc,close all,home

load lenna256.mat;
[n,m]=size(x)
grayimage(x);
title('Original image');
pause

% take the FT of x
X=fft2((x));
% take the inverse FT of the magnitude of X -- thereby eliminating phase
xamp=ifft2(abs(X));

% xamp will be in complex form (even if complex part is zero) so
% make life easier by taking the real part

xampr = real(xamp);

% need to check if any of these values go over 255 -- if they 
% do, set them to 255
for i=1:n
  for j=1:m
    if xampr(i,j)>255
      xampr(i,j)=255;
    end
  end
end

figure(2)
grayimage(xampr);
title('Image reconstructed from amplitude of spectrum');
pause


% fftshift so that dc level is at centre of image: this is the one
% printed in the lecture notes

figure(3)
grayimage(fftshift(xampr));
title('Image reconstructed from amplitude of spectrum: fftshifted');
pause


% now look at the phase-only reconstruction

% divide by the amplitude of the complex number in the fft to give an 
% array of complex numbers with unit amplitude for each element

Xamp = abs(X);
Xang = X./Xamp;


xang=ifft2((Xang));
% note -- take the real value: the phases are all approx zero
% but taking absolute values would result in loss of signs
xang=(real(xang));

figure(3)
grayimage(acos(xang));
title('Image reconstructed from phase of spectrum');
pause


