function OutputName = Recognition(TestImage, m, A, Eigenfaces,varargin)
% Recognizing step....
%
% Description: This function compares two faces by projecting the images into facespace and
% measuring the Euclidean distance between them.
%
% Argument: TestImage - Path of the input test image
%
% m - (M*Nx1) Mean of the training
% database, which is output of 'EigenfaceCore' function.
%
% Eigenfaces - (M*Nx(P-1)) Eigen vectors of the
% covariance matrix of the training
% database, which is output of 'EigenfaceCore' function.
%
% A - (M*NxP) Matrix of centered image
% vectors, which is output of 'EigenfaceCore' function.
%
% Returns: OutputName - Name of the recognized image in the training database.
%
% See also: RESHAPE, STRCAT
% Original version by Amir Hossein Omidvarnia, October 2007
% Email: aomidvar@ece.ut.ac.ir
%%%%%%%%%%%%%%%%%%%%%%%% Projecting centered image vectors into facespace
% All centered images are projected into facespace by multiplying in
% Eigenface basis's. Projected vector of each face will be its corresponding
% feature vector.
% varargin
ProjectedImages = [];
Train_Number = size(Eigenfaces,2);
for i = 1 : Train_Number
temp = Eigenfaces'*A(:,i); % Projection of centered images into facespace
ProjectedImages = [ProjectedImages temp];
end
%%%%%%%%%%%%%%%%%%%%%%%% Extracting the PCA features from test image
InputImage = imread(TestImage);
if isempty(varargin)~=1
if fix(varargin{1}/10)==1
%% 11
x = InputImage;
if (size(x,3)>1)%if RGB image make gray scale
try
x=rgb2gray(x);%image toolbox dependent
catch
x=sum(double(x),3)/3;%if no image toolbox do simple sum
end
end
x=double(x);%make sure the input is double format
[output,count,mm,svec]=facefind(x,32,inf,5,3,1);%full scan
b=[output(1)-(output(2)-output(1))*0.3 ...
output(2)+(output(2)-output(1))*0.3...
output(3)-(output(4)-output(3))*0.6...
output(4)+(output(4)-output(3))*0.3];
syms x0 y0 r x y
F=(x-x0)^2+(y-y0)^2-r^2;
g=solve(subs(F,[x,y],[(b(2)+b(1))/2 b(4)]),...
subs(F,[x,y],[b(1) b(3)-(b(3)-b(4))*3/4]),...
subs(F,[x,y],[b(2) b(3)-(b(3)-b(4))*3/4]));
im1=InputImage;
im1(1 :b(3),: ,:)=0;
im1(b(4):end ,: ,:)=0;
im1(: ,1 :b(1) ,:)=0;
im1(: ,b(2):end ,:)=0;
InputImage=im1;
if varargin{1}-10==3
InputImage=InputImage;
end
imshow(InputImage)
elseif fix(varargin{1}/10)==2
%% 2222
222
if varargin{1}-20==3
InputImage=InputImage;
end
imshow(InputImage)
elseif fix(varargin{1}/10)==3
%% 333
333
x = InputImage;
if (size(x,3)>1)%if RGB image make gray scale
try
x=rgb2gray(x);%image toolbox dependent
catch
x=sum(double(x),3)/3;%if no image toolbox do simple sum
end
end
x=double(x);%make sure the input is double format
[output,count,mm,svec]=facefind(x,32,inf,5,5,1);%full scan
b=[output(1)-(output(2)-output(1))*0.3 ...
output(2)+(output(2)-output(1))*0.3...
output(3)-(output(4)-output(3))*0.6...
output(4)+(output(4)-output(3))*0.3];
syms x0 y0 r x y
F=(x-x0)^2+(y-y0)^2-r^2;
g=solve(subs(F,[x,y],[(b(2)+b(1))/2 b(4)]),...
subs(F,[x,y],[b(1) b(3)-(b(3)-b(4))*3/4]),...
subs(F,[x,y],[b(2) b(3)-(b(3)-b(4))*3/4]));
im1=InputImage;
im1(1 :b(3),: ,:)=0;
im1(b(4):end ,: ,:)=0;
im1(: ,1 :b(1) ,:)=0;
im1(: ,b(2):end ,:)=0;
InputImage=imresize(imcrop(im1,[b(1) b(3) b(2)-b(1) b(4)-b(3)]),[240 320]);
% if varargin{1}-30==1
% InputImage=InputImage;
% elseif varargin{1}-30==2
% InputImage=InputImage;
% elseif varargin{1}-30==3
%
% end
imshow(InputImage)
end
end
temp = InputImage(:,:,1);
[irow icol] = size(temp);
InImage = reshape(temp',irow*icol,1);
size(InImage)
size(m)
Difference = double(InImage)-m; % Centered test image
ProjectedTestImage = Eigenfaces'*Difference; % Test image feature vector
%%%%%%%%%%%%%%%%%%%%%%%% Calculating Euclidean distances
% Euclidean distances between the projected test image and the projection
% of all centered training images are calculated. Test image is
% supposed to have minimum distance with its corresponding image in the
% training database.
Euc_dist = [];
for i = 1 : Train_Number
q = ProjectedImages(:,i);
temp = ( norm( ProjectedTestImage - q ) )^2;
Euc_dist = [Euc_dist temp];
end
[Euc_dist_min , Recognized_index] = min(Euc_dist);
OutputName = strcat(int2str(Recognized_index),'.jpg');