%
% Simple image compressing demo using the singular value decomposition.
% The grayscale bitmap image is interpreted as a matrix. One can store
% compressed image by storing only singular values and vectors
% s(1:nterms), U(:,1:nterms), V(:,1:nterms) where
% nterms is much smaller than the size of the matrix A. This demo shows
% the quality of some compressed images.
%
function svd_image()
kuva=imread('lena512.bmp');   % standard test image
A=double(kuva);
[U,S,V]=svd(A);
fprintf(1,'Compressing %d x %d bitmap image\n',size(A,1),size(A,2));
colormap(gray);
npic=4;
AA=0.0;
for k=1:npic
    subplot(1,npic,k);
    nterms = k*10;
    AA=0.0;
    for i=1:nterms
        AA = AA + S(i,i)*U(:,i)*V(:,i)' ;
    end
    %
    % Show compressed image:
    %
    imagesc(AA); axis equal; axis off; axis image;
    s = sprintf('# singular values = %d',nterms);
    title(s)
end


