Seeing With OpenCV, Part 5: Implementing Eigenface
by Robin Hewitt
This article originally appeared in
SERVO Magazine, May 2007.
Reprinted by permission of T & L Publications, Inc.
Last month's article
explained how the face recognition method called eigenface works. This month's article concludes both the OpenCV series and the eigenface topic with a detailed look at a complete program for implementing eigenface with OpenCV.
A Brief Review
Here's a brief recap of last month's article explaining how eigenface works.
Eigenface consists of two phases: learning and recognition. In the learning phase, you give eigenface one or more face images for each person you want it to recognize. These images are called the training images. In the recognition phase, when you give eigenface a face image, it responds by telling you which training image is "closest" to the new face image.
Eigenface uses the training images to "learn" a face model. This face model is created by applying a method called Principal Components Analysis (PCA) to reduce the "dimensionality" of these images. Eigenface defines image dimensionality as the number of pixels in an image.
The lower dimensionality representation that eigenface finds during the learning phase is called a subspace. In the recognition phase, it reduces the dimensionality of the input image by
"projecting" it onto the subspace it found during learning. "Projecting onto a subspace" means finding the closest point in that subspace. After the unknown face image has been projected, eigenface calculates the distance between it and each training image. Its output is a pointer to the closest training image. You can then look up which person that was to see whom eigenface has identified.
Setting up for Eigenface
In use, you'll probably want to combine eigenface with the face detection method presented in
of this series. To simplify the example code for this article, however, I'll be assuming you already have a set of training images and a set of test images. These face images must all be exactly the same size.
For the examples in this article, I've used a free, publicly available face database, the Olivetti Research Lab's (ORL) Face Database, which you can download
To set up for using eigenface, unzip the ORL database in the same directory you'll use to run eigenface. This database contains ten face images for each of 40 subjects. These are organized into 40 directories, named s1 s40. Each directory contains ten images, named 1.pgm 10.pgm. All the ORL images are already the same size - 92x112 pixels.
You'll also need two input files: train.txt and test.txt.
shows an example of these input files. Both files use the same format: person number, whitespace, path to image file.
Figure 1. The two input files for the Eigenface program: train.txt and test.txt. The paths in these input files point to images in the ORL face database.
You may have noticed that the first three test images are the same as the training images. These are useful test cases, because eigenface should always give the right answer when you ask it to recognize one of its training images. If it doesn't, you know you have some debugging to do!
To run the learning phase of this eigenface program, enter
at the command prompt. To run the recognition phase, enter