-
Notifications
You must be signed in to change notification settings - Fork 0
/
learn.m
126 lines (112 loc) · 5 KB
/
learn.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
function [imgMatrix, meanImage, eigenFaces, projectedImages] = learn(database,K)
%LEARN Extracts the features within a give database
% INPUT:
% - database Database path
%
% OUTPUT:
% - imgMatrix Matrix holding the ID number of the subjects
% - meanImage Mean image of all images in the database
% - eigenFaces Matrix holding the eigen faces of the images in
% the DB
% - projectedImages Matrix with the feature vector of each image
%##########################################################################
fprintf("[INFO]: Using " + database + " database.\n");
databaseChar = char(database);
dBPath = [databaseChar,'/yale*'];
people = dir(dBPath);
%##########################################################################
%% X MATRIX
% Iterates over the folders (subjects) within the database, reads the
% images of all the subjects and save the pixels values in columns of the
% xMatrix.
% The subject of each folder is also saved in imgMatrix to be able to map
% each column with the relevant person.
%##########################################################################
fprintf("[INFO]: Obtaining X matrix... ");
Xmatrix = [];
imgMatrix = {};
for f = 1:length(people)
%person = string(people(f).name);
person = people(f).name;
personPath=[databaseChar,'/',people(f).name,'/','*.pgm'];
%images = dir(fullfile(database+"\"+person, "*.pgm"));
images = dir(personPath);
for i = 1:length(images)
imageName = images(i).name;
%image = imread(database+"\"+person+"\"+imageName);
imgPath=[databaseChar,'/',person,'/',imageName];
image = imread(imgPath);
[rows, cols] = size(image);
vector = reshape(image', rows*cols, 1);
Xmatrix = [Xmatrix vector];
imgMatrix = [imgMatrix, imageName]; %Saving filenames in a matrix
end
end
fprintf(repmat('\b complete.\n', 1, 1));
%##########################################################################
%% MEAN IMAGE
% Calculates mean value of each pixel of all images loaded in xMatrix
%##########################################################################
fprintf("[INFO]: Obtaining mean image... ");
meanImage = mean(Xmatrix,2);
fprintf(repmat('\b complete.\n', 1, 1));
%##########################################################################
%% DEVIATION MATRIX
% Iterates over all the columns of xMatrix (each image) and substract the
% value of the mean image for each pixel. The result is saved in
% deviationMatrix.
%##########################################################################
fprintf("[INFO]: Obtaining deviation matrix... ");
samplesNumber = size(Xmatrix,2);
deviationMatrix = [];
for i = 1 : samplesNumber
imgDeviation = double(Xmatrix(:,i)) - meanImage;
deviationMatrix = [deviationMatrix imgDeviation];
end
fprintf(repmat('\b complete.\n', 1, 1));
%##########################################################################
%% EIGENVECTORS
% Calculates the eigenvectors and the diagonal matrix of the surrogate
% (transposed matrix * matrix) of deviationMatrix.
%##########################################################################
fprintf("[INFO]: Obtaining eigenvectors... ");
surrogateCovMatrix = deviationMatrix' * deviationMatrix;
[eigenVectors, diagonalMatrix] = eig(surrogateCovMatrix);
fprintf(repmat('\b complete.\n', 1, 1));
%##########################################################################
%% PRINCIPAL COMPONENT
% Iterates over the diagonal values of diagonalMatrix and stores the
% relevant eigenvectors in principalCompMatrix
%##########################################################################
fprintf("[INFO]: Obtaining principal component matrix... ");
principalCompMatrix = [];
for i = 1 : size(eigenVectors,2)
if(diagonalMatrix(i,i)>1)
principalCompMatrix = [principalCompMatrix eigenVectors(:,i)];
end
end
if K>0
principalCompMatrix=principalCompMatrix(:,1:K);
end
fprintf(repmat('\b complete.\n', 1, 1));
%##########################################################################
%% EIGENFACES
% Calculates the eigenvectors (or eigenfaces) by multipliying
% deviationMatrix by principalCompMatrix
%##########################################################################
fprintf("[INFO]: Obtaining eigenfaces... ");
eigenFaces = deviationMatrix * principalCompMatrix;
fprintf(repmat('\b complete.\n', 1, 1));
%##########################################################################
%% PROJECTED IMAGES
% Calculates the feature vector of each image and stores the result in
% projectedImages
%##########################################################################
fprintf("[INFO]: Obtaining projected images... ");
projectedImages = [];
for i = 1 : samplesNumber
featureVector = eigenFaces' * deviationMatrix(:,i);
projectedImages = [projectedImages featureVector];
end
fprintf(repmat('\b complete.\n', 1, 1));
end