%%该程序主要用于训练样本
%读取文件名为1人的手指静脉图片
file_path='E:\dataset\fv\fingervein2_2\'; %图片文件夹路径
img_path_list=dir(strcat(file_path,'*.bmp')); %获取图片文件夹下所有bmp格式的图片
img_num=length(img_path_list); %获取图片总数量
%%
l1=5;
l4=5;
%%
cc=210;
ni=3;
for j=1:cc
sum1=0;
for i=1:ni
image_name=img_path_list(i).name; %图像名
image=imread(strcat(file_path,image_name));
%img1(:,:,i)=rgb2gray(image);
img1(:,:,i)=image;
sum1=sum1+double(img1(:,:,i));
end
x(:,:,(j-1)*ni+1:j*ni)=img1(:,:,1:ni);
m(:,:,j)=sum1/ni;%计算每个手指的平均图像
[row,low]=size(sum1);
end
%总均值
sum13=0;
for i=1:cc
sum13=sum13+m(:,:,i);
end
averge=sum13/cc;
%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%
%去列相关
sum2=0;
for i=1:ni*cc
temp1=(double(x(:,:,i))-averge)'*(double(x(:,:,i))-averge);
sum2=sum2+temp1;
end
COR1=1/(ni*cc)*sum2;
[v,r]=eig(COR1);
[t1,t2]=size(v);
V1=v(:,t2-l1+1:t2);
sum(sum(r(:,t2-l1+1:t2)))/sum(sum(r));
%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%
%把原数据变换到2dpca中
for i=1:cc*ni
tem = double(x(:,:,i));
xx(:,:,i)=tem*V1;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%计算把特征空间的类均值投影到2dpca中
xx(:,:,:)=double(xx(:,:,:));
images=[];
for c=1:cc
mm(:,:,c)=m(:,:,c)*V1;
end
aver=averge*V1;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%对经过pca变换的数据在行方向上进行fisher变换
for c=1:cc
sum2=0;
for i=ni*(c-1)+1:ni*c
temp1=(double((xx(:,:,i)-mm(:,:,c)))*double((xx(:,:,i)-mm(:,:,c)))');
sum2=sum2+temp1;
end
end
kw=sum2/(ni*cc);
%把特征空间的总体均值投影到高维空间中
images=[];
sum3=0;
for i=1:cc
temp2=double((mm(:,:,i)-aver))*double((mm(:,:,i)-aver))';
sum3=sum3+temp2;
end
kb=sum3/cc;
rank(kw);
%求广义逆矩阵得到投影矩阵v
[v r]=eig(kb,kw);
t2=size(v);
V2=v(t2-l4+1:t2,:);
size(V2);
sum(sum(r(t2-l4+1:t2,:)))/sum(sum(r));
%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%
%各样本均值行和列方向上分别经过pca和fisher变换后作为训练样本的类中心
for i=1:cc
averg(:,:,i)=V2*double(m(:,:,i))*V1;
end
save('E:\dataset\fv\data_train.mat','V1','V2','averg','ni');
%训练结束
%err
%该程序用于测试样本的识别
load('E:\dataset\fv\data_train.mat','V1','V2','averg','ni');
%%
file_path='E:\dataset\fv\fingervein2_2\'; %图片文件夹路径
img_path_list=dir(strcat(file_path,'*.bmp')); %获取图片文件夹下所有bmp格式的图片
%%
%用最小距离分类器进行分类并计算错误率
cc=210;
n=6;
e=0;
for j=1:cc
for i=ni+1:n
image_name=img_path_list(i).name; %图像名
image=imread(strcat(file_path,image_name));
img=image;
sum10=V2*double(img)*V1;%将待识别的样本投影到识别空间
dd=[];
for l=1:cc
d(l)=norm(sum10-averg(:,:,l)); %计算距离
dd=[dd d(l)]
end
mind=min(dd(1:cc));
if(d(j)~=mind)
e=e+1;
end
end
end
disp('错分类的样本数为:');
disp(e);
disp('识别率;');
disp(1-e/(cc*(n-ni)));
错分样本数为0,分类准确率100%。
转载于:https://www.cnblogs.com/natalie/p/5969902.html
相关资源:人脸识别经典算法PCA以及2DPCA完美matlab版