1. Resizing images
First of all, we should import all the images from the testing and training in order to build the database by using the BuildImagesDB() function. The BuildImagesDB() function takes five parameters, but I have changed it to take seven parameters.
This is an example of run BuildImagesDB(), and it explains to us how to import images from training datasets.
In the previous example, I have added two new inputs: ‘M’ and ‘N’. The ‘M’ input has a value of 40, and the ‘N’ input has a value of 30. These inputs can help us reach the results in an expeditious manner, and we do not need to change the BuildImagesDB() function when we want to resize to 80 x 60.
I have written the script to resize all of the images for the training and testing as well as into 40 x 30 and another resized to 80 x 60.
Here is an example of running these functions:
2. Generating K1 and K2 Eigfaces
We now need to determine K1 and K2 by using the following formulas:
We can do this by using BuildFacesModel(), which takes two parameters. The first is the training datasets and other is the threshold that is used to choose the best K eigenfaces. The threshold will determine the required information preservation; for instance, 0.85 would mean the selected eigenfaces preserve 85% of the datasets in the training.
For our system, we will build four face models, two images using 30 x 40 pixels and two using 60 x 80 pixels. We will have a threshold of 85%, and it will be called K1. The other two are different in threshold value, which will be 95% and will be called K2. The following are the commands to build them:
Now, we will see how many eigenfaces have been selected.
We can now use the computeEigenfaces() function to compute eigenfaces for training datasets. The computeEigenfaces() function will return eigenfaces ordered from largest to smallest, and we can run it by using this command in MATLAB.
40 x 30 pixels 80 x 60 pixels
Now, we can show the top five eigenfaces, and we can do this by using the plotEigenfaces() function. I have modified this function to take three arguments. The first is the eigenfaces, and the second is the column. The third is the size of the images. Then, we can run this function for each model that we have built, but I will show an example of run. This function is for the first face model, which is K1_40 x 30.
Here are the top five eigenfaces with the mean face.
3. Recognising all images for the test datasets
Table 1: Results for the test dataset (K1)
1-NN 3-NN 5-NN
40 x 30 size 90% 90% 90%
80 x 60 size 90% 90% 90%
Average 90% 90% 90%
Table 2: Results for the test dataset (K2)
1-NN 3-NN 5-NN
40 x 30 size 90% 90% 90%
80 x 60 size 85% 90% 85%
Average 95% 90% 100%
The facial expression and similar lighting conditions could have played a role here in not recognizing the correct face. We can notice that the system found a different image to be the major first three images. In fact, the wrong images dominated the first three closest faces.
As seen in the figure above, there are similarities between Mohammed and Nawaf (surprised and sleepy). Lighting may have played a significant role in the degree of similarity between them because the existing lighting is similar.
4. Recognition of My Own Face
I modified the function recognizeFacesKnn() and renamed it to getClosestFaces(). The modified function basically takes a face image and an int K—among other arguments—and then finds a K number of closest faces to the given one. Then, the function plotDBImages() is used to plot the found closest faces.
We now want to run the function for my first photo, and we can run it by using this command in MATLAB.
Figure 1: First test
Figure 2: Second test
We can see from Figure 2 the system has detected my three face images to be the nearest. Also, we can see the result is the same for other subjects’ face images, which points out the accuracy of the building faces model. As a result, the system matched my face images with the correct labels when testing each face model that we built.
5. In conclusion
Eigenface is a great technology for recognising faces. Also, we can see from the result of this assignment how useful and powerful using eigenface can be in our lives. Moreover, building face models helps us to find face images with the same label. However, this technique can recognise images with similar lighting conditions and facial expressions, which mean some images with poor lighting may not be recognised in the system. Finally, eigenface is a useful technology that needs to be taught for many reasons, such as improving security systems.