写这个程序是老师布置的作业。一个莫名其妙的机会选了一个莫名其妙的课,于是写了与自己关系不大的人工智能的人脸识别的程序。这里给自己记录一下,估计这个学习都要和这个方面的打交道了。
Part 1:程序流程简介
这个程序是典型的。在已有资源中使用一部分做训练集,找到一个合适的模型或者结论,然后用剩下的部分来测试自己的结论的正确度,进而一步步提高自己的算法效率或者正确性等。
所以,在这次的程序中,前半部分是训练部分,中间有几段是画图部分,后面部分是测试部分。
Part 2:数据库和PCA算法简介
数据库使用的是Yale的人脸数据库。一共15组图片,每组图片里面有11张图片。在我的程序里面,我使用了每组里面的8张照片为训练集,剩下的3张为测试集。所以,一共是120张训练照片,45张测试照片。
PCA算法步骤:
1.Matrix X (input data) N dimensional input space
即原始矩阵
2.Matrix QX (Covariance of X) QX = cov(X) = E[(x-m)(x-m)T]
求出X的协方差矩阵QX
3.Valuable λk(eigenvalue of QX) λ1≥λ2≥λ3。。。
求出特征值,降序排列
4.Vector Vk
求出对应的特征向量
5.Projection: V*X
最后一步,在求出的向量基上面投影(降维)
Part 3:具体代码
%**********************************************
% Aim : The first AI program homework
% Title : PCA for face recognition
% Author : GongWanlu & Hufei
% Version : 1.0 final
% Submit Time : 2011-04-07
%**********************************************
% %%%%%%%%%%%%%%%%%%%%%%INITIAL
clear all
clc
close all
% %%%%%%%%%%%%%%%%%%%%%%Some variables according to the Yale Face DB
Num_subject = 15;
Num_image = 11;
Train_num_image = 8; %for every subject we choose 8 to train
Test_num_image = 9; %choose the left 3 images to test
% %%%%%%%%%%%%%%%%%%%%%%Load Data
Data = [];
for i=1:Num_subject
for j=1:Train_num_image
path = sprintf('FaceDB_yaleA/%03d/%02d.jpg',i,j);
pic = imread (path); %read one picture
%Make Data ,Add pic into Data
pic_line = pic(1:147*137); %The pic size is 147*137
%pic_line is 1*N ,N=147*137. from up to
%down,left to right.
%Reshape 2D image to 1D image
%vectors
Data = [Data;double(pic_line)]; %add pic_line into Data
end
end
% End of Load Data
%%%%%%%%%%%%%%%%%%%%%%%Substract mean from Data and make covariance from centering Data
samplemean = mean(Data); %mean pic 1*N
for k = 1:(Num_subject * Train_num_image)
xmean(k,:)=Data(k,:)-samplemean; %Normalize
end %xmean is M*N ,each line is one pic
%data(mean data) be normalized
sigma = xmean *xmean'; %M*M , here is 120*120
[V D]=eig(sigma); %calculate the eigenvalue&eigenvector
%eigenvalue in D,and vectors in V
D1=diag(D); %the eigenvalues
%%%%%%%%%%%%%%%%%%%%%%%% Sorting and eliminating eigenvalues
% At first : sort desc
Dsort=flipud(D1);
Vsort=fliplr(V);
%choose part eigenvalues
Dsum = sum(Dsort); %sum of the eigenvalues,we only choose 80%
%we have different ways to choose
%eigenvalues we need,90%,or>1……
temp_sum = 0;
p = 0;
while(temp_sum/Dsum<0.8)
p = p+1;
temp_sum = sum(Dsort(1:p));
end
%End of sort part
%%%%%%%%%%%%%%%%%%%%%%%Train Step: get the coordinate system
i=1;
while(i<=p && Dsort(i)>0)
face_base(:,i) = Dsort(i)^(-1/2) * xmean' * Vsort (:,i);
i=i+1;
end
% Dsort(i)^(-1/2) used to normalize, make variance=1
% face_base is N*p
% xmean' * Vsort (:,i); is change small matrix to big matrix. CHACHENG(Chinese)
%next sentence is vary important is our train result
allcoor = Data * face_base;
%End of training
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%Draw part
%draw CDF
x = Dsort (1:p);
x = flipud (x);
for i=1:p
y(i) = 1/p*i;
end
figure,plot(x,y,'r*'),hold on,plot(x,y,'b');
axis([0 inf 0 1]);
title('CDF of selected values','fontsize',18);
%draw the face (only use the first value) to show the result
for k=1
temp = reshape(face_base(:,k),147,137); %1D to 2D
end
figure,imshow(mat2gray(temp)); %draw it
title('one eigen face','fontsize',18);
%draw the face (use 100 values) to show the result
BigMap = [];
for k=1:4
map = [];
for j=1:4
temp = reshape(face_base(:,(k-1)*4+j),147,137);%1D to 2D
map = cat(2,map,temp);
end
BigMap = cat (1,BigMap,map);
end
figure,imshow(mat2gray(BigMap)); %draw it
Title ('Result of Chosen EigenValues','fontsize',18);
%End of drwing part
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%Test Part
Bingo = 0;
for i=1:Num_subject
for j=Test_num_image:Num_image %read the left 15*3 images
path = sprintf('FaceDB_yaleA/%03d/%02d.jpg',i,j);
test_pic = imread(path);
test_pic_line = test_pic(1:147*137);
test_pic_line = double(test_pic_line);
tcoor= test_pic_line * face_base; %get the value
for k=1:Num_subject* Train_num_image
mdist(k)=norm(tcoor-allcoor(k,:)); %norm() distence
end;
%三阶近邻
[dist,index2] = sort(mdist);
class1=floor( (index2(1)-1)/Train_num_image)+1;
class2=floor((index2(2)-1)/Train_num_image)+1;
class3=floor((index2(3)-1)/Train_num_image)+1;
if class1~=class2 && class2~=class3 ID = class1;
elseif (class1==class2) ID = class2;
elseif (class2==class3) ID = class3;
end;
if ID==i
Bingo=Bingo+1;
end;
end;
end;
accuracy = Bingo/45; %output the result
disp(accuracy);
% End of Test
附件里面,faceDB是使用的数据库。PCA是原始的程序。ProgramResult是一些简单的结论。直接把m文件和数据库文件夹放在一个目录下面就可以运行了。
分享到:
相关推荐
该系统为基于MATLAB平台的PCA的人脸识别系统,可识别ORL和YALE人脸库,方法实现统一,包括GUI界面。另外可二次开发成摄像头的实时人脸系统,识别出库外人脸,可做成门禁系统,考勤系统,打卡签到系统。实现登记出勤...
直接下载,在matlab运行即可。里面的result是我当时写的结果,如果不需要可以删除。
本设计为基于matlab的人脸识别系统,可读取ORL或者YALE人脸库或者自己的人脸,采用的方法可以通过下拉框形式切换成PCA,KPCA,LDA,K-L,BP神经网络多种方式,计算识别率,也可以调用笔记本自带摄像头进行识别。...
基于PCA主成分分析算法和Yale人脸数据库,采用Matlab语言实现的人脸识别程序,整个程序分为预处理、训练、识别、GUI界面控制等几个模块,注释清晰,通俗易懂。
matlab 实现基于 PCA 的人脸识别算法 一、 问题描述 在一个 yale 人脸库中,有 15 个人,每人有 11 幅图像。要求选定每一个人的 若干幅图像组成样本库,由样本库得到特征库。再任取 yale 图像库的一张图片, 识别它...
在一个yale人脸库中,有15个人,每人有11幅图像。要求选定每一个人的若干幅图像组成样本库,由样本库得到特征库。...因此,本次采用PCA算法确定一个子空间,最后使用最小距离法进行识别,并用matlab实现。
人脸识别算法实现 基于PCA降维和SVM分类器实现 包含工程文件代码和测试人脸数据库
MATLAB平台上先用pca降维之后的yale人脸识别算法
是基于主成分分析的算法,调试后,大概有80几%的识别率
Matlab代码,基于PCA算法原理实现人脸识别 感谢两篇帖子带来的启发 matlab实现基于PCA的人脸识别算法 人脸识别之主成分分析(PCA) 使用方法: 运行PCA.m主脚本 选择test文件夹下的一张图片 即可获得识别后最接近的...
改进PCA算法,不过还没找到python版本的,估计git上有吧。基本上都是matlab版本的,看到博客https://blog.csdn.net/alvine008/article/details/9097109,然后就仿真出来了,本文包括设置不同的主子空间阈值,计算...
利用传统PCA方法进行人脸识别的算法,人脸库为Yale人脸库,主成分分析方法(PCA)是基于K-L变换的统计学方法,K-L变换是数据压缩领域里的一种最优正交变换。
为了提高人脸识别的识别率,提出了一种基于自适应对数变换和主成分分析(PCA)算法的人脸识别方法。将人脸图像进行自适应对数变换...在ORL和Yale人脸数据库上进行了使用验证,结果表明该方法能够提高人脸识别的识别率。