Sei sulla pagina 1di 5

%%%% By Kalyan Sourav Dash %%%%

function [m,eigenfaces,projectimg]=train()

% In this part of function, we align a set of face images (the training set
x1, x2, ... , xM )
%
% This means we reshape all 2D images of the training database
% into 1D column vectors. Then, it puts these 1D column vectors in a row to
% construct 2D matrix 'X'.
%
%
% datapath - path of the data images used for training
% X - A 2D matrix, containing all 1D image vectors.
% Suppose all P images in the training
database
% have the same size of MxN. So the
length of 1D
% column vectors is MxN and 'X' will be
a (MxN)xP 2D matrix.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%

%%%%%%%%% finding number of training images in the data path specified as


argument %%%%%%%%%%
X = zeros(19200,0);
count=0;
for i=1:12
cd 'C:\Program Files\MATLAB\R2018b\test_set';
cd(num2str(i));
imagefiles = dir('*.jpg');
imgcount = length(imagefiles);
%%%%%%%%%%%%%%%%%%%%%%%%%% creating the image matrix X %%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%

for j=1:imgcount
str = strcat('C:\Program
Files\MATLAB\R2018b\test_set\',num2str(i),'\',imagefiles(j).name);
img = imread(str);
img = rgb2gray(img);
[r c] = size(img);
temp = reshape(img',r*c,1); %% Reshaping 2D images into 1D image vectors
%disp(size(temp));
disp(length(temp));
%% here img' is used because reshape(A,M,N)
function reads the matrix A columnwise
if (length(temp)<=19200);
count=count+1; %% where as an image matrix is constructed with
first N pixels as first row,next N in second row so on
X = [X temp]; %% X,the image matrix with columnsgetting
added for each image
end
%disp(size(X));
end
cd ..
end
disp(count);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Now we calculate m, A and eigenfaces.The descriptions are below :
%
% m - (MxN)x1 Mean of the training images
% A - (MxN)xP Matrix of image vectors after each vector
getting subtracted from the mean vector m
% eigenfaces - (MxN)xP' P' Eigenvectors of Covariance matrix (C)
of training database X
% where P' is the number of eigenvalues of
C that best represent the feature set
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%% calculating mean image vector %%%%%

m = mean(X,2); % Computing the average face image m = (1/P)*sum(Xj's) (j =


1 : P)
imgcount = size(X,2);

%%%%%%%% calculating A matrix, i.e. after subtraction of all image vectors


from the mean image vector %%%%%%

A = [];
for i=1 : 167
temp = double(X(:,i)) - m;
A = [A temp];
end
disp(size(A));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% CALCULATION OF EIGENFACES %%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% we know that for a MxN matrix, the maximum number of non-zero eigenvalues
that its covariance matrix can have
%%% is min[M-1,N-1]. As the number of dimensions (pixels) of each image
vector is very high compared to number of
%%% test images here, so number of non-zero eigenvalues of C will be maximum
P-1 (P being the number of test images)
%%% if we calculate eigenvalues & eigenvectors of C = A*A' , then it will be
very time consuming as well as memory.
%%% so we calculate eigenvalues & eigenvectors of L = A'*A , whose
eigenvectors will be linearly related to eigenvectors of C.
%%% these eigenvectors being calculated from non-zero eigenvalues of C, will
represent the best feature sets.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

L= A' * A;
[V,D]=eig(L); %% V : eigenvector matrix D : eigenvalue matrix
disp(size(V,2));
%%%% again we use Kaiser's rule here to find how many Principal Components
(eigenvectors) to be taken
%%%% if corresponding eigenvalue is greater than 1, then the eigenvector will
be chosen for creating eigenface
L_eig_vec= zeros(167,0);
%L_eig_vec = [];
disp(size(D));
for i = 1 : size(V,2)
if( D(i,i) > 1 )
L_eig_vec = [L_eig_vec V(:,i)];
end

end
disp(size(L_eig_vec ));
%%% finally the eigenfaces %%%
eigenfaces = A * L_eig_vec;

%In this part of recognition, we compare two faces by projecting the images
into facespace and
% measuring the Euclidean distance between them.
%
% recogimg - the recognized image name
% testimg - the path of test image
% m - mean image vector
% A - mean subtracted image vector matrix
% eigenfaces - eigenfaces that are calculated from
eigenface function
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%

%%%%%%% finding the projection of each image vector on the facespace (where
the eigenfaces are the co-ordinates or dimensions) %%%%%

projectimg = [ ]; % projected image vector matrix


for i = 1 : size(eigenfaces,2)
temp = eigenfaces' * A(:,i);
projectimg = [projectimg temp];
end

function [recognized_index]=test(m,eigenfaces,testimg,projectimg)
test_image = imread(testimg);
test_image = test_image(:,:,1);
[r c] = size(test_image);
temp = reshape(test_image',r*c,1); % creating (MxN)x1 image vector from the 2D
image
disp(strcat('SIZE OF MEAN: ',size(m)));
disp(strcat('SIZE OF TEST TEMP: ',size(temp)));
temp = double(temp)-m; % mean subtracted vector
projtestimg = eigenfaces'*temp; % projection of test image onto the facespace

%%%%% calculating & comparing the euclidian distance of all projected trained
images from the projected test image %%%%%
euclide_dist = [ ];
for i=1 : size(eigenfaces,2)
temp = (norm(projtestimg-projectimg(:,i)))^2;
euclide_dist = [euclide_dist temp];
end
[euclide_dist_min recognized_index] = min(euclide_dist);

%%% face recognition by Kalyan Sourav Dash %%%


clear all
close all
clc
%%%%%%% provide the data path where the training images are present %%%%%%%
%%% if your matlab environment doesn't support 'uigetdir' function
%%% change those lines in code for datapath and testpath as :
% datapath = 'give here the path of your training images';
% testpath = 'similarly give the path for test images';
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%datapath = uigetdir('C:\Documents and Settings\KsDash\My
Documents\MATLAB','select path of training images');
%testpath = uigetdir('C:\Documents and Settings\KsDash\My
Documents\MATLAB','select path of test images');
%{
prompt = {'Enter test image name (a number between 1 to 10):'};
dlg_title = 'Input of PCA-Based Face Recognition System';
num_lines= 1;
def = {' '};
%TestImage = inputdlg(prompt,dlg_title,num_lines,def);
%TestImage = strcat(testpath,'\',char(TestImage),'.jpg');
TestImage = 'C:\Program Files\MATLAB\R2018b\test_set\1\Image21.jpg';
%}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%
%%%%%%%%%%%%% calling the functions %%%%%%%%%%%%%%%%%%%%%%%%
[meanimg,eigenspace,projectimg]=train();
for i=1:12
cd 'C:\Program Files\MATLAB\R2018b\test_set';
cd(num2str(i));
imagefiles = dir('*.jpg');
nfiles = length(imagefiles); % Number of files found
for j=1:nfiles
pathname='C:\Program Files\MATLAB\R2018b\test_set';
currentfilename = imagefiles(j).name;
TestImage=strcat(pathname,'\',num2str(i),'\',currentfilename);
val = test(meanimg,eigenspace,TestImage,projectimg);
%selected_img = strcat(datapath,'\',recog_img);
%select_img = imread(selected_img);
%imshow(select_img);
%title('Recognized Image');
%test_img = imread(TestImage);
%figure,imshow(test_img);
%title('Test Image');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%result = strcat('the recognized image is : ',recog_img);
disp(val);
end
cd ..
end

Potrebbero piacerti anche