首页 > 代码库 > PCA
PCA
人脸识别(mat见文件)
clc;close all;clear all
load(‘facedata.mat‘)
person1=zeros(2576,10);
for i=1:10
a=double(facedata{9,i});
person1(:,i)=reshape(a,2576,1);
end
person111=person1(:,1);
xmean=mean(person1,2);
for i=1:10
person1(:,i)=person1(:,i)-xmean;
end
sigma=person1‘*person1;
[eigvectors eigvalues]=eig(sigma);
eigvectors=person1*eigvectors;
for i=1:10
eigvectors(:,i)= eigvectors(:,i) ./ sqrt(sum(eigvectors(:,i).*eigvectors(:,i)));
end
invalid=eigvectors(:,2:10);
eigfaces=zeros(2576,1);
for i=1:9
eigfaces=invalid(:,i);
subplot(3,3,i);
imagesc(reshape(eigfaces,56,46));
end
person11=person1(:,1);
eg=[];
mse=zeros(9,1);
figure;
for i=9:-1:1
eg=invalid(:,9:-1:i);
p=eg‘*person11;
xnew=eg*p;
xnew=xnew+xmean;
subplot(3,3,10-i);
imagesc(reshape(xnew,56,46))
mse(i)=sum((xnew-person111).^2);
mse(i)=mse(i)/2576;
title([num2str(mse(i))]);
end
PCA