%--------------------------------------------------------------------
clear;
clc;
chos=0;
possibility=7;
messaggio='Insert the number of set: each set determins a class. This set should include a number of images for each person, with some variations in expression and in the lighting.';
while chos~=possibility,
chos=menu('Face Recognition System','Select image','Add selected image to database','Database Info','Face Recognition','Delete Database',...
'Visualization tool','Exit');
%----------------
if chos==1,
clc;
[namefile,pathname]=uigetfile('*.*','Select image');
if namefile~=0
[img,map]=imread(strcat(pathname,namefile));
imshow(img);
dimensioni = size(img);
disp('Input image has been selected.');
disp('Now press on "Add selected image to database" button to add this image to database or,');
disp('press on "Face Recognition" button to start face matching.');
else
warndlg('Input image must be selected.',' Warning ')
end
end
%----------------
if chos==2,
clc;
if exist('img')
if (exist('face_database.dat')==2)
load('face_database.dat','-mat');
face_number=face_number+1;
data{face_number,1}=img(:);
prompt={strcat(messaggio,'Class number must be a positive integer <= ',num2str(max_class))};
mytitle='Class number';
lines=1;
def={'1'};
answer=inputdlg(prompt,mytitle,lines,def);
zparameter=double(str2num(char(answer)));
if size(zparameter,1)~=0
class_number=zparameter(1);
if (class_number<=0)||(class_number>max_class)||(floor(class_number)~=class_number)||(~isa(class_number,'double'))||(any(any(imag(class_number))))
warndlg(strcat('Class number must be a positive integer <= ',num2str(max_class)),' Warning ')
else
if class_number==max_class;
max_class=class_number+1;
end
data{face_number,2}=class_number;
save('face_database.dat','data','face_number','max_class','-append');
msgbox(strcat('Database already exists: image succesfully added to class number ',num2str(class_number)),'Database result','help');
close all;
clear('img')
end
else
warndlg(strcat('Class number must be a positive integer <= ',num2str(max_class)),' Warning ')
end
else
face_number=1;
max_class=1;
data{face_number,1}=img(:);
prompt={strcat(messaggio,'Class number must be a positive integer <= ',num2str(max_class))};
mytitle='Class number';
lines=1;
def={'1'};
answer=inputdlg(prompt,mytitle,lines,def);
zparameter=double(str2num(char(answer)));
if size(zparameter,1)~=0
class_number=zparameter(1);
if (class_number<=0)||(class_number>max_class)||(floor(class_number)~=class_number)||(~isa(class_number,'double'))||(any(any(imag(class_number))))
warndlg(strcat('Class number must be a positive integer <= ',num2str(max_class)),' Warning ')
else
max_class=2;
data{face_number,2}=class_number;
save('face_database.dat','data','face_number','max_class','dimensioni');
msgbox(strcat('Database was empty. Database has just been created. Image succesfully added to class number ',num2str(class_number)),'Database result','help');
close all;
clear('img')
end
else
warndlg(strcat('Class number must be a positive integer <= ',num2str(max_class)),' Warning ')
end
end
else
errordlg('No image has been selected.','File Error');
end
end
%----------------
if chos==3,
clc;
close all;
clear('img');
if (exist('face_database.dat')==2)
load('face_database.dat','-mat');
msgbox(strcat('Database has ',num2str(face_number),' image(s). There are',num2str(max_class-1),' class(es). Input images must have the same size.'),'Database result','help');
else
msgbox('Database is empty.','Database result','help');
end
end
%----------------
if chos==4,
clc;
close all;
if exist('img')
ingresso=double(img(:));
if (exist('face_database.dat')==2)
load('face_database.dat','-mat');
% face_number is equal to "M" of Turk's paper
% i.e. the number of faces present in the database.
% These image are grouped into classes. Every class (or set) should include
% a number of images for each person, with some variations in expression and in the
% lighting.
matrice=zeros(size(data{1,1},1),face_number);
for ii=1:face_number
matrice(:,ii)=double(data{ii,1});
end
somma=sum(matrice,2);
media=somma/face_number;
for ii=1:face_number
matrice(:,ii)=matrice(:,ii)-media;
end
matrice=matrice/sqrt(face_number);
% up to now matrix "matrice" is matrix "A" of Turk's paper
elle=matrice'*matrice;
% matrix "elle" is matrix "L" of Turk's paper
% eigenvalues and eigenvectors of the "reduced" matrix A'*A
[V,D] = eig(elle);
% the following multiplication is performed to obtain the
% eigenvectors of the original matrix A*A' (see Turk's paper)
% See also Karhunen-Loeve algorithm, for face recognition
if det(D)~=0
Vtrue=matrice*V*(abs(D))^-0.5;
else
Vtrue=matrice*V;
end
%Vtrue=matrice*V;
Dtrue=diag(D);
% the eigenvalues are sorted by order and only M' of them
% are taken. We impose M' equal to the number of classes
% (max_class-1)
[Dtrue,ordine]=sort(Dtrue);
Dtrue=flipud(Dtrue);
ordine=flipud(ordine);
Vtrue(:,1:face_number)=Vtrue(:,ordine);
Vtrue=Vtrue(:,1:max_class-1);
Dtrue=Dtrue(1:max_class-1);
% we calculate the eigenface components of
% the normalized input (mean-adjusted). I.e. the input
% image is projected into "face-space"
pesi=Vtrue'*(ingresso-media);
pesi_database = zeros(max_class-1,max_class-1);
pesi_database_mediati = zeros(max_class-1,max_class-1);
numero_elementi_classe=zeros(max_class-1,1);
for ii=1:face_number
ingresso_database=double(data{ii,1});
classe_database=data{ii,2};
pesi_correnti=Vtrue'*(ingresso_database-media);
pesi_database(:,classe_database)