-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
315 lines (281 loc) · 8.74 KB
/
main.cpp
File metadata and controls
315 lines (281 loc) · 8.74 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <cmath>
#include <vector>
#include "eigen.h"
#include "config.h"
/** Methods to work with the data. **/
/*
Reads pgm file from the input file.
Returns the vector of pixel values.
*/
std::vector<double> read_pgm(std::ifstream& file) {
std::vector<double> values;
std::string line;
getline(file, line); // Skip P2 line
getline(file, line); // Skip width line
getline(file, line); // Skip height line
getline(file, line); // Skip max value line
int val;
while(file >> val)
{
values.push_back(val);
}
return values;
}
/*
Writes pgm file, represented by a matrix (must be 1xM) to the output file.
*/
void write_pgm(std::string file, Matrix *image) {
std::stringstream filename;
filename << file;
std::ofstream image_file(filename.str().c_str());
image_file << "P2" << std::endl << Width << std::endl << Height << std::endl << MaxValue << std::endl;
for (int i = 0; i < M; ++i)
{
int val = image->array[0][i];
if (val < 0)
{
val = 0;
}
image_file << val << " ";
}
image_file.close();
}
/*
Reads the training data from the folder, specified in DataPath.
Samples are assumed to be in s{1}/{2}.pgm, where {1} is the number of a person, {2} is the number of a sample.
For each person only the mean image is calculated.
Returns the vector, s.t. vector[i][j] is the jth value of the ith image (mean image). Vector has N elements, each element has M elements.
*/
std::vector< std::vector<double> > read_training_data() {
std::vector< std::vector<double> > array;
// Iteration over people.
for (int face = 0; face < Faces; ++face)
{
std::vector< std::vector<double> > facearray;
// Iteration over photos.
for (int sample = 0; sample < Samples; ++sample)
{
std::stringstream filename;
filename << DataPath << "s" << face + 1 << "/" << sample + 1 << ".pgm";
std::ifstream image(filename.str().c_str());
if (image.is_open()) {
facearray.push_back(read_pgm(image));
image.close();
} else {
std::cout << "Image was not opened.";
}
}
// Find the mean image.
std::vector<double> mean;
for (int i = 0; i < M; ++i)
{
double sum = 0;
for (int j = 0; j < Samples; ++j)
{
sum += facearray[j][i];
}
mean.push_back(sum/Samples);
}
array.push_back(mean);
}
return array;
}
/****/
/** Methods to work with matrices **/
/*
Scales the elements of a matrix to the new range. Used when outputting the matrices to the PGM format.
Returns the new matrix.
*/
Matrix scale(Matrix m, double min = 0, double max = 255) {
// Find the current minimum and maximum.
double m_min = m.array[0][0];
double m_max = m.array[0][0];
for (int r = 0; r < m.rows; ++r) {
for (int c = 0; c < m.columns; ++c) {
if (m.array[r][c] < m_min) {
m_min = m.array[r][c];
}
if (m.array[r][c] > m_max) {
m_max = m.array[r][c];
}
}
}
double old_range = m_max - m_min;
double new_range = max - min;
// Create a new matrix with scaled elements.
Matrix result;
result.columns = m.columns;
result.rows = m.rows;
for (int r = 0; r < m.rows; ++r) {
std::vector<double> row;
for (int c = 0; c < m.columns; ++c) {
row.push_back((m.array[r][c] - m_min)*new_range/old_range + min);
}
result.array.push_back(row);
}
return result;
}
/****/
/*
Recognizes the photo. See main() for explanation of X, B, U, W.
Returns the number of matched person in the training set.
*/
int recognize(Matrix X, Matrix B, Matrix U, Matrix W) {
/* Subtract the mean image */
for (int c = 0; c < M; ++c)
{
X.array[0][c] -= B.array[0][c];
if (X.array[0][c] < 0)
{
X.array[0][c] = 0;
}
}
/* Find weights */
Matrix Wx = Matrix(Eigenfaces, 1);
for (int r = 0; r < Eigenfaces; ++r)
{
Wx.array[r][0] = (U.getRow(r)*X.transpose()).array[0][0];
}
/* Find the closest face from the trainig set */
double min_distance = 0;
int image_number = 0;
for (int image = 0; image < N; ++image)
{
double distance = 0;
for (int eigenface = 0; eigenface < Eigenfaces; ++eigenface)
{
distance += fabs(W.array[eigenface][image] - Wx.array[eigenface][0]);
}
if (distance < min_distance || image == 0)
{
min_distance = distance;
image_number = image;
}
}
return image_number;
}
int main(int argc, const char * argv[])
{
/*
A contains the images as rows. A is NxM, [A]i,j is the jth pixel value of the ith image.
B contains the mean image. B is 1xM matrix, [B]0,j is the jth pixel value of the mean image.
S is the covariance matrix (each pixel is a random variable). S is NxN, computed as AA^T.
V contains eigenvectors of S as rows. V is NxN.
U contains eigenfaces as rows. U is NxM.
W contains the weights of the eigenfaces in the training images. W is NxN, [W]i,j is the weight of the ith eigenface in the jth image.
X is a 1xM matrix of the image to recognize (normalized).
Wx is a Nx1 matrix, [W]i,0 is the weight of the ith eigenface in the X.
*/
Matrix A = Matrix(N, M, read_training_data());
Matrix B = Matrix(1, M);
/* Find the mean image */
for (int c = 0; c < M; ++c)
{
double sum = 0;
for (int r = 0; r < N; ++r)
{
sum += A.array[r][c];
}
B.array[0][c] = sum/N;
}
/* Output the mean image */
write_pgm("output/meanimage.pgm", &B);
/* Subtract the mean from each image */
for (int r = 0; r < N; ++r)
{
for (int c = 0; c < M; ++c)
{
A.array[r][c] -= B.array[0][c];
if (A.array[r][c] < 0)
{
A.array[r][c] = 0;
}
}
}
/* Output the normalized images */
for (int i = 0; i < N; ++i)
{
Matrix image = A.getRow(i);
std::ostringstream filename;
filename << "output/normalized/" << i << ".pgm";
write_pgm(filename.str(), &image);
}
/* Find the covariance matrix */
Matrix S = A*A.transpose();
/* Find eigenvectors of the covariance matrix */
Matrix V = eigensystem(&S).second.transpose();
/* Find eigenfaces */
Matrix U = Matrix(Eigenfaces, M);
for (int r = 0; r < Eigenfaces; ++r)
{
Matrix eigenface = V.getRow(r)*A;
U.array[r] = eigenface.array[0];
double norm = 0;
for (int i = 0; i < U.columns; i++) {
norm += pow(U.array[r][i], 2);
}
norm = sqrt(norm);
for (int i = 0; i < U.columns; i++) {
U.array[r][i] /= norm;
}
/* Output eigenface */
eigenface = scale(U.getRow(r));
std::ostringstream filename;
filename << "output/eigenfaces/" << r << ".pgm";
write_pgm(filename.str(), &eigenface);
}
/* Find weights */
Matrix W = Matrix(Eigenfaces, N);
for (int r = 0; r < Eigenfaces; ++r)
{
for (int c = 0; c < N; ++c)
{
W.array[r][c] = (U.getRow(r)*A.getRow(c).transpose()).array[0][0];
}
}
/* Perform recognition */
if (argc == 2)
{
/* Classify the image from the arguments */
std::ifstream image(argv[1]);
std::vector< std::vector<double> > array;
if (image.is_open()) {
array.push_back(read_pgm(image));
image.close();
} else {
std::cout << "Error: could not open image specified in the arguments.";
return 0;
}
Matrix X = Matrix(1, M, array);
std::cout << recognize(X, B, U, W) + 1;
return 0;
}
double accuracy = 0;
for (int i = 1; i <= N; ++i)
{
/* Read image */
std::stringstream filename;
filename << DataPath << "s" << i << "/" << SampleName << ".pgm";
std::ifstream image(filename.str().c_str());
std::vector< std::vector<double> > array;
if (image.is_open()) {
array.push_back(read_pgm(image));
image.close();
} else {
std::cout << "Image was not opened.";
}
Matrix X = Matrix(1, M, array);
int image_number = recognize(X, B, U, W);
std::cout << i << ". " << image_number + 1 << std::endl;
if (i == image_number + 1)
{
accuracy = accuracy + 1;
}
}
std::cout << accuracy/N;
return 0;
}