马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?我要加入
x
function recognize = my_pca(path,persons,numface,numtrain,numpc)
path='D:\ORLface\';
persons=2;
numface=5;
numtrain=3;
numpc=15;
rbf_var=0.1;
for i=1:persons
str=num2str(i);
if i<10
str=['0',str];
end
Folder(i,:)=[path,'s',str,'\'];
end
BMPfile =['01.bmp';'02.bmp';'03.bmp'];
for i=1:persons
for j=1:numtrain
img_name=[Folder(i,:),BMPfile(j,:)];
img=double(imread(img_name));
if i==1 & j==1
size_img=size(img);
Dim=size_img(1)*size_img(2);
end
img=reshape(img,Dim,1);
X(:,numtrain*(i-1)+j)=img;
end
end
all_train=persons*numtrain;
eface=zeros(Dim,1);
for i=1:all_train
eface=eface+X(:,i);
end
eface=eface/all_train;
for i=1:all_train
X(:,i)=X(:,i)-eface;
end
[X1,X2]=wavedec2(X,1,'haar');
Xa=appcoef2(X1,X2,'haar',1);
for i=1:all_train,
for j=i:all_train,
Xa(i,j) = exp(-norm(X(:,i)-X(:,j))^2/rbf_var);
Xa(j,i) = Xa(i,j);
end
end
unita=ones(all_train,all_train)/all_train;
Xcal=Xa-unita*Xa-Xa*unita+unita*Xa*unita;
[V,D]=eig(Xa);
D=real(diag(D));
for i=1:all_train,
V(:,i)=V(:,i)/(sqrt(D(i)));
end
Features_Xcal=zeros(all_train,numpc);
Features_Xcal=Xcal*V(:,1:numpc);
numtest=numface-numtrain;
for i=1:persons
for j=(numtrain+1):numface
img_name=[Folder(i,:),BMPfile(j,:)];
img=double(imread(img_name));
img=reshape(img,Dim,1);
Y(:,numtest*(i-1)+(j-numtrain))=img;
end
end
all_test=persons*numtest;
for i=1:all_test
Y(:,i)=Y(:,i)-eface;
end
[Y1,Y2]=wavedec2(Y,1,'haar');
Ya=appcoef2(Y1,Y2,'haar',1);
unitb=ones(all_test,all_train)/all_train;
Ya=zeros(all_test,all_train);
for i=1:all_test,
for j=1:all_train,
Ya(i,j)=exp(-norm(Y(:,i)-X(:,j))^2/rbf_var);
end
end
Ycal=Ya-unitb*Xa-Ya*unita+unitb*Xa*unita;
Features_Ycal=zeros(all_test,numpc);
Features_Ycal=Ycal*V(:,1:numpc);
err=0;
Xclass=zeros(all_train,1);
Yclass=zeros(all_test,1);
Yresult=zeros(all_test,1);
for xc=1:all_train
Xclass(ac)=ceil(ac/numtrain);
end
for yc=1:all_test
Yclass(bc)=ceil(bc/numtest);
end
for i=1:all_test
Dij=zeros(all_train,1);
for j=1:all_train
FD=Feature_Ycal(:,i)-Feature_Xcal(:,j);
Dij(j)=norm(FD);
end
[Min,IND]=sort(Dij);
Yresult(i)=Xclass(IND(1));
if Yresult(i)~=Yclass(i)
err=err+1;
end
end
recognize=['正确识别率:',num2str((1-err/all_test)*100),'%']
请各位帮忙看一下为什么运行不了,急~~谢谢了 |