Skip to content
Olya Andreeva edited this page Mar 14, 2014 · 3 revisions

Description

The program implements the Eigenface algorithm. The details of the algorithm can be found in M. Turk and A. Pentland's paper "Eigenfaces for recognition". The program consists of three modules - config, eigen, matrix - and main.cpp file.

config

This module allows the user to modify some of parameters of the algorithm and to specify the training data.

matrix

This module implements a simple 2D matrix. The following methods are available:

  • output the matrix to the standard output
  • transposition
  • fetching a row as a new matrix
  • multiplication of matrices

Examples of usage:

// Matrix stores its elements in a 2D double vector
std::vector<std::vector<double>> array (5, std::vector<double>(6, 1));
Matrix A = Matrix(5, 6, array); // Create a 5x6 matrix.
Matrix B = A.transpose();
std::cout << B.rows << " " << B.columns; // >> 6 5
B = B*B;
B.array[5][4] = 10; // Access the element in the 5th row and the 4th column.
Matrix C = B.getRow(0); // C is a 1xB.columns matrix.
C.print();

eigen

This module implements method eigensystem that returns eigenvalues and eigenvectors of the symmetric matrix. It uses Jacobi eigenvalue method. The signature of the method is as follows:

std::pair<std::vector<double>, Matrix> eigensystem(Matrix *m);

An example of its usage:

std::pair<std::vector<double>, Matrix> es = eigensystem(&matrix);
// The first member of the pair is a vector of eigenvalues sorted in the descendent order.
std::vector<double> eigenvalues = es.first;
// The second member of the pair is a matrix, which columns are eigenvectors
// s.t. the ith column corresponds to the ith eigenvalue in es.first.
Matrix eigenvectors = es.second;

main.cpp

The main logic of the algorithm is in main() function. The file also contains helper methods:

  • std::vector<double> read_pgm(std::ifstream& file): reads PGM file from the input file and returns a vector of pixel values.
  • void write_pgm(std::string file, Matrix *image): writes PGM file represented by a matrix (must be 1xM) to the output file.
  • std::vector< std::vector<double> > read_training_data(): reads the training data from the folder, specified in DataPath and returns the vector, s.t. vector[i][j] is the jth value of the ith normalized image.
  • Matrix scale(Matrix m, double min = 0, double max = 255): scales the elements of a matrix to the new range, used when outputting the matrices to the PGM format.
  • int recognize(Matrix X, Matrix B, Matrix U, Matrix W): recognizes the photo, returns the number of a matched person in the training set.

Clone this wiki locally