% Script: ShowSVDImage
% Looks at SVD-based low-rank approximations of an image.
close all
% Read in an image and convert to black and white...
A = imread('GoldenGate.jpg');
A = double(rgb2gray(A));
[m,n] = size(A);
[U,S,V] = svd(A);
% The vector of singular values...
sigma = diag(S);
% Regard a singular value sigma(k) as "significant" if
% sigma(k)>=sigma(1)/200.
% The number of significant singular values...
N = sum(sigma>=sigma(1)/200);
Ar = zeros(m,n);
for r=1:N
Ar = Ar + sigma(r)*U(:,r)*V(:,r)';
% Display a rank-r approximation of the original image...
imshow(uint8(Ar))
title(sprintf('The rank of the approximation is %1d',r),'fontsize',16)
shg
pause(.4)
end