function [Out]=facerec(Trainset)
Slect=Trainset;
n=1;
M=45;
N=length(Slect);
allsamples=[];%所有训练图像
for i=1:1:15
for j=1:N
filename = sprintf('D:\\zzh\\work\\zzh\\lab\\yalefaces\\%d\\s%d.bmp',i,Slect(j));
a=imread(filename);
% figure(n);
% subplot(221);imshow(a);title('原始图像');
a=a';
%************************直方图均衡*****************
% grayx_num=zeros(1,256);
% for ii=1:100
% for jj=1:100
% grayx_num(1,a(ii,jj)+1)=grayx_num(1,a(ii,jj)+1)+1;
% end
% end
% for ii=1:256 % ??
% grayx_pro(ii)=grayx_num(1,ii)/(100*100); %????
% if ii>1
% grayx_pro(ii)=grayx_pro(ii-1)+grayx_pro(ii); %????
% end
% Table(ii)=uint8(255*grayx_pro(ii)); %???????
% end
% for ii=1:100
% for jj=1:100
% Out(ii,jj)=Table(a(ii,jj)+1);
% end
% end
% Hist=Out;
% a=Out;
%
%
% n=n+1;
%subplot(222);imshow(a');title('直方图均衡后');
% %%%%%%%%%%%%%%%%%2*2 down Simgmple%%%%%%%%%%%%%%
% nn=1;
% mm=1;
% for ii=1:2:100
% nn=1;
% for jj=1:2:100
% yasuo(mm,nn)=a(ii,jj)/4+a(ii,jj+1)/4+a(ii+1,jj)/4+a(ii+1,jj+1)/4;
% nn=nn+1;
% end
% mm=mm+1;
% end
% a=yasuo;
%
%************************************************
% 作二维DCT,
% k1=25;
% k2=25;
% i1=1;
% j1=1;
% BB=zeros(50,50);
% temp=zeros(25,25);
% s=size(Hist);
% for m=1:s(1)
% j1=1;
% if rem(m,50)==0
% for n=1:s(2)
% if rem(n,50)==0
% B((m-50+1):m,(n-50+1):n)=dct2(Hist((m-50+1):m,(n-50+1):n));
% B((m-k1+1):m,(n-50+1):n)=0;
% B((m-50+1):m,(n-k2+1):n)=0;
% temp=[B((m-50+1):m-25,(n-50+1):n-25)];
% BB(i1:i1+24,j1:j1+24)=temp;
% j1=j1+24;
% end%if
% end%for n
% i1=i1+24;
% end%if
% end%for m
% for m=1:s(1)
% if rem(m,50)==0
% for n=1:s(2)
% if rem(n,50)==0
% C((m-50+1):m,(n-50+1):n)=idct2(B((m-50+1):m,(n-50+1):n));
% end
% end
% end
% end
% C=uint8(C);
% figure(1);
% subplot(2,2,2);
% subimage(C');
% subplot(2,2,1);
% subimage(Hist');
%**********************************************************
%%%%%%%%%%
b=a(1:100*100); % b是行矢量 1×N,其中N=10000,提取顺序是先列后行,即从上到下,从左到右
b=double(b);
allsamples=[allsamples; b]; % allsamples 是一个M * N 矩阵,allsamples 中每一行数据代表一张图片,其中M=200
end
end
samplemean=mean(allsamples); % 平均图片
xmean=[];
for i=1:1:M
xmean(i,:)=allsamples(i,:)-samplemean; % xmean是一个M × N矩阵,xmean每一行保存的数据是“每个图片数据-平均图片”
end;
sigma=xmean*xmean'; % M * M 阶矩阵
[v d]=eig(sigma);
d1=diag(d);
[d2 index]=sort(d1); %以升序排序
cols=size(v,2);% 特征向量矩阵的列数
vsort=[];
for i=1:cols
vsort(:,i) = v(:, index(cols-i+1) ); % vsort 是一个M*col(注:col一般等于M)阶矩阵,保存的是按降序排列的特征向量,每一列构成一个特征向量
dsort(i) = d1( index(cols-i+1) ); % dsort 保存的是按降序排列的特征值,是一维行向量
end %完成降序排列
%以下选择x%的能量
dsum = sum(dsort);
dsum_extract = 0;
p = 0;
while( dsum_extract/dsum < 0.90)
p = p + 1;
dsum_extract = sum(dsort(1:p));
end
i=1;
% (训练阶段)计算特征脸形成的坐标系
while (i0)
base(:,i) = dsort(i)^(-1/2) * xmean' * vsort(:,i); % base是N×p阶矩阵,除以dsort(i)^(1/2)是对人脸图像的标准化,详见《基于PCA的人脸识别算法研究》p31
i = i + 1;
end
size(base)
% add by wolfsky 就是下面两行代码,将训练样本对坐标系上进行投影,得到一个 M*p 阶矩阵allcoor
allcoor = allsamples * base;
size(allcoor);
%%%%%%%%%%%%%%%%%%%%%%%%%% LDA %%%%%%%%%%%%%%%%%%%%%%%%%%
% Meanclass=zeros(15,size(allcoor,2));
% for person_num=1:15 % 得到 每一个类内的样本的平均值
% for class_num=1:N
% Meanclass(person_num,:)=Meanclass(person_num,:)+allcoor(N*(person_num-1)+class_num,:);
% end
% end
% Meanclass=Meanclass/N;
%
% j=1;
% Sw=zeros(size(allcoor,2),size(allcoor,2)); % 计算wihin-class matrix Sw
% for person_num=1:15
% for i=1:N
% Sw=Sw+(allcoor(N*(person_num-1)+i,:)-Meanclass(person_num,:))'*(allcoor(N*(person_num-1)+i,:)-Meanclass(person_num,:));
% %Sw=Sw+(allcoor(4*(person_num-1)+i,:)-Meanclass(person_num,:))*(allcoor(4*(person_num-1)+i,:)-Meanclass(person_num,:))';
% j=j+1;
% end
% end
%
% j=1;
% Meansample=mean(allcoor); %求所有样本的平均值
% Sb=zeros(size(allcoor,2),size(allcoor,2)); % 计算between-class matrix Sb
% for person_num=1:15
% Sb=Sb+(Meanclass(person_num,:)-Meansample)'*(Meanclass(person_num,:)-Meansample);
% j=j+1;
% end
%
% S=inv(Sw)*Sb;
% [Vs,Ds]=eig(S);
%
% Wlda=base*Vs;
%
% allcoor = allsamples * Wlda;
% size(allcoor);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% accu = 0;
% accu1 = 0;
% accu2 = 0;
% m=1;
% %%%%%%%%%%%%%%%%%%测试过程%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %%%%%%%%%%%%%%%%%%测试过程%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %%%%%%%%%%%%%%%%%%测试过程%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %%%%%%%%%%%%%%%%%%测试过程%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %%%%%%%%%%%%%%%%%%测试过程%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %%%%%%%%%%%%%%%%%%测试过程%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %%%%%%%%%%%%%%%%%%测试过程%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %%%%%%%%%%%%%%%%%%测试过程%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %%%%%%%%%%%%%%%%%%测试过程%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% for i=1:1:15
% disp('...........')
% for j=1:11%读入测试图像
% %j=11;
% filename = sprintf('D:\\MATLAB702\\work\\K-L\\yalefaces\\%d\\s%d.bmp',i,j);
% %filename = sprintf('D:\MATLAB702\work\test\\s%d\\test%d.pgm',i,j);
% a=imread(filename);
% a=a';
% %%%%%%histogram%%%%%%%%%%%%%
% grayx_num=zeros(1,256);
% for ii=1:100
% for jj=1:100
% grayx_num(1,a(ii,jj)+1)=grayx_num(1,a(ii,jj)+1)+1;
% end
% end
% for ii=1:256 % ??
% grayx_pro(ii)=grayx_num(1,ii)/(100*100); %????
% if ii>1
% grayx_pro(ii)=grayx_pro(ii-1)+grayx_pro(ii); %????
% end
% Table(ii)=uint8(255*grayx_pro(ii)); %???????
% end
% for ii=1:100
% for jj=1:100
% Out(ii,jj)=Table(a(ii,jj)+1);
% end
% end
% Hist=Out;
% a=Out;
%
% % 二值化
% %a=floor(Out/240)*255;
%
% % %%%%%%%%%%%%%%%%%2*2 down Simgmple%%%%%%%%%%%%%%
% nn=1;
% mm=1;
% for ii=1:2:100
% nn=1;
% for jj=1:2:100
% yasuo(mm,nn)=a(ii,jj)/4+a(ii,jj+1)/4+a(ii+1,jj)/4+a(ii+1,jj+1)/4;
% nn=nn+1;
% end
% mm=mm+1;
% end
% a=yasuo;
%
% %********************************
% % 作二维DCT,
% % k1=25;
% % k2=25;
% % i1=1;
% % j1=1;
% % BB=zeros(50,50);
% % temp=zeros(25,25);
% % s=size(Hist);
% % for m=1:s(1)
% % j1=1;
% % if rem(m,50)==0
% % for n=1:s(2)
% % if rem(n,50)==0
% % B((m-50+1):m,(n-50+1):n)=dct2(Hist((m-50+1):m,(n-50+1):n));
% % B((m-k1+1):m,(n-50+1):n)=0;
% % B((m-50+1):m,(n-k2+1):n)=0;
% % temp=[B((m-50+1):m-25,(n-50+1):n-25)];
% % BB(i1:i1+24,j1:j1+24)=temp;
% % j1=j1+24;
% % end%if
% % end%for n
% % i1=i1+24;
% % end%if
% % end%for m
% %********************************
% b=a(1:50*50);
% b=double(b);
% tcoor= b * base; %计算坐标,是1×p阶矩阵
% %******************************** SVM *******************************
%
% Class=zeros(1,20);
% Y=[-ones(1,3) ones(1,3)]';
% for I=1:14
% J=I;
% MSV(1:3,:)=allcoor(1+3*(I-1):1+3*(I-1)+2,:);
% while(15-J~=0)
% MSV(4:6,:)=allcoor(1+3*J:1+3*J+2,:);
% J=J+1;
% [nsv,alpha,bias,T]=svm168(MSV,Y,'linear',5);
% W=MSV'*alpha;
% Output=tcoor*W+bias;
% if(sign(Output)==-1)
% Class(I)=Class(I)+1;
% else
% Class(J)=Class(J)+1;
% end
% end
% end
% [CC,Index]=max(Class);
%
% if Index==i
% accu=accu+1;
% if (j==Slect(1)|j==Slect(2)|j==Slect(3) )
% accu1=accu1+1;
% else
% accu2=accu2+1;
% end
% end;
% %************************** 欧式距离 ******************************
%
% % for k=1:1:M
% % mdist(k)=norm(tcoor-allcoor(k,:));
% % end;
% %
% % %三阶近邻
% % [dist,index2]=sort(mdist);
% % index2=index2-1;
% % class1=floor(index2(1)/N)+1;
% % class2=floor(index2(2)/N)+1;
% % class3=floor(index2(3)/N)+1;
% % if class1~=class2 && class2~=class3
% % class=class1;
% % elseif class1==class2
% % class=class1;
% % elseif class2==class3
% % class=class2;
% % end;
% % if class==i
% % accu=accu+1;
% % if (j==Slect(1)|j==Slect(2)|j==Slect(3) )
% % accu1=accu1+1;
% % else
% % accu2=accu2+1;
% % end
% % end;
% %************************** 欧式距离 ******************************
% end;
% end;
% %accu
% %输出识别率
% accuracy=accu/165;
% accuracy1=accu1/45;
% accuracy2=accu2/120;
% Out=[Slect,accuracy,accuracy1,accuracy2]; %训练集 输出识别率