2dpca的matlab代码,人脸识别2dpca的matlab源码

function [eigvectors, eigvalues, meanData, newTrainData, newTestData] = TDPCA(trainData, testData, height, width, numvecs)

%2DPCA        Two Dimensional Principal component analysis

%        Usage:

%        [eigvectors, eigvalues, meanData, newTrainData, newTestData] = TDPCA(trainData, testData, height, width, numvecs)

%

%        trainData: Rows of vectors of training data points

%   testData: Rows of vectors of testing data points

%   height: height of the image matrix

%   width: width of the image matrix

%   numvecs: the needed number of eigenvectors

%

%   meanData: Mean of all the data.

%        newTrainData: The data after projection (mean removed)

%   newTestData: The data after projection (mean removed)

%        eigvectors: Each column of this matrix is a eigenvector of the convariance

%                   matrix defined in 2DPCA

%        eigvalues: Eigenvalues of the convariance matrix

%

%

%   Reference paper: J.Yang,D.Zhang,A.F.Frangi,and J.Yang.Two-dimensional

%                    pca:A new approach to a appearance-based face

%                    represenation and recognition. IEEE Trans.on

%                    PAMI,2004

%   Written by Zhonghua Shen (cnjsnt_s@yahoo.com.cn), 2006.07

% Check arguments

if nargin ~= 5

error('usage: [eigvectors, eigvalues, meanData, newTrainData, newTestData] = TDPCA(trainData, testData, height, width, numvecs)');

end;

[nSam,nFea] = size(trainData);

fprintf(1,'Computing average matrix...\n');

meanDataVector = mean(trainData);

meanData = reshape(meanDataVector,height,width);

fprintf(1,'Calculating matrix differences from avg and 2DPCA covariance matrix L...\n');

L = zeros(width,width);

ddata = zeros(nSam,nFea);

for i = 1:nSam

ddata(i,:) = trainData(i,:)-meanDataVector;

dummyMat = reshape(ddata(i,:),height,width);

L = L + dummyMat'*dummyMat;

end;

L = L/nSam;

L = (L + L')/2;

fprintf(1,'Calculating eigenvectors of L...\n');

[eigvectors,eigvalues] = eig(L);

fprintf(1,'Sorting eigenvectors according to eigenvalues...\n');

[eigvectors,eigvalues] = sortem(eigvectors,eigvalues);

eigvalues = diag(eigvalues);

fprintf(1,'Normalize Vectors to unit length, kill vectors corr. to tiny evalues...\n');

num_good = 0;

for i = 1:size(eigvectors,2)

eigvectors(:,i) = eigvectors(:,i)/norm(eigvectors(:,i));

if eigvalues(i) < 0.00001

% Set the vector to the 0 vector; set the value to 0.

eigvalues(i) = 0;

eigvectors(:,i) = zeros(size(eigvectors,1),1);

else

num_good = num_good + 1;

end;

end;

if (numvecs > num_good)

fprintf(1,'Warning: numvecs is %d; only %d exist.\n',numvecs,num_good);

numvecs = num_good;

end;

eigvectors = eigvectors(:,1:numvecs);

if nargout == 5

fprintf(1,'Feature extraction and calculating new training and testing data...\n');

newTrainData = zeros(nSam,height*numvecs);

for i = 1:nSam

dummyMat = reshape(ddata(i,:),height,width);

newTrainData(i,:) = reshape(dummyMat*eigvectors,1,height*numvecs);

end

%testData

……………………

…………限于本文篇幅 余下代码请从51黑下载附件…………

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值