Last active
August 17, 2017 21:33
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% 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