-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPCA.m
43 lines (29 loc) · 921 Bytes
/
PCA.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
function PCA()
% To generate eigenfaces by using ORLTrain.mat
% Input Parameter: none
%load the data matrix
load testData.mat
dTrainPCA = double(data);
%-------------------------------------------------
% PCA Training
%-------------------------------------------------
B = dTrainPCA;
num_faces = size(B,2);
% Average face
avgfaceT = ((sum(B') / num_faces))';
% Subtract average face from each individual face
for row = 1:num_faces
B(:, row) = B(:, row) - avgfaceT;
end
% Covariance Matrix
st = B*B';
% Eigenvalue Decomposition
[Vt, Dt]=eig(st);
% Resort the eigenvalues and eigenvectors (EigenFaces) in descending order
[junk, index] = sort(-diag(Dt));
eigenfacesT = Vt(:, index);
d = diag(Dt);
eigenvaluesT = d(index);
%----------------------------------------------------
% this is to save into directory folder "gray test"
save([pwd,'\gray test\ORLtest.mat'],'eigenfacesT','eigenvaluesT','avgfaceT')