clear;
select=1;
switch select
case 1
[IMG M N c p NN]=ORL2();%c=40总人数;p=10每人10幅图像;NN=M*N图像大小;A读取图像矩阵
case 2
[IMG M N c p NN]=FERET2();%c=200总人数;p=7每人7幅图像;NN=M*N图像大小;A读取图像矩阵
case 3
[IMG M N c p NN]=Yale2();%c=15总人数;p=11每人11幅图像;NN=M*N图像大小;A读取图像矩阵
case 4
[IMG M N c p NN]=AR2();%c=120总人数;p=26每人26幅图像;NN=M*N图像大小;A读取图像矩阵
case 5
[IMG M N c p NN]=face_db2();
end
ln=5; %训练样本数
train=zeros(1,ln); %存储训练样本位置
test=zeros(1,(p-ln)); %存储测试样本位置
%[train test]=RandSelect(ln,p);%从每人p幅图像中随机抽取ln个训练样本,剩余p-ln幅作测试样本
%选择训练样本
selectsample=0;
switch selectsample
case 0
train=1:ln; %采用前面ln幅图像训练样本
test=ln+1:p; %剩余p-ln幅图像作测试样本
case 1 %随机选择ln幅作训练样本
train=[2];
test=Gettestsample(p,train);
case 2
train=[3,5];
test=Gettestsample(p,train);
case 3
train=[1,3,12,];
test=Gettestsample(p,train);
case 4
train=[5,10,12,18];
test=Gettestsample(p,train);
case 5
train=[3,9,17,25,20];
test=Gettestsample(p,train);
case 6
train=[2,5,8,11,18,19];
test=Gettestsample(p,train);
case 7
train=[2,7,10,15,18,22,23];
test=Gettestsample(p,train);
case 8
train=[4,9,10,18,13,19,21,24];
test=Gettestsample(p,train);
case 9
train=[3,9,11,13,17,19,20,22,23];
test=Gettestsample(p,train);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%ICA%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fprintf('************ICA实验结果***********************\n');
c1=clock;
X1_=zeros(NN,ln*c);
for i=1:c
for j=1:ln
X1_(:,j+(i-1)*ln)=IMG(:,i,train(j));
end
end
%X2_是测试样本集
X2_=zeros(NN,(p-ln)*c);
for i=1:c
for j=1:p-ln
X2_(:,j+(i-1)*(p-ln))=IMG(:,i,test(j));
end
end
%将训练样本中心化
mx=zeros(NN,1);
for i=1:ln*c
mx=mx+X1_(:,i);
end
mx=(1/(ln*c)).*mx;
X1=zeros(NN,ln*c);
for i=1:ln*c
X1(:,i)=X1_(:,i)-mx;
end
%白化及求解混阵
x=X1';
k=70;%选择投影轴数
[P1 R]=prewhitening_1(x,k);%P1为相互正交矩阵,而R代表一对角矩阵
P2=(inv(R)*P1'*x)';
Z=P2'*X1;%球化
ini=randn(k,k);%随即产生初始一个初始权矢量(随机的W,使其维数为Z的行向量个数)
epsilon=0.01;
g=1;
Wd=FastICA(ini,Z,epsilon,g);
W=Wd*P2';
%%%%%%%进行识别%%
feature1=W*X1_; %抽取训练样本特征
feature2=W*X2_; %抽取测试样本特征
c2=clock;
fprintf('特征抽取时间:');etime(c2,c1) %特征抽取时间
wrong=0;
for i=1:c
for j=1:(p-ln)
d=1e6;
a2=feature2(:,j+(i-1)*(p-ln));
for a=1:c
for b=1:ln
a1=feature1(:,b+(a-1)*ln);
d0=a2-a1;
d0=norm(d0);
if d>d0
d=d0;
index=a;
end
end
end
if index~=i;
wrong=wrong+1;
end
end
end
c3=clock;
fprintf('分类时间:');etime(c3,c2) %分类时间
fprintf('总识别时间:');etime(c3,c1) %总识别时间
fprintf('识别率:');r=(c*(p-ln)-wrong)/(c*(p-ln))
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
评论0