%% Exercise 1
% Create a script to your function that:
% ? reads in an image,
% ? converts it to grayscale,
% ? crops (imcrop) it or resizes (imresize) it to a reasonable size (lower than
% 100x100, eg.: given the example image LucienHerve_ParisSansQuitterMaFenetre.jpg, crop a 100x100
% sized part of it with imcrop from pixels starting [430, 640]),
% ? calls your DFT function on the image,
% ? calls MATLAB’s built-in inverse Fourier
% Transformation on the output image of
% your function (see ifft2()),
% ? displays the original input image and
% the output image of ifft2()

im = imread('LucienHerve_ParisSansQuitterMaFenetre.jpg');
im = rgb2gray(im);
im = imcrop(im, [430, 640, 50, 50]);
im_dft = DFT(im);

figure(1)
subplot(2,2,1)
imshow(im)
title('Original image')

subplot(2,2,2)
imshow(uint8(ifft2(im_dft)))
title('Image after DFT+iFFT')

%% Exercise 2
% Extend the above script with
% displaying and saving the phase and magnitude images of the DFT-image
%     ? use fftshift() to center the output of your DFT-function,
%     ? use abs() and log() functions to better visualize the magnitude-part,
%     ? use angle() function to retrieve the phase angles;
% ? note: try to avoid log(0); scale the images before imshow (either if you give a
% parameter with type uint8, the value-range of it should be between [0, 255]; or if you give a parameter
% with type double, the value-range of it should be between [0, 1]). 

im_dft_magnitude = uint8( fftshift( mat2gray( log( abs( im_dft ) + 1 ) ) * 255 ) );
im_dft_phase = uint8( fftshift( mat2gray( angle( im_dft ) ) * 255 ) );

subplot(2,2,3)
imshow(im_dft_magnitude)
title('Magnitude')

subplot(2,2,4)
imshow(im_dft_phase)
title('Phase')
