Cognotics Home  
Search Cognotics:
.
Home > OpenCV Resources > Seeing With OpenCV > Part 5 Pages:   Prev   1   2   3   4   5   Next
.
 
Seeing With OpenCV, Part 5: Implementing Eigenface (Continued)
 
This article originally appeared in SERVO Magazine, May 2007. Reprinted by permission of T & L Publications, Inc.
 

The learning phase

Figure 4 shows the learn() function, which implements the learning phase as four steps:

   1.  Load the training data (line 6)
   2.  Do PCA on it to find a subspace (line 16)
   3.  Project the training faces onto the PCA subspace (lines 20-29)
   4.  Save all the training information (line 32)
     
   a.  Eigenvalues
   b.  Eigenvectors
   c.  The average training face image
   d.  Projected faces
   e.  Person ID numbers

The next four subsections analyze each of these steps in detail.

. Figure 4 .
.
Figure 4. (Click for larger view) The learn() function. This function implements the learning phase of Eigenface.
.

Loading face images for training or test

The loadFaceImgArray() function (Figure 5) loads face images and person ID numbers for both the learning and recognition phases.

The face images, assumed here to be all the same size, are stored in the global variable faceImgArr. loadFaceImgArray() returns the number of face images loaded.

The person ID numbers are stored in a CvMat variable, personNumTruthMat. "Truth" here refers to the AI term, "ground truth." It means the values in this variable are the true (correct) values for each face image. During the learning phase, those are the only type of person ID numbers we have. But during the recognition phase, the program will have both ground truth values (specified in the file test.txt) and the output from Eigenface. Having both allows us to evaluate how well Eigenface does under varying conditions.

The cvCreateMat() function, called at line 16, creates the personNumTruthMat variable. This function takes three parameters: the number of rows, the number of columns, and the datatype for the matrix. On a 32-bit operating system, the datatype for a matrix of int values is CV_32SC1. The S here stands for "signed," and C1 indicates that the matrix has one channel. (A matrix can have up to four channels. Multiple channels allow you to add a third dimension to a matrix variable.)

. Figure 5 .
.
Figure 5. (Click for larger view) The loadFaceImgArray() function loads face images and person ID numbers for both the learning and recognition phases.
.

The CvMat datatype is a struct, with the raw data stored in the struct element data. data is defined as a union (the definition is given in the CXCORE documentation), with int data accessed as data.i. Lines 22-23 show one way to access CvMat values - as offsets from the start of the data buffer.

Matrix rows are aligned to start on 4-byte intervals. The in-memory row width, in bytes, is stored in CvMat.step. Since we're using a 4-byte datatype with this matrix (and also, since it has only one row), we can ignore CvMat.step. But, if you create a matrix of, for example, char data, you may need to take the step size into account when you access the data elements.

CONTINUED   1   2   3   4   5   Next

.
.
 
Source Code for this Article
 
Related Resources:

Download OpenCV
Official OpenCV usergroup
OpenCV Wiki

 
Articles in this series:

Part 1: Introduction to OpenCV

Part 2: Finding Faces in Images

Part 3: Follow that Face!

Part 4: Face Recognition With Eigenface

Part 5: Implementing Eigenface
 
.
bottom margin
Home | OpenCV Resources | Seeing With OpenCV