close all;
clc;
clear all;
%load face database
path=strcat('H:\深度数据\200305\') ; %%图像的路径是D盘得ORL,读取第i个人的第j个图像
nclass = 5; %数据库中有5个对象
nsmpaleeachclass = 16; %每个对象有1个人,分为16块
%图像集
for i=1:nclass
for j = 1:nsampleeachclass
X(:,(i-1)*nsampleeachclass+j) =facedatabase(:,(i-1)*(nsmpaleeachclass)+j*2-1);
end
end
%测试样本
for i=1:nclass
for j = 1:neachtest
Xtest(:,(i-1)*neachtest+j) = facedatabase(:,(i-1)*(nsmpaleeachclass)+j*2);
end
end
%训练样本矩阵转换为heigh*width的矩阵
for i=1:neachtrain*nclass
trainSample{i}=reshape(Xtrain(:,i), height, width);
%B = reshape(A,m,n) returns the m-by-n matrix B whose elements are taken column-wise from A. An error results if A does not have m*n elements
end
%测试样本矩阵转换为heigh*width的矩阵
for i=1:neachtest*nclass
testSample{i}=reshape(Xtest(:,i), height, width);
end
[vec, val] = tdfda(trainSample, nclass); %计算矩阵的离散矩阵
for d=2:2:20
%对训练样本进行降维
for i=1:neachtrain*nclass %训练样本的总数
newTrainSample{i} = trainSample{i}*vec(:,1:d);
newTestSample{i} = testSample{i}*vec(:,1:d);
newXtrain(i,:) = reshape(newTrainSample{i}, 1, height*d);
newXtest(i,:) = reshape(newTestSample{i}, 1, height*d);
end
%识别测试样本
%re_im{d/5} = uint8(newTrainSample{1}*vec(:,1:d)');
tic % toc Measure performance using stopwatch timer
classification=classif(newXtrain', newXtest');
suc(d/2) = success(classification, neachtrain, neachtest);
t(d/2)=toc;
clear newTrainSample newTestSample newXtrain newXtest;
end
t
disp('Recognition rate:');
suc
box
axis([2 20 0.65 1.00]);
set(gca,'Xtick',[2:2:20],'ytick',[0.7:0.05:1.00]);
ylabel('识别率');
xlabel('特征数');
hold on;
plot(2:2:20,suc,'-^')