www.pudn.com > GaitRecognition.rar > PCA_2D.m, change:2012-11-04,size:1830b


function [dataAfter2DPCA,eValue,pca_2D_Vector] = PCA_2D(dirOfTrainSet) 
    tic; 
    clc; 
    setPath; 
    cd(dirOfTrainSet); 
    peopleList=dir('.'); 
    %计算图像总数 
    totalNum=(length(peopleList)-2)*4; 
     
    temp=1; 
    for i=3:length(peopleList) 
        cd(peopleList(i).name); 
        ConditionList=dir('.'); 
        for k=1:(length(ConditionList)-2) 
            cd(ConditionList(k+2).name); 
            cd([pwd '\' xAngle]);%进入90°视角的目录 
            GEIpic=dir('*.png'); 
            if (i==3&&k==1) 
                GEIsize=size(imread(GEIpic(1).name)); 
                %待进行PCA的图像序列 
                rawGEIdata=zeros(GEIsize(1),GEIsize(2),totalNum); 
            else 
                rawGEIdata(:,:,temp)=double(imread(GEIpic(1).name)); 
                temp=temp+1; 
            end 
            cd ../..%退出到个人目录 
        end 
        cd .. 
    end 
 
    %计算能量图平均值 
    avgGEI=sum(rawGEIdata,3)/totalNum; 
 
    %构建协方差矩阵 
    covMatrix=zeros(size(rawGEIdata,1)); 
    for i=1:totalNum 
       covMatrix=covMatrix+ (rawGEIdata(:,:,i)-avgGEI) * (rawGEIdata(:,:,i)-avgGEI)'; 
    end 
    Eigenvalues=eig(covMatrix); 
    Eigenvalues=sort(abs(Eigenvalues),'descend');  
    %阈值为累积贡献率的99% 
    threshold=sum(Eigenvalues)*0.999; 
    eigtotalNum=1; 
    while (threshold>=0) 
       threshold=threshold-Eigenvalues(eigtotalNum); 
       eigtotalNum=eigtotalNum+1; 
    end   
    %计算协方差矩阵的特征值和对应的特征向量 
   OPTS.disp=0; 
   [eVector,eValue]=eigs(covMatrix,eigtotalNum-1,'LM',OPTS); 
   pca_2D_Vector=eVector'; 
   %计算经PCA降维的结果 
   dataAfter2DPCA=zeros(size(eValue,1),GEIsize(2),totalNum); 
   for i=1:totalNum 
       dataAfter2DPCA(:,:,i)=pca_2D_Vector*rawGEIdata(:,:,i); 
   end 
   disp('2DPCA time:'); 
   toc; 
   cd(rootPath);