forked from manar189/ABoB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrainLDA.m
58 lines (44 loc) · 1.31 KB
/
trainLDA.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
load('trainingFaces.mat')
% Number of classes
C = 16;
% Eigenfaces
A = faces - meanAll;
L = A' * A;
[V, S] = eig(L);
sortedV = sortEigenvalues(V, diag(S));
U = A * sortedV(:, end-(N-C-1):end);
% sortedU = sortEigenvalues(U, diag(S));
% majorU = U(:, end-(N-C-1):end);
majorU = U;
% Project mu
% pMu = majorU'*mu;
pMeanAll = majorU'*meanAll;
% Initialize scatter matrices
Sb = zeros(N-C);
Sw = zeros(N-C);
% Iterate over each class, keeping track of the starting index in faces
mu = zeros(w*h, C);
start = 1;
for c = 1:C
className = pad(int2str(c), 2, 'left', '0');
Ni = length(dir("Pictures\LDA\" + className + "\*.jpg"));
% Compute mu for the current class
classFaces = faces(:, start:start+Ni-1);
mu(:, c) = mean(classFaces, 2);
% Project mu
pMu = majorU'*mu(:, c);
% Between class scatter matrix
Sb = Sb + Ni * (pMeanAll - pMu)*(pMeanAll - pMu)';
% Project faces of the current class
pFaces = majorU'*classFaces;
% Within class scatter matrix
Ai = pFaces - pMu;
Sw = Sw + Ai*Ai';
start = start + Ni;
end
% Compute Fisher faces
[fisherU, fisherS] = eig(Sw, Sb);
F = majorU * fisherU(:, 1:c-1);
% Project mean faces for each class onto Fisher space
muW = F'*mu;
save('fisher.mat', 'F', 'muW')