% this script reads out all files in a directory into images that are
% stored lexicographically (one large row, row by row.
cd yalefaces
% get a list of all the files in the current directory
[stat, flist] = fileattrib('*');

% Read in jpg and pgm files only; Assume that all files are the same size.
% for the yale face repository all files are gifs
% find number of files
nfiles = max(size(flist));
% Initialize structure

%imdata = zeros(nfiles,121*160);
cnt = 0;
data = zeros(nfiles,243*320);
facemean = zeros(243,320);
cnt = 0;
for imloop = 1:nfiles
    fn = flist(imloop).Name;
    x = double(imread(fn,'gif'));
    facemean = facemean + x;cnt=cnt+1;
    %figure,imagesc(x);
    data(imloop,:) = reshape(x,1,size(x,1)*size(x,2));
end;
cd ..  % get out of images data directory
% display four images
imview = [reshape(data(2,:),243,320),reshape(data(6,:),243,320);reshape(data(8,:),243,320),reshape(data(10,:),243,320)];
figure;imagesc(imview);colormap(gray);title('Different expressions');
imview = [reshape(data(2,:),243,320),reshape(data(13,:),243,320);reshape(data(24,:),243,320),reshape(data(35,:),243,320)];
figure;imagesc(imview);colormap(gray);title('Different Faces');

save imagesall data;
facemean = facemean/cnt; %Find image mean
figure;imagesc(facemean);colormap(gray);title('Image Mean');
% subtract mean from all images
data_mean = data - ones(cnt,1)*reshape(facemean,1,243*320);
% do PCA 
[v,mu] = eig(data_mean*data_mean');
mu=diag(mu);
percent_explained = 100*mu/sum(mu);
figure;pareto(percent_explained)
xlabel('Principal Component')
ylabel('Variance Explained (%)')

% transform data
ef=(data'*v)';
imview = [reshape(ef(1,:),243,320),reshape(ef(2,:),243,320);reshape(ef(3,:),243,320),reshape(ef(4,:),243,320)];
figure;imagesc(imview);colormap(gray);title('First 4 eigenfaces');

save eig ef facemean