When using eigen faces to reconstruct images final image comes out fuzzy even when using all eigen faces

I am trying to reconstruct these images using eigen faces but keep having the same issue.
I attatched all of the images along with the average face that is created. Unfortunatly, I am unable to attach allFaces.mat as it is too large. However it is just the data from the Yale faces database. I have linked a copy of it thought I dont believe this is the same one I am using as the one I am using has 38 participants. If you have any suggestions on how to deal with this please let me know.
I have seen code very similar to this work but everytime I try to replicate it I run into this same issue.
clear all, close all, clc
r1 = im2double(im2gray(imread('r1-168x192.png')),'indexed');
r2 = im2double(im2gray(imread('r2-168x192.jpg')),'indexed');
r3 = im2double(im2gray(imread('r3-168x192.jpg')),'indexed');
r4 = im2double(im2gray(imread('r4-168x192.jpg')),'indexed');
r5 = im2double(im2gray(imread('r5-168x192.jpg')),'indexed');
r6 = im2double(im2gray(imread('r6-168x192.jpg')),'indexed');
r7 = im2double(im2gray(imread('r7-168x192.jpg')),'indexed');
r8 = im2double(im2gray(imread('r8-168x192.jpg')),'indexed');
r9 = im2double(im2gray(imread('r9-168x192.jpg')),'indexed');
r10 = im2double(im2gray(imread('r10-168x192.jpg')),'indexed');
m1 = im2double(rgb2gray(imread('m1-168x192.jpg')),'indexed');
m2 = im2double(im2gray(imread('m2-168x192.jpg')),'indexed');
m3 = im2double(im2gray(imread('m3-168x192.jpg')),'indexed');
m4 = im2double(im2gray(imread('m4-168x192.jpg')),'indexed');
m5 = im2double(im2gray(imread('m5-168x192.jpg')),'indexed');
m6 = im2double(im2gray(imread('m6-168x192.jpg')),'indexed');
m7 = im2double(im2gray(imread('m7-168x192.jpg')),'indexed');
m8 = im2double(im2gray(imread('m8-168x192.jpg')),'indexed');
m9 = im2double(im2gray(imread('m9-168x192.jpg')),'indexed');
m10 = im2double(im2gray(imread('m10-168x192.jpg')),'indexed');
load allFaces.mat % Load data file of faces
r1 = reshape(r1,32256,1);
r2 = reshape(r2,32256,1);
r3 = reshape(r3,32256,1);
r4 = reshape(r4,32256,1);
r5 = reshape(r5,32256,1);
r6 = reshape(r6,32256,1);
r7 = reshape(r7,32256,1);
r8 = reshape(r8,32256,1);
r9 = reshape(r9,32256,1);
r10 = reshape(r10,32256,1);
m1 = reshape(m1,32256,1);
m2 = reshape(m2,32256,1);
m3 = reshape(m3,32256,1);
m4 = reshape(m4,32256,1);
m5 = reshape(m5,32256,1);
m6 = reshape(m6,32256,1);
m7 = reshape(m7,32256,1);
m8 = reshape(m8,32256,1);
m9 = reshape(m9,32256,1);
m10 = reshape(m10,32256,1);
Mfaces = [r1 r2 r3 r4 r5 r6 r7 r8 r9 r10 m1 m2 m3 m4 m5 m6 m7 m8 m9 m10];
%% Use the first 36 people for training data
trainingFaces = faces(:,1:sum(nfaces(1:38)));
avgFace = mean(trainingFaces,2); % size n*m by 1;
% compute eigenfaces on mean-subtracted training data
X = trainingFaces-avgFace*ones(1,size(trainingFaces,2));
[U,S,V] = svd(X,'econ');
figure(3) % Open new figure to plot average face
axes('position',[0 0 1 1]), axis off
imagesc(reshape(avgFace,n,m)), colormap gray
im = m6;
figure(6) % Open new figure to plot reconstructed image
subplot(2,4,1), imagesc(reshape(im,n,m)), colormap gray
title('Person #37 Original')
%% Plot first 64 eigenfaces
count = 1;
testFaceMS = im - avgFace ;
for r=[400 800 2282] % Error in original code; 50 not 0 between 25 and 100
count=count+1;
subplot(2,4,count)
reconFace = avgFace + (U(:,1:r)*(U(:,1:r)'*testFaceMS));
imagesc(reshape(reconFace,n,m)), colormap gray % Plot reconstructed face
title(['r=',num2str(r,'%d')]);
pause(0.1)
end

3 Comments

To test we will need the rest of the images as well, and also allfaces.mat
I can upload the other jpgs but I was unable to upload the allfaces.mat as the file size was too large even when compressed. however the allfaces.mat is just the yale faces database.
Everything should be uploaded as much as possible now.

Sign in to comment.

Answers (1)

Is the face you are working with (m6) one of the columns in the trainingFaces matrix from which the SVD is constructed?
I would expect an exact reconstruction in that case, since norm(U*(U'*X)) should be small and that should mean every column of X is reconstructed to high precision.

Asked:

te
on 10 May 2026 at 18:26

Answered:

on 11 May 2026 at 11:36

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!