资源简介
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.YangD.ZhangA.F.Frangiand J.Yang.Two-dimensional
% pca:A new approach to a appearance-based face
% represenation and recognition. IEEE Trans.on
% PAMI2004
% 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;
[nSamnFea] = size(trainData);
fprintf(1‘Computing average matrix...\n‘);
meanDataVector = mean(trainData);
meanData = reshape(meanDataVectorheightwidth);
fprintf(1‘Calculating matrix differences from avg and 2DPCA covariance matrix L...\n‘);
L = zeros(widthwidth);
ddata = zeros(nSamnFea);
for i = 1:nSam
ddata(i:) = trainData(i:)-meanDataVector;
dummyMat = reshape(ddata(i:)heightwidth);
L = L + dummyMat‘*dummyMat;
end;
L = L/nSam;
L = (L + L‘)/2;
fprintf(1‘Calculating eigenvectors of L...\n‘);
[eigvectorseigvalues] = eig(L);
fprintf(1‘Sorting eigenvectors according to eigenvalues...\n‘);
[eigvectorseigvalues] = sortem(eigvectorseigvalues);
eigvalues = diag(eigvalues);
fprintf(1‘Normalize Vectors to unit length kill vectors corr. to tiny evalues...\n‘);
num_good = 0;
for i = 1:size(eigvectors2)
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(eigvectors1)1);
else
num_good = num_good + 1;
end;
end;
if (numvecs > num_good)
fprintf(1‘Warning: numvecs is %d; only %d exist.\n‘numvecsnum_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(nSamheight*numvecs);
for i = 1:nSam
dummyMat = reshape(ddata(i:)heightwidth);
newTrainData(i:) = reshape(dummyMat*eigvectors1height*numvecs);
en
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 3312 2006-08-03 10:44 TDPCA.m
文件 0 2009-07-23 01:03 2dpca\MATLAB.txt
文件 3312 2006-08-03 10:44 2dpca\TDPCA.m
目录 0 2009-07-23 01:03 2dpca
----------- --------- ---------- ----- ----
6624 4
- 上一篇:模糊c均值聚类 FCM算法的MATLAB代码
- 下一篇:雷达信号处理Matlab程序
评论
共有 条评论