⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pca_training.m

📁 It is for Face Recognition
💻 M
字号:
function face_pca_training(nEigenFaceNum,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 = 'test.dat'; % trainingFiles contains the file names of training images
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) = [];

% read each image and store it to one row of V.
%compute the mean face image;
fMeanFace = mean(fTrainMatrix); % equal to mean(fTrainMatrix,1);

%compute the variance matrix
fVar = cov(fTrainMatrix); % 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
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 + -