Eigenfaces display code issues

1 view (last 30 days)
Subhransu Sekhar Bhattacharjee
I am not being able to display the eigenfaces, in the visualisable image format of the top 10 eigenfaces.
here is my code up to this point. Please can someone help.
function [recognized_img_1,recognized_img_2,recognized_img_3,eigen_face,mean_img]=facerecog(datapath,testimg)
D = dir(datapath); % D is a Lx1 structure with 4 fields as: name,date,byte,
% isdir of all L files present in the directory 'datapath'
imgcount = 0;
for i=1 : size(D,1)
if not(strcmp(D(i).name,'.')|strcmp(D(i).name,'..')|strcmp(D(i).name,'Thumbs.db'))
imgcount = imgcount + 1; % Number of all images in the training database
end
end
X = [];
m = zeros();
for i = 1 : imgcount
str = strcat(datapath,'\',int2str(i),'.png');
img = imread(str);
I = im2single(img);
[r, c] = size(img);
temp = reshape(img',r*c,1);
m = m + (1/imgcount)*I;
X = [X temp];
end
mean_img = reshape(m,r,c);
imgcount = size(X,2);
M = reshape(m,r*c,1);
A = [];
for i=1 : imgcount
temp = double(X(:,i)) - M;
A = [A temp];
end
L= A' * A;
[V,D]=eig(L);
% % V : eigenvector matrix D : eigenvalue matrix
L_eig_vec = [];
for i = 1 : size(V,2)
if( D(i,i) > 1 )
L_eig_vec = [L_eig_vec V(:,i)];
end
end
%% eigenfaces
eigenfaces = A * L_eig_vec;
[U,~,~] = svd(L_eig_vec,0);
pca = [];
for k=1:size(eigenfaces,2)
pca{k} = eigenfaces(:,k);
end
[~,xci]=sort(diag(D),'descend');% largest eigenval
eigen_face = [ pca{xci(1)} pca{xci(2)} pca{xci(3)} pca{xci(4)} pca{xci(5)};pca{xci(6)} pca{xci(7)} pca{xci(8)} pca{xci(9)} pca{xci(10)}];

Answers (0)

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!