SlideShare une entreprise Scribd logo
1  sur  10
Télécharger pour lire hors ligne
T. A. Makumburage Index No: 13440481
1 | P a g e
MCS3108 Assignment (Vision): Eigenfaces
Introduction
Eigenfaces refers to an appearance-based approach to face recognition that seeks to capture the
variation in a collection of face images and use this information to encode and compare images of
individual faces in a holistic (as opposed to a parts-based or feature-based) manner. Eigenfaces is the
name given to a set of eigenvectors when they are used in the computer vision problem of human
face recognition. The approach of using Eigenfaces for recognition was developed by Sirovich and
Kirby (1987) and used by Matthew Turk and Alex Pentland in face classification.
Implementation
Below are the steps that I followed for the implementation of the Eigenfaces using Matlab.
1. I prepared following images in exact height and width (200*250), to represent the set of images
used to create our Eigenspace for face recognition
Figure 1: Image collection
T. A. Makumburage Index No: 13440481
2 | P a g e
The first step is to obtain a set S with M face images. In our example M = 25. Each image is transformed
into a vector of size N and placed into the set.
Figure 2: Training image set
T. A. Makumburage Index No: 13440481
3 | P a g e
2. Next step is to obtain the mean image. The following image is the mean image obtained for my
image set.
Figure 4: Mean Image
Figure 3: Normalized training image set
T. A. Makumburage Index No: 13440481
4 | P a g e
3. Then I can find the difference Φ between the input image and the mean image.
4. Next I need to find a set of M orthonormal vectors, un , which best describes the distribution of
the data. The kth
vector, uk, is chosen such that
is a maximum, subject to
5. I obtain the covariance matrix C in the following manner
6. AT
7. Once we have found the eigenvectors, vl, ul
8. Following are the Eigenfaces of my image set (S).
T. A. Makumburage Index No: 13440481
5 | P a g e
Recognition Procedure
1. A new face is transformed into its Eigenfaces components. First I compared my input image with
mean image and multiply their difference with each eigenvector of the L matrix. Each value would
represent a weight and would be saved on a vector Ω.
2. Now determine which face class provides the best description for the input image. This is done by
minimizing the Euclidean distance
3. The input face is consider to belong to a class if εk is bellow an established threshold θε. Then the
face image is considered to be a known face. If the difference is above the given threshold, but bellow
a second threshold, the image can be determined as an unknown face. If the input image is above
these two thresholds, the image is determined NOT to be a face.
Figure 5: Eigenfaces
T. A. Makumburage Index No: 13440481
6 | P a g e
ghgh
Figure 7: Analysis of the input image
Figure 6: Comparison
T. A. Makumburage Index No: 13440481
7 | P a g e
Appendix
The implementation was done using Matlab 7.12.0 version.
Matlab Code
% Face recognition by Santiago Serrano
clear all
close all
clc
% number of images on your training set.
M=25;
%Chosen std and mean.
%It can be any number that it is close to the std and mean of most of the
images.
um=100;
ustd=80;
%read and show images(bmp);
S=[]; %img matrix
figure(1);
for i=1:M
str=strcat(int2str(i),'.bmp'); %concatenates two strings that form
the name of the image
eval('img=imread(str);');
subplot(ceil(sqrt(M)),ceil(sqrt(M)),i)
imshow(img)
if i==3
title('Training set','fontsize',18)
end
drawnow;
[irow icol]=size(img); % get the number of rows (N1) and columns
(N2)
temp=reshape(img',irow*icol,1); %creates a (N1*N2)x1 matrix
S=[S temp]; %X is a N1*N2xM matrix after finishing the sequence
%this is our S
end
%Here we change the mean and std of all images. We normalize all images.
%This is done to reduce the error due to lighting conditions.
for i=1:size(S,2)
temp=double(S(:,i));
m=mean(temp);
st=std(temp);
S(:,i)=(temp-m)*ustd/st+um;
end
%show normalized images
figure(2);
for i=1:M
str=strcat(int2str(i),'.jpg');
img=reshape(S(:,i),icol,irow);
img=img';
eval('imwrite(img,str)');
subplot(ceil(sqrt(M)),ceil(sqrt(M)),i)
imshow(img)
drawnow;
T. A. Makumburage Index No: 13440481
8 | P a g e
if i==3
title('Normalized Training Set','fontsize',18)
end
end
%mean image;
m=mean(S,2); %obtains the mean of each row instead of each column
tmimg=uint8(m); %converts to unsigned 8-bit integer. Values range from 0
to 255
img=reshape(tmimg,icol,irow); %takes the N1*N2x1 vector and creates a
N2xN1 matrix
img=img'; %creates a N1xN2 matrix by transposing the image.
figure(3);
imshow(img);
title('Mean Image','fontsize',18)
% Change image for manipulation
dbx=[]; % A matrix
for i=1:M
temp=double(S(:,i));
dbx=[dbx temp];
end
%Covariance matrix C=A'A, L=AA'
A=dbx';
L=A*A';
% vv are the eigenvector for L
% dd are the eigenvalue for both L=dbx'*dbx and C=dbx*dbx';
[vv dd]=eig(L);
% Sort and eliminate those whose eigenvalue is zero
v=[];
d=[];
for i=1:size(vv,2)
if(dd(i,i)>1e-4)
v=[v vv(:,i)];
d=[d dd(i,i)];
end
end
%sort, will return an ascending sequence
[B index]=sort(d);
ind=zeros(size(index));
dtemp=zeros(size(index));
vtemp=zeros(size(v));
len=length(index);
for i=1:len
dtemp(i)=B(len+1-i);
ind(i)=len+1-index(i);
vtemp(:,ind(i))=v(:,i);
end
d=dtemp;
v=vtemp;
%Normalization of eigenvectors
for i=1:size(v,2) %access each column
kk=v(:,i);
temp=sqrt(sum(kk.^2));
v(:,i)=v(:,i)./temp;
T. A. Makumburage Index No: 13440481
9 | P a g e
end
%Eigenvectors of C matrix
u=[];
for i=1:size(v,2)
temp=sqrt(d(i));
u=[u (dbx*v(:,i))./temp];
end
%Normalization of eigenvectors
for i=1:size(u,2)
kk=u(:,i);
temp=sqrt(sum(kk.^2));
u(:,i)=u(:,i)./temp;
end
% show eigenfaces;
figure(4);
for i=1:size(u,2)
img=reshape(u(:,i),icol,irow);
img=img';
img=histeq(img,255);
subplot(ceil(sqrt(M)),ceil(sqrt(M)),i)
imshow(img)
drawnow;
if i==3
title('Eigenfaces','fontsize',18)
end
end
% Find the weight of each face in the training set.
omega = [];
for h=1:size(dbx,2)
WW=[];
for i=1:size(u,2)
t = u(:,i)';
WeightOfImage = dot(t,dbx(:,h)');
WW = [WW; WeightOfImage];
end
omega = [omega WW];
end
% Acquire new image
% Note: the input image must have a bmp or jpg extension.
% It should have the same size as the ones in your training set.
% It should be placed on your desktop
InputImage = input('Please enter the name of the image and its extension
n','s');
InputImage = imread(strcat('C:UsersThiwankaDownloads',InputImage));
figure(5)
subplot(1,2,1)
imshow(InputImage); colormap('gray');title('Input image','fontsize',18)
InImage=reshape(double(InputImage)',irow*icol,1);
temp=InImage;
me=mean(temp);
st=std(temp);
temp=(temp-me)*ustd/st+um;
T. A. Makumburage Index No: 13440481
10 | P a g e
NormImage = temp;
Difference = temp-m;
p = [];
aa=size(u,2);
for i = 1:aa
pare = dot(NormImage,u(:,i));
p = [p; pare];
end
ReshapedImage = m + u(:,1:aa)*p; %m is the mean image, u is the
eigenvector
ReshapedImage = reshape(ReshapedImage,icol,irow);
ReshapedImage = ReshapedImage';
%show the reconstructed image.
subplot(1,2,2)
imagesc(ReshapedImage); colormap('gray');
title('Reconstructed image','fontsize',18)
InImWeight = [];
for i=1:size(u,2)
t = u(:,i)';
WeightOfInputImage = dot(t,Difference');
InImWeight = [InImWeight; WeightOfInputImage];
end
ll = 1:M;
figure(68)
subplot(1,2,1)
stem(ll,InImWeight)
title('Weight of Input Face','fontsize',14)
% Find Euclidean distance
e=[];
for i=1:size(omega,2)
q = omega(:,i);
DiffWeight = InImWeight-q;
mag = norm(DiffWeight);
e = [e mag];
end
kk = 1:size(e,2);
subplot(1,2,2)
stem(kk,e)
title('Eucledian distance of input image','fontsize',14)
MaximumValue=max(e)
MinimumValue=min(e)
Problems Encountered
 At the first instance, all the images were RGB images. Thus there was an errors from reshape
function. So I had to change all the images to grayscale.

Contenu connexe

Tendances

Face recogntion Using PCA Algorithm
Face recogntion Using PCA Algorithm Face recogntion Using PCA Algorithm
Face recogntion Using PCA Algorithm
Ashwini Awatare
 

Tendances (20)

Face Recognition
Face RecognitionFace Recognition
Face Recognition
 
Dip Morphological
Dip MorphologicalDip Morphological
Dip Morphological
 
Machnical Engineering Assignment Help
Machnical Engineering Assignment HelpMachnical Engineering Assignment Help
Machnical Engineering Assignment Help
 
Some Engg. Applications of Matrices and Partial Derivatives
Some Engg. Applications of Matrices and Partial DerivativesSome Engg. Applications of Matrices and Partial Derivatives
Some Engg. Applications of Matrices and Partial Derivatives
 
Digital Image Processing (Lab 09 and 10)
Digital Image Processing (Lab 09 and 10)Digital Image Processing (Lab 09 and 10)
Digital Image Processing (Lab 09 and 10)
 
Support Vector Machine
Support Vector MachineSupport Vector Machine
Support Vector Machine
 
Fuzzy image processing- fuzzy C-mean clustering
Fuzzy image processing- fuzzy C-mean clusteringFuzzy image processing- fuzzy C-mean clustering
Fuzzy image processing- fuzzy C-mean clustering
 
Face recogntion Using PCA Algorithm
Face recogntion Using PCA Algorithm Face recogntion Using PCA Algorithm
Face recogntion Using PCA Algorithm
 
Numerical Method Analysis: Algebraic and Transcendental Equations (Non-Linear)
Numerical Method Analysis: Algebraic and Transcendental Equations (Non-Linear)Numerical Method Analysis: Algebraic and Transcendental Equations (Non-Linear)
Numerical Method Analysis: Algebraic and Transcendental Equations (Non-Linear)
 
Fuzzy dm
Fuzzy dmFuzzy dm
Fuzzy dm
 
Probability Assignment Help
Probability Assignment HelpProbability Assignment Help
Probability Assignment Help
 
numerical methods
numerical methodsnumerical methods
numerical methods
 
Bresenham algorithm
Bresenham algorithmBresenham algorithm
Bresenham algorithm
 
Digital Image Processing (Lab 07)
Digital Image Processing (Lab 07)Digital Image Processing (Lab 07)
Digital Image Processing (Lab 07)
 
Numerical Methods
Numerical MethodsNumerical Methods
Numerical Methods
 
6 logistic regression classification algo
6 logistic regression   classification algo6 logistic regression   classification algo
6 logistic regression classification algo
 
Unit 2.1
Unit 2.1Unit 2.1
Unit 2.1
 
Statistics Assignment Help
Statistics Assignment HelpStatistics Assignment Help
Statistics Assignment Help
 
Computer graphic
Computer graphicComputer graphic
Computer graphic
 
Matlab project
Matlab projectMatlab project
Matlab project
 

En vedette (6)

Distance Education - A summary
Distance Education - A summaryDistance Education - A summary
Distance Education - A summary
 
Mobile Augmented Reality Development tools
Mobile Augmented Reality Development toolsMobile Augmented Reality Development tools
Mobile Augmented Reality Development tools
 
Towards Virtual Rehabilitation - MCS (UCSC) Research Project 2016 - Presentation
Towards Virtual Rehabilitation - MCS (UCSC) Research Project 2016 - PresentationTowards Virtual Rehabilitation - MCS (UCSC) Research Project 2016 - Presentation
Towards Virtual Rehabilitation - MCS (UCSC) Research Project 2016 - Presentation
 
complete Php code for a project .... (hospital management system)
complete Php code for a project .... (hospital management system)complete Php code for a project .... (hospital management system)
complete Php code for a project .... (hospital management system)
 
BIT (UCSC) Final Year Project - Microfinance Loan Management System
BIT (UCSC) Final Year Project - Microfinance Loan Management SystemBIT (UCSC) Final Year Project - Microfinance Loan Management System
BIT (UCSC) Final Year Project - Microfinance Loan Management System
 
Hospital management system (php project) web engineering
Hospital management system (php project) web engineeringHospital management system (php project) web engineering
Hospital management system (php project) web engineering
 

Similaire à Eigenfaces

Ai_Project_report
Ai_Project_reportAi_Project_report
Ai_Project_report
Ravi Gupta
 
please do the extr.pdf
please do the extr.pdfplease do the extr.pdf
please do the extr.pdf
amarnathmahajansport
 
Simple Pendulum Experiment and Automatic Survey Grading using Computer Vision
Simple Pendulum Experiment and Automatic Survey Grading using Computer VisionSimple Pendulum Experiment and Automatic Survey Grading using Computer Vision
Simple Pendulum Experiment and Automatic Survey Grading using Computer Vision
Anish Patel
 
Iaetsd traffic sign recognition for advanced driver
Iaetsd traffic sign recognition for  advanced driverIaetsd traffic sign recognition for  advanced driver
Iaetsd traffic sign recognition for advanced driver
Iaetsd Iaetsd
 
E E 458 Project 002
E E 458 Project 002E E 458 Project 002
E E 458 Project 002
Chad Weiss
 

Similaire à Eigenfaces (20)

matlab.docx
matlab.docxmatlab.docx
matlab.docx
 
Ai_Project_report
Ai_Project_reportAi_Project_report
Ai_Project_report
 
CE344L-200365-Lab2.pdf
CE344L-200365-Lab2.pdfCE344L-200365-Lab2.pdf
CE344L-200365-Lab2.pdf
 
please do the extr.pdf
please do the extr.pdfplease do the extr.pdf
please do the extr.pdf
 
Chapter 1: Linear Regression
Chapter 1: Linear RegressionChapter 1: Linear Regression
Chapter 1: Linear Regression
 
Image Similarity Test Using Eigenface Calculation
Image Similarity Test Using Eigenface CalculationImage Similarity Test Using Eigenface Calculation
Image Similarity Test Using Eigenface Calculation
 
Etienne_ME_a241ms
Etienne_ME_a241msEtienne_ME_a241ms
Etienne_ME_a241ms
 
Simple Pendulum Experiment and Automatic Survey Grading using Computer Vision
Simple Pendulum Experiment and Automatic Survey Grading using Computer VisionSimple Pendulum Experiment and Automatic Survey Grading using Computer Vision
Simple Pendulum Experiment and Automatic Survey Grading using Computer Vision
 
Hand gesture recognition using discrete wavelet transform and hidden Markov m...
Hand gesture recognition using discrete wavelet transform and hidden Markov m...Hand gesture recognition using discrete wavelet transform and hidden Markov m...
Hand gesture recognition using discrete wavelet transform and hidden Markov m...
 
New Approach: Dominant and Additional Features Selection Based on Two Dimensi...
New Approach: Dominant and Additional Features Selection Based on Two Dimensi...New Approach: Dominant and Additional Features Selection Based on Two Dimensi...
New Approach: Dominant and Additional Features Selection Based on Two Dimensi...
 
G1804014348
G1804014348G1804014348
G1804014348
 
maXbox starter65 machinelearning3
maXbox starter65 machinelearning3maXbox starter65 machinelearning3
maXbox starter65 machinelearning3
 
Iaetsd traffic sign recognition for advanced driver
Iaetsd traffic sign recognition for  advanced driverIaetsd traffic sign recognition for  advanced driver
Iaetsd traffic sign recognition for advanced driver
 
Report
ReportReport
Report
 
E E 458 Project 002
E E 458 Project 002E E 458 Project 002
E E 458 Project 002
 
Image Classification
Image ClassificationImage Classification
Image Classification
 
CNN_INTRO.pptx
CNN_INTRO.pptxCNN_INTRO.pptx
CNN_INTRO.pptx
 
A few solvers for portfolio selection
A few solvers for portfolio selectionA few solvers for portfolio selection
A few solvers for portfolio selection
 
Tutorial 2
Tutorial     2Tutorial     2
Tutorial 2
 
Regression
RegressionRegression
Regression
 

Dernier

The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
shinachiaurasa2
 

Dernier (20)

MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdfAzure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
 
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
 
ManageIQ - Sprint 236 Review - Slide Deck
ManageIQ - Sprint 236 Review - Slide DeckManageIQ - Sprint 236 Review - Slide Deck
ManageIQ - Sprint 236 Review - Slide Deck
 
BUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptxBUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptx
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdf
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 

Eigenfaces

  • 1. T. A. Makumburage Index No: 13440481 1 | P a g e MCS3108 Assignment (Vision): Eigenfaces Introduction Eigenfaces refers to an appearance-based approach to face recognition that seeks to capture the variation in a collection of face images and use this information to encode and compare images of individual faces in a holistic (as opposed to a parts-based or feature-based) manner. Eigenfaces is the name given to a set of eigenvectors when they are used in the computer vision problem of human face recognition. The approach of using Eigenfaces for recognition was developed by Sirovich and Kirby (1987) and used by Matthew Turk and Alex Pentland in face classification. Implementation Below are the steps that I followed for the implementation of the Eigenfaces using Matlab. 1. I prepared following images in exact height and width (200*250), to represent the set of images used to create our Eigenspace for face recognition Figure 1: Image collection
  • 2. T. A. Makumburage Index No: 13440481 2 | P a g e The first step is to obtain a set S with M face images. In our example M = 25. Each image is transformed into a vector of size N and placed into the set. Figure 2: Training image set
  • 3. T. A. Makumburage Index No: 13440481 3 | P a g e 2. Next step is to obtain the mean image. The following image is the mean image obtained for my image set. Figure 4: Mean Image Figure 3: Normalized training image set
  • 4. T. A. Makumburage Index No: 13440481 4 | P a g e 3. Then I can find the difference Φ between the input image and the mean image. 4. Next I need to find a set of M orthonormal vectors, un , which best describes the distribution of the data. The kth vector, uk, is chosen such that is a maximum, subject to 5. I obtain the covariance matrix C in the following manner 6. AT 7. Once we have found the eigenvectors, vl, ul 8. Following are the Eigenfaces of my image set (S).
  • 5. T. A. Makumburage Index No: 13440481 5 | P a g e Recognition Procedure 1. A new face is transformed into its Eigenfaces components. First I compared my input image with mean image and multiply their difference with each eigenvector of the L matrix. Each value would represent a weight and would be saved on a vector Ω. 2. Now determine which face class provides the best description for the input image. This is done by minimizing the Euclidean distance 3. The input face is consider to belong to a class if εk is bellow an established threshold θε. Then the face image is considered to be a known face. If the difference is above the given threshold, but bellow a second threshold, the image can be determined as an unknown face. If the input image is above these two thresholds, the image is determined NOT to be a face. Figure 5: Eigenfaces
  • 6. T. A. Makumburage Index No: 13440481 6 | P a g e ghgh Figure 7: Analysis of the input image Figure 6: Comparison
  • 7. T. A. Makumburage Index No: 13440481 7 | P a g e Appendix The implementation was done using Matlab 7.12.0 version. Matlab Code % Face recognition by Santiago Serrano clear all close all clc % number of images on your training set. M=25; %Chosen std and mean. %It can be any number that it is close to the std and mean of most of the images. um=100; ustd=80; %read and show images(bmp); S=[]; %img matrix figure(1); for i=1:M str=strcat(int2str(i),'.bmp'); %concatenates two strings that form the name of the image eval('img=imread(str);'); subplot(ceil(sqrt(M)),ceil(sqrt(M)),i) imshow(img) if i==3 title('Training set','fontsize',18) end drawnow; [irow icol]=size(img); % get the number of rows (N1) and columns (N2) temp=reshape(img',irow*icol,1); %creates a (N1*N2)x1 matrix S=[S temp]; %X is a N1*N2xM matrix after finishing the sequence %this is our S end %Here we change the mean and std of all images. We normalize all images. %This is done to reduce the error due to lighting conditions. for i=1:size(S,2) temp=double(S(:,i)); m=mean(temp); st=std(temp); S(:,i)=(temp-m)*ustd/st+um; end %show normalized images figure(2); for i=1:M str=strcat(int2str(i),'.jpg'); img=reshape(S(:,i),icol,irow); img=img'; eval('imwrite(img,str)'); subplot(ceil(sqrt(M)),ceil(sqrt(M)),i) imshow(img) drawnow;
  • 8. T. A. Makumburage Index No: 13440481 8 | P a g e if i==3 title('Normalized Training Set','fontsize',18) end end %mean image; m=mean(S,2); %obtains the mean of each row instead of each column tmimg=uint8(m); %converts to unsigned 8-bit integer. Values range from 0 to 255 img=reshape(tmimg,icol,irow); %takes the N1*N2x1 vector and creates a N2xN1 matrix img=img'; %creates a N1xN2 matrix by transposing the image. figure(3); imshow(img); title('Mean Image','fontsize',18) % Change image for manipulation dbx=[]; % A matrix for i=1:M temp=double(S(:,i)); dbx=[dbx temp]; end %Covariance matrix C=A'A, L=AA' A=dbx'; L=A*A'; % vv are the eigenvector for L % dd are the eigenvalue for both L=dbx'*dbx and C=dbx*dbx'; [vv dd]=eig(L); % Sort and eliminate those whose eigenvalue is zero v=[]; d=[]; for i=1:size(vv,2) if(dd(i,i)>1e-4) v=[v vv(:,i)]; d=[d dd(i,i)]; end end %sort, will return an ascending sequence [B index]=sort(d); ind=zeros(size(index)); dtemp=zeros(size(index)); vtemp=zeros(size(v)); len=length(index); for i=1:len dtemp(i)=B(len+1-i); ind(i)=len+1-index(i); vtemp(:,ind(i))=v(:,i); end d=dtemp; v=vtemp; %Normalization of eigenvectors for i=1:size(v,2) %access each column kk=v(:,i); temp=sqrt(sum(kk.^2)); v(:,i)=v(:,i)./temp;
  • 9. T. A. Makumburage Index No: 13440481 9 | P a g e end %Eigenvectors of C matrix u=[]; for i=1:size(v,2) temp=sqrt(d(i)); u=[u (dbx*v(:,i))./temp]; end %Normalization of eigenvectors for i=1:size(u,2) kk=u(:,i); temp=sqrt(sum(kk.^2)); u(:,i)=u(:,i)./temp; end % show eigenfaces; figure(4); for i=1:size(u,2) img=reshape(u(:,i),icol,irow); img=img'; img=histeq(img,255); subplot(ceil(sqrt(M)),ceil(sqrt(M)),i) imshow(img) drawnow; if i==3 title('Eigenfaces','fontsize',18) end end % Find the weight of each face in the training set. omega = []; for h=1:size(dbx,2) WW=[]; for i=1:size(u,2) t = u(:,i)'; WeightOfImage = dot(t,dbx(:,h)'); WW = [WW; WeightOfImage]; end omega = [omega WW]; end % Acquire new image % Note: the input image must have a bmp or jpg extension. % It should have the same size as the ones in your training set. % It should be placed on your desktop InputImage = input('Please enter the name of the image and its extension n','s'); InputImage = imread(strcat('C:UsersThiwankaDownloads',InputImage)); figure(5) subplot(1,2,1) imshow(InputImage); colormap('gray');title('Input image','fontsize',18) InImage=reshape(double(InputImage)',irow*icol,1); temp=InImage; me=mean(temp); st=std(temp); temp=(temp-me)*ustd/st+um;
  • 10. T. A. Makumburage Index No: 13440481 10 | P a g e NormImage = temp; Difference = temp-m; p = []; aa=size(u,2); for i = 1:aa pare = dot(NormImage,u(:,i)); p = [p; pare]; end ReshapedImage = m + u(:,1:aa)*p; %m is the mean image, u is the eigenvector ReshapedImage = reshape(ReshapedImage,icol,irow); ReshapedImage = ReshapedImage'; %show the reconstructed image. subplot(1,2,2) imagesc(ReshapedImage); colormap('gray'); title('Reconstructed image','fontsize',18) InImWeight = []; for i=1:size(u,2) t = u(:,i)'; WeightOfInputImage = dot(t,Difference'); InImWeight = [InImWeight; WeightOfInputImage]; end ll = 1:M; figure(68) subplot(1,2,1) stem(ll,InImWeight) title('Weight of Input Face','fontsize',14) % Find Euclidean distance e=[]; for i=1:size(omega,2) q = omega(:,i); DiffWeight = InImWeight-q; mag = norm(DiffWeight); e = [e mag]; end kk = 1:size(e,2); subplot(1,2,2) stem(kk,e) title('Eucledian distance of input image','fontsize',14) MaximumValue=max(e) MinimumValue=min(e) Problems Encountered  At the first instance, all the images were RGB images. Thus there was an errors from reshape function. So I had to change all the images to grayscale.