clear all
%%%%%%%%%%%%%%%设置参数
K1=2; %邻域大小K
K2=6;
t = 1.0e+007;
% NumofEigenvectors=7; %取非零最小特征向量个数
Class_train_num = 3; %%%%每个类选择的训练样本个数
Class_num = 20; %%%%%类别个数
Class_image_num = 19; %%%%%每个类的图象总数
Class_test_num = Class_image_num-Class_train_num; %%%%%每个类选择的测试样本个数
Train_num = Class_num*Class_train_num;
Test_num = Class_num*Class_test_num;
fid=fopen('C:\Documents and Settings\Administrator\桌面\2D_DLPP等\MFA\MFA_UMIST_1_3.txt','a');
fprintf(fid,'%12s','K1邻域大小:');
fprintf(fid,'%12d\n',K1);
fprintf(fid,'%20s','K2领域大小:');
fprintf(fid,'%12d\n',K2);
fprintf(fid,'%12s','t:');
fprintf(fid,'%12d\n',t);
fprintf(fid,'%20s','Class number:');
fprintf(fid,'%12g',Class_num);
fprintf(fid,'%20s','Image number:');
fprintf(fid,'%12g',Class_image_num);
fprintf(fid,'%20s','Training number:');
fprintf(fid,'%12g\n',Class_train_num);
filename = 'D:\人脸识别\MATLAB实现\资料\UMIST\s';
for i = 1 : Class_num
for j = 1 : Class_train_num
fname = strcat(filename,int2str(i),'\',int2str(j),'.pgm');
a = double(imread(fname));
[row column]=size(a);
index1=column*((i-1)*Class_train_num+(j-1))+1;
index2=((i-1)*Class_train_num+j)*column;
train_matrix(:,index1:index2) = a; %%%%%%读入训练图像
clear a;
end
for k = 1 : Class_test_num
fname1 = strcat(filename,int2str(i),'\',int2str(k+Class_train_num),'.pgm');
a = double(imread(fname1));
[row column]=size(a);
index3=((i-1)*Class_test_num+k-1)*column+1;
index4=((i-1)*Class_test_num+k)*column;
test_matrix(:,index3:index4) = a; %%%%%%读入测试图像
clear a;
end
end
clear i j k
distance=zeros(Train_num,Train_num);
for i=1:Train_num
Image_matrix1=train_matrix(:,((i-1)*column+1):column*i);
for j=(i+1):Train_num
Image_matrix2=train_matrix(:,(column*(j-1)+1):column*j);
diff_matrix=Image_matrix1-Image_matrix2;
distance(i,j)=norm(diff_matrix,'fro');
end
end
distance=distance+distance';
clear i j
[distance1,Index1]=sort(distance,2);
for i=1 : Train_num
ii = 1;
jj = 1;
for j = 1 : Train_num
if Index1(i,j)>=(fix((i-1)/Class_train_num)*Class_train_num+1) && Index1(i,j)<=(fix((i-1)/Class_train_num)*Class_train_num+Class_train_num)
distance2(i,ii)=distance1(i,Index1(i,j));
Index2(i,ii)=Index1(i,j);
ii = ii+1;
else
distance3(i,jj)=distance1(i,Index1(i,j));
Index3(i,jj)=Index1(i,j);
jj = jj+1;
end
end
end
clear i j ii jj
S=zeros(Train_num,Train_num);
for i = 1:Train_num
for j = 1:K1+1
S(i,Index2(i,j))=exp((-1)*(distance2(i,j)^2)/t);
end
end
clear i j
B=zeros(Train_num,Train_num);
for i = 1 : Train_num
for j = 1 : K2+1
B(i,Index3(i,j))=exp((-1)*(distance3(i,j)^2)/t);
end
end
S=max(S,S');
B=max(B,B');
D1=diag(sum(B));
D = diag(sum(S));
L=D-S;
L1=D1-B;
x1=zeros(row,column*Train_num);
x2=zeros(row,column*Train_num);
clear i j
for i=1:Train_num
% x1(:,((i-1)*column+1):column*i)=train_matrix(:,((i-1)*column+1):column*i)*D1(i,i);
for j=1:Train_num
x1(:,((i-1)*column+1):column*i)=x1(:,((i-1)*column+1):column*i)+train_matrix(:,((j-1)*column+1):column*j)*L1(j,i);
x2(:,((i-1)*column+1):column*i)=x2(:,((i-1)*column+1):column*i)+train_matrix(:,((j-1)*column+1):column*j)*L(j,i);
end
end
DX=x1*train_matrix';
LX=x2*train_matrix';
[v,d]=eig(LX,DX);
D2=diag(d);
[eigvalues,Index]=sort(D2);
eigvectors=v(:,Index);
clear fname fname1 row Image_matrix1 Image_matrix2 distance index1 index2 index3 index4 i j Index Index1
for NumofEigenvectors = 1 : 15
fprintf(fid,'%12s','取非零最小特征向量个数:');
fprintf(fid,'%12d\n',NumofEigenvectors);
eigvectors1=eigvectors(:,1:NumofEigenvectors); %%%%%取前NumofEigenvectors个最小非零特征值对应特征向量
Train_Z=(eigvectors1)'*train_matrix; %%训练样本的LPP投影矩阵
Test_Z=(eigvectors1)'*test_matrix;
distance_1=zeros(Test_num,Train_num);
for i=1:Test_num
Image_matrix1=Test_Z(:,((i-1)*column+1):column*i);
for j=1:Train_num
Image_matrix2=Train_Z(:,(column*(j-1)+1):column*j);
diff_matrix=Image_matrix1-Image_matrix2;
% distance(j,k)=norm(diff_matrix);
for k = 1: column
distance_1(i,j) = distance_1(i,j) + norm(diff_matrix(:,k));
end
end
end
% for m1=1:Test_num %求对应最小距离
% f(m1)=1;
% for n1=1:Train_num-1
% if distance(m1,f(m1))>distance(m1,n1+1)
% f(m1)=n1+1;
% end
% end
% end
[distance1,Index] = sort(distance_1,2);
NumofTrue=0;
for i=1 : Class_num
begin=Class_train_num*(i-1);
for j=1 : Class_test_num
if (Index(((i-1)*Class_test_num+j),1)>begin) && (Index(((i-1)*Class_test_num+j),1)<=Class_train_num*i)
NumofTrue=NumofTrue+1;
end
end
end
% for i=1 : Class_num
% begin=Class_num*(i-1);
% for j=1 : Test_num/Class_num
% if (f((i-1)*Test_num/Class_num+j)>begin) && (f((i-1)*Test_num/Class_num+j) <=Class_num*i)
% NumofTrue=NumofTrue+1;
% end
% end
% end
ratio=NumofTrue/Test_num;
Ratio(NumofEigenvectors)=ratio;
fprintf(fid,'%20s','测试正确率');
fprintf(fid,'%12g\n',ratio);
end
fprintf(fid,'%12g\n',Ratio');
fclose(fid);