% svd for image compression
clc;
k = 1;
choice = input('Choose image: 1 = Clown (200x320), 2 = Duerer (648x509) ');
if choice == 1
    load clown.mat;
else
    load durer.mat;
end
figure
colormap('gray');
image(X)
title('Original Image')
[m n] = size(X);
[l u] = lu(X);
kmax = floor(min([m n])*100*(m+n)/(m*n));
fprintf('\n\n\n\n\n\n\n\n\n\nThe image has %d singular values\n', min([m n]))
compression_ratio = input(sprintf('Input desired compression ratio (0=stop, %3d=max) ', kmax));
k = ceil(compression_ratio/100*m*n/(m+n));
while k ~= 0
    figure
    colormap('gray');
    image(l(:,1:k)*u(1:k,:));
    title(sprintf('LU Compression ratio %5.4f, %d columns used', ...
        compression_ratio/100, k));
    compression_ratio = input(sprintf('Input desired compression ratio (0=stop, %3d=max) ', kmax));
    k = ceil(compression_ratio/100*m*n/(m+n));
end