人脸识别源码分享.docx
《人脸识别源码分享.docx》由会员分享,可在线阅读,更多相关《人脸识别源码分享.docx(8页珍藏版)》请在冰豆网上搜索。
![人脸识别源码分享.docx](https://file1.bdocx.com/fileroot1/2022-10/9/8fc312fd-8af3-482f-87b9-3c4286a8af84/8fc312fd-8af3-482f-87b9-3c4286a8af841.gif)
人脸识别源码分享
计划实现了一个基于PCA的人脸识别方法,我称之为“特征点方法”,所有的功能简单而且实用。
下面,我使用一个简单的MATLAB脚本说明它的用法。
一般情况,你应该按照以下这个顺序执行这个方法:
1. 选择实际测试和参照组人脸图像数据库的路径;
2. 选择实际测试人脸图像的路径;
3. 运行“CreateDatabase”函数来创建所有参照组人脸图像的二维矩阵;
4. 运行“eigenfacecore"函数产生基础人脸图像空间;
5. 运行“识别”功能得到参照组人脸图像数据库中等价图像的名称。
为了您的方便,我准备了实际测试和参照组人脸图像数据库,其中部分来自“Face94”埃塞克斯人脸数据库。
你只需要复制上述功能,指定实际测试和参照组人脸图像数据库的路径(比如Matlab工作路径)。
然后按照对话框提示输入图片编号,实例将运行实现。
希望您能喜欢它!
引用:
[1]P.N.Belhumeur,J.Hespanha,andD.J.Kriegman.Eigenfacesvs.Fisherfaces:
Recognition
usingclassspecificlinearprojection.InECCV
(1),pages45--58,1996.
[2]Availableat:
以下为源代码文件:
----------------------------------------------------------------------------------------------------------------------
CreateDatabase.m
functionT=CreateDatabase(TrainDatabasePath)
%Alignasetoffaceimages(thetrainingsetT1,T2,...,TM)
%
%Description:
Thisfunctionreshapesall2Dimagesofthetrainingdatabase
%into1Dcolumnvectors.Then,itputsthese1Dcolumnvectorsinarowto
%construct2Dmatrix'T'.
%
%Argument:
TrainDatabasePath -Pathofthetrainingdatabase
%
%Returns:
T -A2Dmatrix,containingall1Dimagevectors.
% SupposeallPimagesinthetrainingdatabase
% havethesamesizeofMxN.Sothelengthof1D
% columnvectorsisMNand'T'willbeaMNxP2Dmatrix.
%
%Seealso:
STRCMP,STRCAT,RESHAPE
%OriginalversionbyAmirHosseinOmidvarnia,October2007
% Email:
***************.ac.ir
%%%%%%%%%%%%%%%%%%%%%%%%
TrainFiles=dir(TrainDatabasePath);
Train_Number=0;
fori=1:
size(Train)
ifnot(strcmp(TrainFiles(i).name,'.')|strcmp(TrainFiles(i).name,'..')|strcmp(TrainFiles(i).name,'Thumbs.db'))
Train_Number=Train_Number+1;%Numberofallimagesinthetrainingdatabase
end
end
%%%%%%%%%%%%%%%%%%%%%%%%Constructionof2Dmatrixfrom1Dimagevectors
T=[];
fori=1:
Train_Number
%Ihavechosenthenameofeachimageindatabasesasacorresponding
%number.However,itisnotmandatory!
str=int2str(i);
str=strcat('\',str,'.jpg');
str=strcat(TrainDatabasePath,str);
img=imread(str);
img=rgb2gray(img);
[irowicol]=size(img);
temp=reshape(img',irow*icol,1); %Reshaping2Dimagesinto1Dimagevectors
T=[Ttemp];%'T'growsaftereachturn
end
---------------------------------------------------------------------------------------------------------------------------
EigenfaceCore.m
function[m,A,Eigenfaces]=EigenfaceCore(T)
%UsePrincipleComponentAnalysis(PCA)todeterminethemost
%discriminatingfeaturesbetweenimagesoffaces.
%
%Description:
Thisfunctiongetsa2Dmatrix,containingalltrainingimagevectors
%andreturns3outputswhichareextractedfromtrainingdatabase.
%
%Argument:
T -A2Dmatrix,containingall1Dimagevectors.
% SupposeallPimagesinthetrainingdatabase
% havethesamesizeofMxN.Sothelengthof1D
% columnvectorsisM*Nand'T'willbeaMNxP2Dmatrix.
%
%Returns:
m -(M*Nx1)Meanofthetrainingdatabase
% Eigenfaces -(M*Nx(P-1))Eigenvectorsofthecovariancematrixofthetrainingdatabase
% A -(M*NxP)Matrixofcenteredimagevectors
%
%Seealso:
EIG
%OriginalversionbyAmirHosseinOmidvarnia,October2007
% Email:
***************.ac.ir
%%%%%%%%%%%%%%%%%%%%%%%%Calculatingthemeanimage
m=mean(T,2);%Computingtheaveragefaceimagem=(1/P)*sum(Tj's) (j=1:
P)
Train_Number=size(T,2);
%%%%%%%%%%%%%%%%%%%%%%%%Calculatingthedeviationofeachimagefrommeanimage
A=[];
fori=1:
Train_Number
temp=double(T(:
i))-m;%ComputingthedifferenceimageforeachimageinthetrainingsetAi=Ti-m
A=[Atemp];%Mergingallcenteredimages
end
%%%%%%%%%%%%%%%%%%%%%%%%SnapshotmethodofEigenfacemethos
%WeknowfromlinearalgebratheorythatforaPxQmatrix,themaximum
%numberofnon-zeroeigenvaluesthatthematrixcanhaveismin(P-1,Q-1).
%Sincethenumberoftrainingimages(P)isusuallylessthanthenumber
%ofpixels(M*N),themostnon-zeroeigenvaluesthatcanbefoundareequal
%toP-1.SowecancalculateeigenvaluesofA'*A(aPxPmatrix)insteadof
%A*A'(aM*NxM*Nmatrix).ItisclearthatthedimensionsofA*A'ismuch
%largerthatA'*A.Sothedimensionalitywilldecrease.
L=A'*A;%Listhesurrogateof