% Work through an example of principal component analysis (PCA)
% using singular value decomposition (SVD). Here we find "average faces"
% from a collection of portraits.
%
% Samuli Siltanen November 2012

% Plot parameters
msize  = 15;
lwidth = 1.5;
fsize  = 16;

% Number of pictures
Npics = 32;

% Initialize the data matrix
XT = zeros(Npics,80*128);

% Construct the data matrix where the data vectors appear as rows. There
% are as many rows as there are images.
for iii = 1:Npics
    fname     = ['portraits/im', num2str(iii), '.tif'];
    im        = imread(fname,'tif');
    im        = double(im);
    XT(iii,:) = im(:).';
end

% Subtract mean
%m  = mean(XT);
%XT = XT-repmat(m,size(XT,1),1);

% Record image size for later use
[row,col] = size(im);

% Calculate SVD
X       = XT.';   % Taking the transpose
[W,S,V] = svd(X); % This results in X=W*S*V.'

% % Transform the data using the formula YT = XT*W = V*ST
% YT = V*(S.');
% Y  = YT.';
%
% % Dimension reduction
% SS      = S;
% SS(2,2) = 0;
% Y2T     = V*(SS.');
% Y2      = Y2T.';



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Show the principal components
for iii = 1:Npics
    figure(1)
    clf
    pcomp = W(:,iii);
    pcomp = reshape(pcomp,row,col);
    
    imagesc(pcomp)
    colormap gray
    axis equal
    axis off
    
    drawnow
    pause
end


