Eigenfaces display code issues

3 次查看(过去 30 天)
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)}];

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Dimensionality Reduction and Feature Extraction 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by