资源简介
用matlab实现的2dpca人脸识别程序,运行速度比传统的pca方法快至少10倍, 识别率也比pca 高 -Using matlab to achieve 2dpca face recognition program, run faster than the traditional PCA method at least 10 times faster, than the PCA recognition rate is high
代码片段和文件信息
function []=TwoDPCA
%%%%%%%%%%%%%特征脸显示已正确,训练与测试没有分开。
% Face recognition
clear all
close all
clc
M=200;%%%%
traincopy=5;%%%表示同一个人有几张相片。
eignum=3;%%%选取的特征个数。
cel=cell(1M);
cellafter=cell(1M);
tt=clock;
S=[];
ii=1;
str=strcat(‘E:\三维人脸\2dfacedatabase\ORL\s1\1.pgm‘);
img=imread(str);
[ia ib]=size(img);
sum=zeros(iaib);
B=zeros(iaib);
for i=1:40
for j=1:5
str=strcat(‘E:\三维人脸\2dfacedatabase\ORL\s‘int2str(i)‘\‘int2str(j)‘.pgm‘);
eval(‘img=imread(str);‘);
sum=double(sum)+double(img);
cel{1ii}=img;
ii=ii+1;
end
end
meanA=sum/M;
cov=zeros(ib);
for i=1:M
img=cel{1i};
B=double(img)-double(meanA);
temp=B‘*B;
cov=double(cov)+double(temp);
end
[vv dd]=eig(cov);
num2=size(vv);
% Sort and eliminate those whose eigenvalue is zero
v=[];
d=[];
for i=1:size(vv2)
if(dd(ii)>1e-4)
v=[v vv(:i)];
d=[d dd(ii)];
end
end
num1=size(v2);
%sort will return an ascending sequence
[B index]=sort(d);
ind=zeros(size(index));
dtemp=zeros(size(index));
vtemp=zeros(size(v));
len=length(index);
for i=1:len
dtemp(i)=B(len+1-i);
ind(i)=len+1-index(i);
vtemp(:ind(i))=v(:i);
end
d=dtemp;
v=vtemp;
imgafter=[];
for i=1:M
for j=1:eignum
img=cel{1i};
temp1=double(img)*double(v(:j));
imgafter=[imgafter temp1];
end
cellafter{1i}=imgafter;
imgafter=[];
end
timeconsume=etime(clocktt);
testimg=M/traincopy;
findimgnum=traincopy;
suc=0;
% figure(5);
for k=1:testimg
InputImage =imread(strcat(‘E:\三维人脸\2dfacedatabase\ORL\s‘int2str(k)‘\10.pgm‘));
testafter=[];
for j=1:eignum
temp=double(InputImage)*double(v(:j));
testafter=[testafter temp];
end
% Find Euclidean distance
e=[];
for i=1:M
tempA=double(testafter)-double(cellafter{1i});
total=0;
for j=1:eignum
aa=norm(tempA(:j));
total=total+aa;
end
e=[e total];
end
[C index]=sort(e);
min=index(1);
%%%%%%%计算正确率
testingroup=floor((min-1)/traincopy)+1; %%计算要测试的图像所在的组
if testingroup==k
suc=suc+1;
else
fprintf(‘%d.jpg fails to match!\n‘k);
end
% %%%%%%%%%%%%%%%%%%%显示所有找到的与测试图像为同一个人的图片(可)。(显示所有与测试图片最小距离的那组,而不是比较出来的最小的5个)
% subplot(testimgfindimgnum+1(k-1)*(findimgnum+1)+1);% subplot(行数,列数,放图像位置的序数)
% imshow(InputImage);
%
% for i=1:findimgnum
% temppos=(testingroup-1)*traincopy+i;
% str=strcat(‘E:\三维人脸\testpic\orl\‘int2str(temppos)‘.pgm‘); %concatenates two strings that form the name of the image
% eval(‘img1=imread(str);‘);
% subplot(testimgfindimgnum+1(k-1)*(findimgnum+1)+i+1)
% imshow(img1)
% drawnow;
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 3329 2008-03-13 22:22 TwoDPCA.m
----------- --------- ---------- ----- ----
3547 2
- 上一篇:空气参数计算器
- 下一篇:三相逆变器及simuli
nk仿真
评论
共有 条评论