title: Matlab plot of eigen faces in PCA algorithm for face recognition
body: <p>I am new to field of machine learning and matlab. I have written the code for PCA for face recognition. But when i am trying to display the eigen faces, i am getting stuck .After getting the eigen faces, how will i be able to display them . If anyone could point out my mistake i would be very grateful. I have 150 images of training set. Each image is 231*195 matrix. When i turn into column major it turns to be 45045 *1 matrix. So my mean reduced matix of all images is A= 45045*150. After taking PCA my eigen vector matrix 45045*36 (36 eigen vectors taken main features). So my eigen face matrix is 36*150.</p>
  
  <pre><code>    function trialtrialface
  
  trainingdata = uigetdir('F:\Pattern recognition\face recognition\Yale','select path of training images');
  testdata = uigetdir('F:\Pattern recognition\face recognition\Yale','select path of test images');
  
  prompt = {'Enter test image name :'};
  dlg_title = 'Input of PCA-Based Face Recognition System';
  num_lines= 1;
  def = {' '};
  TestImage = inputdlg(prompt,dlg_title,num_lines,def);
  TestImage = strcat(testdata,'\',char(TestImage),'.pgm');
  
  %%call function
  recog_img = facerecog(trainingdata,TestImage);
  selected_img = strcat(trainingdata,'\',recog_img);
  figure;
  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(result);
  
  function [recognized_img]=facerecog(trainingdata,testimg)
  
  D = dir(trainingdata);  
  imgcount = 0;
  for i=1 : size(D,1)
      if not(strcmp(D(i).name,'.')|strcmp(D(i).name,'..')|strcmp(D(i).name,'Thumbs.db'))
          imgcount = imgcount + 1; % Number of all images in the training database
      end
  end
  
  %%%%%%%%%%%%%%%%%%%%%%%%%%  creating the image matrix X  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  
  X = [];
  for i = 1 : imgcount
      str = strcat(trainingdata,'\',int2str(i),'.pgm');
      img = imread(str);
      [r c] = size(img);
      temp = reshape(img',r*c,1);  
  %%                               
      X = [X temp];                
  end
  
  %%
  %
  %          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
  %                                    
  
  %%%%% 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 : imgcount
      temp = double(X(:,i)) - m;
      A = [A temp];
  end
  
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  % COV=cov(A');
  L= A' * A;
  [Ve,Va]=eig(L)
  % [Ve,Va]=eig(COV);
  
  
   L_eig_vec = [];
  
  DD=diag(Va);
  sorted=sort(DD,'descend');
  D=sorted/sum(sorted);
  % AA = Va;
  sumA = cumsum(D);
  kk = find(sumA &lt; 0.9)
  sum1 = sumA(kk)
  for ii=1:length(kk)
      L_eig_vec=[L_eig_vec Va(:,kk(ii))];
  end
  
  eigenfaces = A * L_eig_vec;   %eigenfaces 
  
  for i=1:size(eigenfaces,2)
      img=reshape(eigenfaces(:,i),c,r);
      img=img';
      img=histeq(img,255);
      subplot(ceil(sqrt(imgcount)),ceil(sqrt(imgcount)),i)
      imshow(img)
      drawnow;
      if i==1
          title('Eigenfaces','fontsize',18)
      end
  end
  
  projectimg = [ ];  % projected image vector matrix
  for i = 1 : size(A,2)
      temp = eigenfaces' * A(:,i);
      projectimg = [projectimg temp];
  end
  
  F     = projectimg;
  
  
  
  
  %%
  %%%%% extractiing PCA features of the test image %%%%%
  
  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
  temp = double(temp)-m; % mean subtracted vector
  projtestimg = eigenfaces'*temp; % projection of test image onto the facespace
  
  %%%%% calculating &amp; 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)));
      euclide_dist = [euclide_dist temp];
  end
  
  
  [euclide_dist_min recognized_index] = min(euclide_dist);
  recognized_img = strcat(int2str(recognized_index),'.pgm');
  end
  
  
  
  end
  </code></pre>