EigenFace算法详解及Matlab代码0103.docx

上传人:b****6 文档编号:7622047 上传时间:2023-01-25 格式:DOCX 页数:19 大小:974.65KB
下载 相关 举报
EigenFace算法详解及Matlab代码0103.docx_第1页
第1页 / 共19页
EigenFace算法详解及Matlab代码0103.docx_第2页
第2页 / 共19页
EigenFace算法详解及Matlab代码0103.docx_第3页
第3页 / 共19页
EigenFace算法详解及Matlab代码0103.docx_第4页
第4页 / 共19页
EigenFace算法详解及Matlab代码0103.docx_第5页
第5页 / 共19页
点击查看更多>>
下载资源
资源描述

EigenFace算法详解及Matlab代码0103.docx

《EigenFace算法详解及Matlab代码0103.docx》由会员分享,可在线阅读,更多相关《EigenFace算法详解及Matlab代码0103.docx(19页珍藏版)》请在冰豆网上搜索。

EigenFace算法详解及Matlab代码0103.docx

EigenFace算法详解及Matlab代码0103

EigenFace算法回顾及Matlab代码

基于PCA的EigenFace算法发表自1987年,是第一种可行的人脸辨识算法。

虽然已有20余年历史,但仍是人脸辨识算法研究中的经典,新算法都要与之作比较。

EigenFace是2D辨识算法,但为了进行3D表情辨识的研究,有必要对这一经典算法进行回顾,SIGGRAPH13的文献Online Modeling For Realtime Facial Animation实现表情3D重构的基础是SIGGRAPH99中A Morphable Model for the Synthesis of 3D Faces提出的Morphable Facial Model,而建立这一模型的基础思想仍是PCA,与Eigenface有着天然联系。

学习EigenFace应该是研究生时代的事儿了,旧编重拾、开卷有益,并写了Matlab代码附录于后。

网上许多实例代码只实现了辨识,略去了一个重要环节:

通过分解-重构,将一幅输入人脸照片表示为EigenFace基底的组合,这对于表情辨识及3D人脸模型分析都是很有用的(Online Modeling For Realtime Facial Animation中就利用了此思想),附录的Matlab代码做了这一步。

 

                                          训练集合包含20幅图片

                                                 生成20-1=19个特征脸

                最近欧式距离法得出的前三位匹配

 

                         利用特征脸空间进行人脸重构

http:

//www.pages.drexel.edu/~sis26/Eigenface Tutorial.htm中给出了EigenFace算法Matlab 代码,含有重构过程与比较,但其代码中有一个错误,训练集合照片未减去平均脸,就计算协方差矩阵了

具体算法文献教材上都有,捡要点写几句:

(1) 将训练集合中的每幅图像拉伸为列向量,并减去所有图像的均值(称之为平均脸),形成N*M矩阵A,其中N为单幅图像像素数,M为图像数目(训练集合容量)。

(2) 求协方差矩阵AA'的特征向量,作为正交基底张成人脸空间,好是好但运算量过大,转而求替代矩阵(surrogate)A'A的特征向量,减少计算量

(3) 矩阵A'A的秩等于M-1,这是由于减去平均脸所致,故有M-1个非零特征值(正),去除属于0的特征向量,将M-1个属于非零特征值得特征向量(记住须作左乘A的修正)作为EigenFace基底(特征脸),张成人脸空间。

(4) EigenFace基底由M-1个相互正交的向量构成,它们是协方差矩阵AA'的前M-1个最显著的特征向量方向,能量主要集中在这些向量方向上,但要记住虽正交但不完备,故存在重构误差。

(5) 为了用EigenFace基底对人脸照片进行正确的分解-重构,需要对所得的基底向量进行规一化修正,因为A'A的特征向量左乘A之后,虽成为AA'的特征向量,但模不为1,需除以自身的模,修正为标准正交向量集合,才能进行投影分解-重构。

(6) 训练集合及测试集合中的人脸照片都能利用EigenFace基底较好地实现分解-重构,但训练集合之外的人脸,重构误差变大

(7) EigenFace缺点:

1拍摄时光照环境对识别效果(EigenFace基底)影响大

                                  2训练集合扩容时,需重构EigenFace基底

(8) 为了有效显示EigenFace基底图像(特征脸)需要用imagesc函数

Eigenface算法识别人脸的步骤:

Thissectiongivesstep-by-stepinstructionsalongwithphotosandformulasonhowtorecognizefacesandimplementedintoMatlab.Allthenecessaryfilestocompletethistutorialwouldbeprovided.

Steps

1.ThefirststepistoobtainasetSwithMfaceimages.InourexampleM=25asshownatthebeginningofthetutorial.EachimageistransformedintoavectorofsizeNandplacedintotheset.

2.Afteryouhaveobtainedyourset,youwillobtainthemeanimageΨ

 

3.ThenyouwillfindthedifferenceΦbetweentheinputimageandthemeanimage

4.NextweseekasetofMorthonormalvectors,un,whichbestdescribesthedistributionofthedata.Thekthvector,uk,ischosensuchthat

求特征值:

isamaximum,subjectto

Note:

ukandλkaretheeigenvectorsandeigenvaluesofthecovariancematrixC

 

5.WeobtainthecovariancematrixCinthefollowingmanner

 

Wherethematrix

.ThematrixC,however,isN2byN2,anddeterminingtheN2eigenvectorsandeigenvaluesisanintractabletaskfortypicalimagesizes.Weneedacomputationallyfeasiblemethodtofindtheseeigenvectors.

WecansolvefortheN2dimensionaleigenvectorsinthiscasebyfirstsolvingfortheeigenvectorsofanMbyMmatrix-e.g.,solvinga16*16matrixratherthana16,384*16,384matrixandthentakingappropriatelinearcombinationsofthefaceimagesΦi,ConsidertheeigenvectorsviofATAsuchthat

ATAvi=μivi

PremultiplyingbothsidesbyA,wehave

AATAvi=μiAvi

FromwhichweseethatAvi(特征向量)aretheeigenvectorsofC=AAT.

Followingthisanalysis,weconstructtheMbyMmatrixL=ATA,where

(Φ是1*16,384维的向量),andfindtheMeigenvectors,vi(M维的向量)ofL.ThesevectorsdeterminelinearcombinationsoftheMtrainingsetfaceimagestoformtheeigenfacesul(特征脸向量).Oncewehavefoundtheeigenvectors,vl,ul

l=1,2,…M

6.Thesearetheeigenfacesofoursetoforiginalimages

RecognitionProcedure

1.Anewfaceistransformedintoitseigenfacecomponents.FirstwecompareourinputimagewithourmeanimageandmultiplytheirdifferencewitheacheigenvectoroftheLmatrix.EachvaluewouldrepresentaweightandwouldbesavedonavectorΩ.

新脸向量在每个特征脸向量上的投影:

  

Fork=1,2,…,M’.

2.Wenowdeterminewhichfaceclassprovidesthebestdescriptionfortheinputimage.ThisisdonebyminimizingtheEuclideandistance

3.Theinputfaceisconsidertobelongtoaclassifεkisbellowanestablishedthresholdθε.Thenthefaceimageisconsideredtobeaknownface.Ifthedifferenceisabovethegiventhreshold,butbellowasecondthreshold,theimagecanbedeterminedasaunknownface.Iftheinputimageisabovethesetwothresholds,theimageisdeterminedNOTtobeaface.

4.Iftheimageisfoundtobeanunknownface,youcoulddecidewhetherornotyouwanttoaddtheimagetoyourtrainingsetforfuturerecognitions.Youwouldhavetorepeatsteps1trough7toincorporatethisnewfaceimage.

ThesourcecodefacerecognitionusingMatlabisprovidedbelow:

 

Matlab代码:

所用训练集合含20幅jpg图片,存于名为TrainDataBase的文件夹中,测试集合含10幅jpg图片,存于名为TestDataBase的文件夹中,命名均为1.jpg  2.jpg  3.jpg........

%----------------------------------------------------------------------

%人脸辨识:

FaceRecognition

%EigenFace算法

%EigenFace空间的生成分解与合成

%人脸辨识

%-----------------------------------------------------------------------

functionmeigenface()

%------------------------------------------------------

%step1:

读取训练集合中的人脸照片,并通过列向量拉伸生成

%人脸训练集合矩阵

%------------------------------------------------------

clearall

clc

closeall

imset=[];

M=20;%训练集合中人脸照片的数目

figure

(1);

fori=1:

M

filename=['.\TrainDataBase\'num2str(i)'.jpg'];

im=imread(filename,'JPG');

im=im(:

:

2);

%im=im(:

:

3);

[imheightimwidth]=size(im);

%将图像矩阵按列拉伸为列向量

imv=reshape(im,imheight*imwidth,1);%将im中数据转化为imheight*imwidth高的列向量

imset=[imsetimv];%将每个图像的像素占据一列位置,共20列向量

ifi==4

title('训练集合人脸照片','fontsize',18)

end

subplot(5,4,i);

imshow(im);

end

disp('============Outputimageiscompleted!

===================')

%imset=double(imset);

imset=double(imset);

[w,h]=size(imset)

%------------------------------------------------------

%step2:

生成训练集合的平均脸

%------------------------------------------------------

averageface=mean(imset,2);

%imset每行的平均值,由于imset为单列向量,每行包含一个元素,因此此操作为将每行的所有值取平均值

figure

(2);

imshow(uint8(reshape(averageface,imheight,imwidth)));

title('平均脸');

%------------------------------------------------------

%step3:

将人脸训练集合修正为与平均人脸的差:

以便计算

%协方差矩阵Covariancematrix

%------------------------------------------------------

imset_d=imset;

figure(3);

fori=1:

20

imset_d(:

i)=imset(:

i)-averageface;

im=reshape(imset_d(:

i),imheight,imwidth);

ifi==4

title('与平均脸的差','fontsize',18)

end

%subplot(4,5,i);

subplot(5,4,i);

im=imagesc(im);colormap('gray');

%imshow(im);

end

%

%------------------------------------------------------

%step4:

生成EigenFace特征脸空间

%------------------------------------------------------

%互相关矩阵为A*A',为减少计算量,计算代理矩阵A'*A的特征值特征向量

A=double(imset_d);%includethedataoftwentyimages.%matrixof36000hightand20width

[Ah,Aw]=size(A)

L=A'*A;%matrixof20hightand20width

[Lh,Lw]=size(L)

[vd]=eig(L)%v为L的特征向量,d为L的特征值

dL=L*v-v*d

%人脸照片修正为与平均脸的差,此时矩阵A'*A的秩为M-1,特征值中含有一个0

%需要去除属于0的特征向量,Eigenface基底由M-1组正交向量组成

[vd]=EliminateZero(v,d)%去除属于0的特征向量,同时将特征向量按特征值大小降序排列

%生成人脸空间矩阵:

eigenface

eigenface=[];

fori=1:

M-1

mv=A*v(:

i);%左乘矩阵A,从而修正为协方差矩阵AA'的特征向量

%对Eigenface向量进行规一化修正,使每个Eigenface向量模为1,便于进行分解与合成

%是否进行此归一化修正对识别没有影响,但对利用Eigenface作为基底对人脸照片进行

%分解与重构却是必要的

mv=mv/norm(mv);%

eigenface=[eigenfacemv];%特征人脸空间

[eh,ew]=size(eigenface)

end

%显示归一化之后的人脸空间eigenface

figure(4);

fori=1:

M-1

im=eigenface(:

i);

im=reshape(im,imheight,imwidth);

ifi==4

title('特征脸空间:

EigenFace','fontsize',18)

end

subplot(5,4,i);

im=imagesc(im);colormap('gray');

%im=histeq(im,255);imshow(im);

end

%------------------------------------------------------

%step5:

计算训练集合中的照片在EigenFace基底上的投影坐标

%以便进行人脸辨识

%注:

如果用Eigenface基底直接对训练集合中的照片进行分解-重构

%则重构误差趋于0(1e-10),如果对测试集合中的人脸(同一人,但表情姿态略有出入)

%进行重构,则可观察到重构误差,这是由于EigenFace基底虽正交但不完备

%------------------------------------------------------

TrainCoeff=[];

fori=1:

M

%计算训练集合中第i幅图像在EigenFace基底上的投影

disp('The%dthfaceimage')

i=i

c=double(imset_d(:

i)')*eigenface;

TrainCoeff=[TrainCoeffc']

end

%------------------------------------------------------

%step6:

人脸辨识,采用最近欧式距离法

%计算输入图像在EigenFace基底上的投影坐标

%与训练集合中各图像的投影坐标进行比较,选取欧式距离最小者作为匹配项

%------------------------------------------------------

%读入测试集合人脸照片

N=10;%测试集合人脸照片数目

figure(5);

testset=[];

fori=1:

N

filename=['.\TestDataBase\'num2str(i)'.jpg'];

im=imread(filename,'JPG');

im=im(:

:

2);

[imheightimwidth]=size(im);

%将图像矩阵按列拉伸为列向量

imv=reshape(im,imheight*imwidth,1);

testset=[testsetimv];

%

subplot(3,4,i);

imshow(im);title(int2str(i));

end

text(200,15,'请选择一幅照片(1-10)','fontsize',18);

testset=double(testset);

n=input('请选择一幅照片(1-10):

');

%选取训练集合中第n幅人脸照片作为输入进行分解

InputImage=testset(:

n)-averageface;

%计算输入照片在EigenFace基底上的投影坐标向量

coeff=InputImage'*eigenface;

%计算与训练集合中各图像的投影坐标向量的欧式距离

cdist=[];

fori=1:

M

dist=(norm(coeff'-TrainCoeff(:

i)))^2;

cdist=[cdistdist];

end

[dindex]=sort(cdist)

inum=[index

(1),index

(2),index(3)];

figure(6);

subplot(2,3,2);

InputImage=reshape(InputImage+averageface,imheight,imwidth);

imshow(uint8(InputImage));title('输入照片');

subplot(2,3,4);

im1=imset(:

index

(1));

im1=reshape(im1,imheight,imwidth);

imshow(uint8(im1));title('匹配照片1');

subplot(2,3,5);

im2=imset(:

index

(2));

im2=reshape(im2,imheight,imwidth);

imshow(uint8(im2));title('匹配照片2');

subplot(2,3,6);

im2=imset(:

index(3));

im2=reshape(im2,imheight,imwidth);

imshow(uint8(im2));title('匹配照片3');

pause;close;

%------------------------------------------------------

%step6:

利用EigenFace基底对测试集合中的人脸照片进行

%分解-合成(重构)

%注:

如果用Eigenface基底直接对训练集合中的照片进行分解-重构

%则重构误差趋于0(1e-10),如果对测试集合中的人脸(同一人,但表情姿态略有出入)

%进行重构,则可观察到重构误差,这是由于EigenFace基底虽正交但不完备

%------------------------------------------------------

;%选取训练集合中第n幅人脸照片作为输入进行分解

InputImage=testset(:

n)-averageface;

%计算输入照片在EigenFace基底上的投影坐标

coeff=InputImage'*eigenface;

%在EigenFace基底上进行图像重构

ReconstructedImage=eigenface(:

1:

M-1)*coeff';

%计算重构误差

diff=ReconstructedImage-InputImage;

norm(diff),

InputImage=reshape(Inpu

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 经管营销 > 经济市场

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1