Skip to content

Instantly share code, notes, and snippets.

@EyalAr
Last active August 17, 2017 21:33
Show Gist options
  • Save EyalAr/5196200 to your computer and use it in GitHub Desktop.
Save EyalAr/5196200 to your computer and use it in GitHub Desktop.
Matlab script which extracts to files the eigenfaces of a set of face images.
% Extract the eigen-faces of a set of face images.
start_index = 1;
end_index = 10;
base_name = '';
extension = '.jpg';
in_dir = 'in';
out_dir = 'ef';
images = [];
%read each image, store it as a row vector, and store all those
%vectors as rows of the images matrix:
for i=start_index:end_index
img = imread(strcat(in_dir, '/', base_name, num2str(i), extension));
img = double(rgb2gray(img));
[rows cols] = size(img);
img = img(:)';
images = vertcat(images, img);
end
images_mean = mean(images);
images = images - ones(end_index - start_index + 1, 1) * images_mean;
%perform PCA on 'images':
[C, S, L] = princomp(images, 'econ');
%save the eigenvectors as images:
for i=1:size(C,2)
img = C(:,i);
img = reshape(img,rows,cols);
img = ( img - min(img(:)) ) ./ ( max(img(:)) - min(img(:)) );
imwrite(img, strcat(out_dir, '/', num2str(i), extension));
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment