📄 pca_training_eye.m
字号:
function [fMeanFace, fEigenFaces]=pca_training_eye(nEigenFaceNum, nSample, sTrainListFile)
% face_pca_training.m
% this program performs principal component analysis training on face images
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%% Setting Section %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
nDefEigenFaceNum = 16; % number of eigenfaces we need.
% dimesion of images: nH---image height, nW---image width
nH = 24; % resizing face image height
nW = 30; % resizing face image width
sDefTrainListFile = 'EyeTrain.dat'; % trainingFiles contains the file names of training images
nSample = 200;
nIsShowRes = 1; % a switch to show or not to show results
%%%%%%%%%%%%%%%%%%%%%%%% End of Setting Section %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
switch nargin
case 0
nEigenFaceNum = nDefEigenFaceNum; % use default directory
sTrainListFile = sDefTrainListFile; % use default directory
case 1
sTrainListFile = sDefTrainListFile; % use default directory
case 2
otherwise
if ( nargin ~= 3 )
disp('ERROR: There are too many input arguments !');
return;
end
end
nVisualDim = nH * nW;
fTrainMatrix = load(sTrainListFile, '-ASCII');
fTrainMatrix(:,end) = [];
nSample = 200;
eyeData = fTrainMatrix(1:nSample,1:nW*nH);
% read each image and store it to one row of V.
%compute the mean face image;
fMeanFace = mean(eyeData); % equal to mean(fTrainMatrix,1);
%compute the variance matrix
fVar = cov(eyeData); % this will consider mean by itself, regardless of mean obtained before
% compute the eigenvectors and eigenvalues (each row represent an eigenface vector)
[fEigenFaces, fEigenFaceValue] = eigs(fVar, nEigenFaceNum);
fEigenFaceValue = diag(fEigenFaceValue); % transform eigenvalues to one vector
% the same as: [fEigenFaces,SCORE,fEigenFaceValue,tsquare] = princomp(fTrainMatrix)
% (Below block can be disabled] : display mean face and then display eigenfaces
save meanface.dat fMeanFace;
save eigentface.dat fEigenFaceValue;
save eigenfacevalue.dat fEigenFaceValue;
% (Below block can be disabled] : display mean face and then display eigenfaces
if nIsShowRes==1
nTestImg = reshape(fMeanFace,nH,nW);
figure, imshow(nTestImg,[]);
figure;
for j=1:2
for i=1:8
k = (j-1)*8+i;
for i1=1:nH
nTestImg(:) = fEigenFaces(:,k);
end
subplot(2, 8, k);
imshow(nTestImg, []);
end
end
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -