% part a
% Load flower matrix from image demos library
load imdemos flower; 
% Convert the matrix to double precision
flower = double(flower); 
% Create a grayscale for any pictures you want to display
colormap('gray'); 
% Display flower in the window
imshow(flower,[0 255]) %

% part b
b = blkproc(flower,[8 8],'dct2');
figure, imshow(b, [0 255]) %

% part c
b(abs(b)<10) = 0;

% part d
d = round(blkproc(b,[8 8],'idct2'));
figure, imshow(d, [0 255]) %

% part e
e = mean2((flower - d).^2) %

% Graph
dct = blkproc(flower,[8 8],'dct2');

% number of non-zero values of the matrix after cutoff procedure
x = [];
% mean square error
y = [];

for cutoff = 0:4:100,

dct(abs(dct)<cutoff) = 0;
idct = round(blkproc(dct,[8 8],'idct2'));

x = [x,nnz(dct)];
y = [y,mean2((flower - idct).^2)];

% Over x.dialup.mit.edu, this is overkill.
% figure, imshow(idct, [0 255])
end

figure, plot(x,y,'d-') %
