function [person,pic] = pca_faces(TestPer,TestPic,NumRecFaces)
%此函数用基于PCA的方法实现人脸检测,采用特征脸实现。
%输入:
% TestPer,TestPic:希望检测的第TestPer个人的第TestPic个脸,应该选择任意人后五幅脸中的一幅作为测试图像
% NumRecFaces:希望得到的最相近的人脸数,小于10,默认为4
%输出:
% Person,Pic:检测到的最相近的几个第Person个人的第Pic个脸
if nargin == 2
NumRecFaces = 4;
end;
load Imgs %load the database
%choose the first five faces for one person as the training faces
TrainImgs = Imgs(:,1:5);
for i=2:40
TrainImgs = [TrainImgs Imgs(:,1+10*(i-1):5+10*(i-1))];
end
%run PCA to train
[Vectors,Values,Psi] = pc_evectors(TrainImgs,30); % Get top 30 PC evectors of Imgs
%show the eigenfaces
figure;
subplot(251)
pc_ev = reshape(Vectors(:,1),w,h)'; % Get PC eigenvector 1
imagesc(pc_ev); colormap(gray(256)); axis off ;axis image
subplot(252)
pc_ev = reshape(Vectors(:,2),w,h)'; % Get PC eigenvector 2
imagesc(pc_ev); colormap(gray(256)); axis off; axis image
subplot(253)
pc_ev = reshape(Vectors(:,3),w,h)'; % Get PC eigenvector 3
imagesc(pc_ev); colormap(gray(256)); axis off; axis image
subplot(254)
pc_ev = reshape(Vectors(:,4),w,h)'; % Get PC eigenvector 4
imagesc(pc_ev); colormap(gray(256)); axis off; axis image
subplot(255)
pc_ev = reshape(Vectors(:,5),w,h)'; % Get PC eigenvector 5
imagesc(pc_ev); colormap(gray(256)); axis off; axis image
subplot(256)
pc_ev = reshape(Vectors(:,6),w,h)'; % Get PC eigenvector 6
imagesc(pc_ev); colormap(gray(256)); axis off; axis image
subplot(257)
pc_ev = reshape(Vectors(:,7),w,h)'; % Get PC eigenvector 7
imagesc(pc_ev); colormap(gray(256)); axis off; axis image
subplot(258)
pc_ev = reshape(Vectors(:,8),w,h)'; % Get PC eigenvector 8
imagesc(pc_ev); colormap(gray(256)); axis off; axis image
subplot(259)
pc_ev = reshape(Vectors(:,9),w,h)'; % Get PC eigenvector 9
imagesc(pc_ev); colormap(gray(256)); axis off; axis image
subplot(2,5,10)
pc_ev = reshape(Vectors(:,10),w,h)'; % Get PC eigenvector 10
imagesc(pc_ev); colormap(gray(256)); axis off ;axis image
%input a new image to recongnize
%show the test face
TestFace = Imgs(:,10*(TestPer-1)+TestPic);
TFace = reshape(TestFace,w,h)';
figure;
imagesc(TFace);
colormap(gray(256));
title('Test Face');axis off
%select the first 10 eigenvectors to reconstruct
LowDim_TestF = Vectors(:,1:10)'*(TestFace - Psi); % Project onto eigenvectors
%construct a matrix to save the closest recognized faces
RecFaces = zeros(2,NumRecFaces);%first row to record the index, the second to the distinction
%find the nearest faces in the database(400 face in all)
for i = 1:NumRecFaces
TemptFace = Imgs(:,i);
LowDim_TemptF = Vectors(:,1:10)'*(TemptFace - Psi);
Dis = sum(abs(LowDim_TestF-LowDim_TemptF));
RecFaces(2,i) = Dis;%distinction
end
[RecFaces(2,:),RecFaces(1,:)] = sort(RecFaces(2,:));
for i = NumRecFaces+1:400
TemptFace = Imgs(:,i);
LowDim_TemptF = Vectors(:,1:10)'*(TemptFace - Psi);
Dis = sum(abs(LowDim_TestF-LowDim_TemptF));
for j = 1:NumRecFaces-1
if (Dis < RecFaces(2,j))
RecFaces(:,j+1:NumRecFaces) = RecFaces(:,j:NumRecFaces-1);
RecFaces(1,j) = i;
RecFaces(2,j) = Dis;
break
end
end
end
%recognization is accomplished
%return to the jth face of the ith person
pic = mod(RecFaces(1,:),10);
index = find(pic == 0);
pic(index) = 10;
person = (RecFaces(1,:)-pic)/10+1;
%show the closest faces
for i = 1:NumRecFaces
figure;
RFace = reshape(Imgs(:,RecFaces(1,i)),w,h)';
imagesc(RFace);
colormap(gray(256));
%每个图的标题显示第i个人的第j个脸,用以判断是否正确
title(['The ',num2str(pic(i)),'th face of the ',num2str(person(i)),'th person']);
end
- 1
- 2
- 3
- 4
前往页