%
% EECS 556 - Winter 2003
%
% 2nd Homework to Test 2D DCT
%
%
% Note - Use the axis image command to make all images
% have the correct aspect ratio
%
%
% Load image File
%
load image_data
[n1 n2]=size(image_data);
%
% Display Original Image
%
figure(1);
imagesc(image_data);
colormap(gray)
axis image
%
% Apply 2-D DFT Algorithm and Display
% over 60 dB display dynamic range
%
spectrum_dft=fft2(image_data);
spec_mag=fftshift(abs(spectrum_dft));
test_max=max(max(spec_mag));
spec_mag=20.0*log10(spec_mag/test_max);
figure(2)
imagesc(spec_mag,[-60.0 0.0]);
colormap(gray)
axis image
%
% Apply 2-D DCT Algorithm and Display
% over 60 dB display dynamic range
%
spectrum_dct=dct_2(image_data);
spec_mag=abs(spectrum_dct);
test_max=max(max(spec_mag));
spec_mag=20.0*log10(spec_mag/test_max);
figure(3)
imagesc(spec_mag,[-60.0 0.0]);
colormap(gray)
axis image
%
% Test Algorithm by Taking Inverse 2-D FFT
% And display original image
%
test_dct=idct_2(spectrum_dct);
figure(4);
imagesc(test_dct);
colormap(gray)
axis image
%
% Look At DCT and DFT in 128 x 128 Blocks
%

block1=image_data(??);
block2=image_data(??);
block3=image_data(??);
block4=image_data(??);
block5=image_data(??);
block6=image_data(??);

%
% Compute 2-D DFT of Each Block
%
block1_dft=dft_2(block1);
block2_dft=dft_2(block2);
block3_dft=dft_2(block3);
block4_dft=dft_2(block4);
block5_dft=dft_2(block5);
block6_dft=dft_2(block6);
%
% Compute 2-D DCT of Each Block
%
block1_dct=dct_2(block1);
block2_dct=dct_2(block2);
block3_dct=dct_2(block3);
block4_dct=dct_2(block4);
block5_dct=dct_2(block5);
block6_dct=dct_2(block6);
%
% Truncate 2-D DCT of Each Block
%
mask_dct=zeros(128,128);
n_cutoff=34;

mask_dct(??)=1.0;

block1_dct=block1_dct.*mask_dct;
block2_dct=block2_dct.*mask_dct;
block3_dct=block3_dct.*mask_dct;
block4_dct=block4_dct.*mask_dct;
block5_dct=block5_dct.*mask_dct;
block6_dct=block6_dct.*mask_dct;
%
% Inverse 2-D DCT of Each Block
%
block1_idct=idct_2(block1_dct);
block2_idct=idct_2(block2_dct);
block3_idct=idct_2(block3_dct);
block4_idct=idct_2(block4_dct);
block5_idct=idct_2(block5_dct);
block6_idct=idct_2(block6_dct);
%
% Put Original Image Back Together
%
image_dct_recon=zeros(n1,n2);

image_dct_recon(??)=block1_idct;
image_dct_recon(??)=block2_idct;
image_dct_recon(??)=block3_idct;
image_dct_recon(??)=block4_idct;
image_dct_recon(??)=block5_idct;
image_dct_recon(??)=block6_idct;

%
% Display Reconstructed Image
%
figure(5);
imagesc(image_dct_recon);
colormap(gray)
axis image
%
% Truncate 2-D DFT of Each Block
%
mask_dft=zeros(128,128);

n_cutoff=??;
mask_dft(??)=1.0;

block1_dft=block1_dft.*mask_dft;
block2_dft=block2_dft.*mask_dft;
block3_dft=block3_dft.*mask_dft;
block4_dft=block4_dft.*mask_dft;
block5_dft=block5_dft.*mask_dft;
block6_dft=block6_dft.*mask_dft;
%
% Inverse 2-D DFT of Each Block
%
block1_idft=real(idft_2(block1_dft));
block2_idft=real(idft_2(block2_dft));
block3_idft=real(idft_2(block3_dft));
block4_idft=real(idft_2(block4_dft));
block5_idft=real(idft_2(block5_dft));
block6_idft=real(idft_2(block6_dft));
%
% Put Original Image Back Together
%
image_dft_recon=zeros(n1,n2);

image_dft_recon(??)=block1_idft;
image_dft_recon(??)=block2_idft;
image_dft_recon(??)=block3_idft;
image_dft_recon(??)=block4_idft;
image_dft_recon(??)=block5_idft;
image_dft_recon(??)=block6_idft;

%
% Display Reconstructed Image
%

figure(6);
imagesc(image_dft_recon);
colormap(gray)
axis image


