%load the file containing images
load ('FaceData');
%create Training_ matriX
Training_matrix=[];
for i=1:40
for j=1:5
H=FaceData(i,j).Image;
H=double(H)/255;
%find the dim of image
[m n]=size(H);
%convert the Image in to a column vector
H=reshape(H,m*n,1);
Training_matrix=horzcat(Training_matrix,H);
end
end
%create Test matrix
Test_matrix=[];
for i=1:40
for j=6:10
H=FaceData(i,j).Image;
H=reshape(H,m*n,1);
Test_matrix=horzcat(Test_matrix,H);
end
end
%Find the covariance of the training Matrix(C)
C= cov(Training_matrix');
%calculate the eigen vectors and eigen values of Covariance matrix c
[eigVec, eigVal] = eig(C);
%select the eigen vectors
R=[];
i=1;
for j=1:size(eigVal,1)
R(i,1)=eigVal(j,j);
R(i,2)=j;
i=i+1;
end
R=sortrows(R,1);
%Initialize the Transformation Matrix
transform_matrix=[];
M=2000;
if( M > size(R,1) )
%M=size(R,1);
end
% Select K eigenvectors, u1; u1; u3; : : : uK
for j=size(R,1):-1: (size(R,1)-M + 1)
transform_matrix=horzcat( transform_matrix , eigVec(:,R(j,2)) );
end
%project the image or extract the feature vector
pca_project=[];
for i=1:200;
pca_project(:,i)=transform_matrix'*Training_matrix(:,i);
end
%ploting the first 20 eigen faces
figure;
eigenface=[];
for j=1:20
eigenface=reshape(transform_matrix(:,j),56,46);
subplot(4,5,j);
imshow(eigenface,[]);
end
%reconstruction of the face image with the ff eigen vectors
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Test_Feature_Vector=[];
F=[2, 5, 10, 20, 40, 60, 100, 150, 200, 300,400, 500, 1000, 2000];
figure;
Image=Training_matrix(:,20);
for j=1: size(F,2)
P=transform_matrix(:,1:F(j));
G=transpose(P) * Image;
I=P * G;
subplot(3,5,j);
imshow( reshape(I,56,46) , []);
title(num2str(F(j)) );
end
subplot(3,5,15);
imshow( reshape(Image,56,46) , []);
title(num2str('original') );
figure;
Rank1_IdentificationRate=[];
for j=1: size(F,2)
counter=0;
Matrix=transform_matrix(:,1:F(j));
Test_Feature_Vector= Matrix' *double(Test_matrix)/255;
Training_Feature_Vector= Matrix' * Training_matrix;
for n=1:size(Test_Feature_Vector,2)
Eculidian_Distance=[];
Total_Distance=0;
for k=1:size(Training_Feature_Vector,2)
d=0;
for i=1:size(Training_Feature_Vector,1)
d=d + ( (Training_Feature_Vector(i,k)-Test_Feature_Vector(i,n)) .^ 2 );
end
d=sqrt(d);
Eculidian_Distance(k)=d;
Total_Distance=Total_Distance + d;
end
[Eculidian_Distance Index]=sort(Eculidian_Distance);
if ceil(Index(1)/5)== ceil(n/5)
counter=counter+1;
end
end
Rank1_IdentificationRate(j)=counter/size(Test_Feature_Vector,2) ;
end
plot(F, Rank1_IdentificationRate)
title('Rank 1 identification rate');
xlabel('Number of feature vectors')
ylabel('Identification rate')