function pca_image(IM)
%function pca_image(IM)
%
% chiamare la funzione, per es., con
%   pca_image('miafigura.jpg')
%

A=imread(IM);
figure(1)
imshow(A)
Adouble=double(A);
[n1,n2,n3]=size(Adouble);
fprintf('immagine esatta, size %d x %d, memory allocation %d \n',n1,n2, n1*n2)
for j=1:3,
 [U(:,:,j),S(:,:,j),V(:,:,j)]=svd(Adouble(:,:,j),0);
 figure(100)
 semilogy(diag(S(:,:,j))/S(1,1,j),'*')
 hold on
end
hold off
title('immagine esatta')

i=1;
while 1==1
   i=i+1;
   k=input('Number of Princ.Components (value=0 to finish) : ');
   if k==0 break,end
   figure(i);

   for j=1:3,
     B(:,:,j)=U(:,1:k,j)*S(1:k,1:k,j)*V(:,1:k,j)';
   end
   imshow(uint8(B))
   t=( n1*k+k*n2)/(n1*n2)*100;
   title(['num. princ.comp.=', int2str(k)])
   fprintf('Truncated Image memory allocation : %d, reduced size : %4.2f \n', n1*k+k*n2, t)

end

