% m-file to look at SVD in image processing

%

% either type in the commands below (or use cut and paste) 

% (do not need to type comment line- lines starting with a '%')

%  or 

% save m-file to a directory on the path (see menu item File|Set Path)

% the type filename at matlab prompt

%

% note:  figures 1 and 2 may overlap- move them apart





% close all current figures

close all



% load 

load('clown.mat')

colormap('gray')

image(X)



size(X)



% use semicolon to suppress output

[U S V]= svd(X);



% create a new figure window (blank for now)

figure(2)

spy(S)

disp('sizes of U and S')

size(U)

size(S)





% checking unitary matrix

% eye(200) is the identity matrix

% U' is the transpose

disp('norm of differences of U* U transpose -I')

norm( U*U'-eye(200))

norm( U'*U- eye(200))



% create a new figure window (blank for now)

figure(3)

% revert back to black and white

colormap('gray')



% extracting matrix

k= 20

Uk= U(:,1:k);

Vk= V(:,1:k);

Sk= S(1:k,1:k);

disp('size of Uk')

size(Uk)



% reconstructing image

% use semicolon to suppress output

Ak= Uk*Sk*Vk';

image(Ak)



% move windows around to compare figures



