Eigen Faces
In this notebook, we will learn about Eigenface — a very interesting application of Principal Component Analysis (PCA) for human faces.
What are EigenFaces ?
Eigenfaces are images that can be added to a mean (average) face to create new facial images. We can write this mathematically as,
where,
is a new face.
is the mean or the average face.
is an EigenFace.
are scalar multipliers we can choose to create new faces. They can be positive or negative.
Eigenfaces are calculated by estimating the principal components of the dataset of facial images. They are used for applications like Face Recognition and Facial Landmark Detection.
How to calculate EigenFaces?
To calculate EigenFaces, we need to go through the following steps.
Obtain a facial image dataset.
Align and resize images.
Create a data matrix.
Calculate Mean Vector [Optional].
Calculate Principal Components.
Reshape Eigenvectors to obtain EigenFaces.
Import Libraries
Read Images
The dataset must be processed so that the centre of the eyes are same and each image has same size. In our case we are using pre-processed images from calebA, which do not require alignment and resizing. Here, we read all images in the specified directory using the defined function readImages. The directory contains images that are aligned. The center of the left and the right eyes in all images are the same. We add these images to a list ( or vector ). We also flip the images vertically and add them to the list. Because the mirror image of a valid facial image, we just doubled the size of our dataset and made it symmetric at that same time.
Create Data Matrix
Create a data matrix containing all images as a row vector. Next, we use the function createDataMatrix to assemble the images into a data matrix. Each row of the data matrix is one image. Let’s look into the createDataMatrix function. If all the images in the dataset are of size 100 x 100 and there are 1000 images, we will have a data matrix of size 30k x 1000. So, according to our example, numImages = 1000, sz[0] = 100, sz[1] = 100 and sz[2] = 3
. flatten
returns a copy of the array collapsed into one dimension.
Generate New Face
The averageFace is calculated below in the main function. We add the output and the weighted eigen faces to generate different results. The weight parameter is acquired from the trackbar position. We use the logic, weight = sliderValues[i] - MAX_SLIDER_VALUE/2
as OpenCV does not allow slider values to be negative. Finally we resize the image to double of its original size.
OpenCV Documentation
Reset Sliders
This is the callback function for mouse hover on the Average
named window. By doing so, we reset the sliders and at the same time reset the output to its preveous state.
OpenCV Documentation
Main Function
This is the main function. As mentioned earlier, the workflow starts with creation of data matrix, then Principal Component Analysis, followed by reshaping of eigen vectors to obtain eigen faces. OpenCV has built-in function for PCA calculation, PCACompute
.
Function Syntax
Parameters:
data
: The data matrix containing every data point as either a row or a column vector. If our data consists of 1000 images, and each image is a 30k long row vector, the data matrix will of size 30k x 1000.mean
: The average of the data. If every data point in the data matrix is a 30k long row vector, the mean will also be a vector of the same size. This parameter is optional and is calculated internally if it is not supplied.maxComponents
: The maximum number of principal components is usually the smaller of the two values 1) Dimensionality of the original data ( in our case it is 30k ) 2) The number of data points ( e.g. 1000 in the above example ). However, we can explicity fix the maximum number of components we want to calculate by setting this argument. For example, we may be interested in only the first 50 principal components. Calculating fewer principal components is cheaper than calculating the theoretical max.
Flowchart
