Matlab 人脸识别源码分享.docx
- 文档编号:8780606
- 上传时间:2023-02-01
- 格式:DOCX
- 页数:8
- 大小:17.35KB
Matlab 人脸识别源码分享.docx
《Matlab 人脸识别源码分享.docx》由会员分享,可在线阅读,更多相关《Matlab 人脸识别源码分享.docx(8页珍藏版)》请在冰豆网上搜索。
Matlab人脸识别源码分享
计划实现了一个基于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:
%%%%%%%%%%%%%%%%%%%%%%%%
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:
%%%%%%%%%%%%%%%%%%%%%%%%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;%ListhesurrogateofcovariancematrixC=A*A'.
[VD]=eig(L);%DiagonalelementsofDaretheeigenvaluesforbothL=A'*AandC=A*A'.
%%%%%%%%%%%%%%%%%%%%%%%%Sortingandeliminatingeigenvalues
%AlleigenvaluesofmatrixLaresortedandthosewhoarelessthana
%specifiedthreshold,areeliminated.Sothenumberofnon-zero
%eigenvectorsmaybelessthan(P-1).
L_eig_vec=[];
fori=1:
size(V,2)
if(D(i,i)>1)
L_eig_vec=[L_eig_vecV(:
i)];
end
end
%%%%%%%%%%%%%%%%%%%%%%%%Calculatingtheeigenvectorsofcovariancematrix'C'
%EigenvectorsofcovariancematrixC(orso-called"Eigenfaces")
%canberecoveredfromL'seiegnvectors.
Eigenfaces=A*L_eig_vec;%A:
centeredimagevectors
----------------------------------------------------------------------------------------------------------------------
Recognition.m
functionOutputName=Recognition(TestImage,m,A,Eigenfaces)
%Recognizingstep....
%
%Description:
Thisfunctioncomparestwofacesbyprojectingtheimagesintofacespaceand
%measuringtheEuclideandistancebetweenthem.
%
%Argument:
TestImage -Pathoftheinputtestimage
%
% m -(M*Nx1)Meanofthetraining
% database,whichisoutputof'EigenfaceCore'function.
%
% Eigenfaces -(M*Nx(P-1))Eigenvectorsofthe
% covariancematrixofthetraining
% database,whichisoutputof'EigenfaceCore'function.
%
% A -(M*NxP)Matrixofcenteredimage
% vectors,whichisoutputof'EigenfaceCore'function.
%
%Returns:
OutputName -Nameoftherecognizedimageinthetrainingdatabase.
%
%Seealso:
RESHAPE,STRCAT
%OriginalversionbyAmirHosseinOmidvarnia,October2007
% Email:
%%%%%%%%%%%%%%%%%%%%%%%%Projectingcenteredimagevectorsintofacespace
%Allcenteredimagesareprojectedintofacespacebymultiplyingin
%Eigenfacebasis's.Projectedvectorofeachfacewillbeitscorresponding
%featurevector.
ProjectedImages=[];
Train_Number=size(Eigenfaces,2);
fori=1:
Train_Number
temp=Eigenfaces'*A(:
i);%Projectionofcenteredimagesintofacespace
ProjectedImages=[ProjectedImagestemp];
end
%%%%%%%%%%%%%%%%%%%%%%%%ExtractingthePCAfeaturesfromtestimage
InputImage=imread(TestImage);
temp=InputImage(:
:
1);
[irowicol]=size(temp);
InImage=reshape(temp',irow*icol,1);
Difference=double(InImage)-m;%Centeredtestimage
ProjectedTestImage=Eigenfaces'*Difference;%Testimagefeaturevector
%%%%%%%%%%%%%%%%%%%%%%%%CalculatingEuclideandistances
%Euclideandistancesbetweentheprojectedtestimageandtheprojection
%ofallcenteredtrainingimagesarecalculated.Testimageis
%supposedtohaveminimumdistancewithitscorrespondingimageinthe
%trainingdatabase.
Euc_dist=[];
fori=1:
Train_Number
q=ProjectedImages(:
i);
temp=(norm(ProjectedTestImage-q))^2;
Euc_dist=[Euc_disttemp];
end
[Euc_dist_min,Recognized_index]=min(Euc_dist);
OutputName=strcat(int2str(Recognized_index),'.jpg');
-----------------------------------------------------------------------------------------------------------
example.m
%Asamplescript,whichshowstheusageoffunctions,includedin
%PCA-basedfacerecognitionsystem(Eigenfacemethod)
%
%Seealso:
CREATEDATABASE,EIGENFACECORE,RECOGNITION
%OriginalversionbyAmirHosseinOmidvarnia,October2007
% Email:
clearall
clc
closeall
%Youcancustomizeandfixinitialdirectorypaths
TrainDatabasePath=uigetdir('D:
\MATLAB701\work','D:
\MATLAB701\work\TrainDatabase');
TestDatabasePath=uigetdir('D:
\MATLAB701\work','D:
\MATLAB701\work\TestDatabase');
prompt={'1:
'};
dlg_title='InputofPCA-BasedFaceRecognitionSystem';
num_lines=1;
def={'1'};
TestImage =inputdlg(prompt,dlg_title,num_lines,def);
TestImage=strcat(TestDatabasePath,'\',char(TestImage),'.jpg');
im=imread(TestImage);
T=CreateDatabase(TrainDatabasePath);
[m,A,Eigenfaces]=EigenfaceCore(T);
OutputName=Recognition(TestImage,m,A,Eigenfaces);
SelectedImage=strcat(TrainDatabasePath,'\',OutputName);
SelectedImage=imread(SelectedImage);
imshow(im)
title('TestImage');
figure,imshow(SelectedImage);
title('EquivalentImage');
str=strcat('Matchedimageis:
',OutputName);
disp(str)
源代码来自Matlab源码:
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Matlab 人脸识别源码分享 识别 源码 分享
![提示](https://static.bdocx.com/images/bang_tan.gif)