Skip to content

Eigenfaces face recognition (MATLAB)

2 December 2010

Eigenfaces is a well studied method of face recognition based on principal component analysis (PCA), popularised by the seminal work of Turk & Pentland. Although the approach has now largely been superseded, it is still often used as a benchmark to compare the performance of other algorithms against, and serves as a good introduction to subspace-based approaches to face recognition. In this post, I’ll provide a very simple implementation of eigenfaces face recognition using MATLAB.

PCA is a method of transforming a number of correlated variables into a smaller number of uncorrelated variables. Similar to how Fourier analysis is used to decompose a signal into a set of additive orthogonal sinusoids of varying frequencies, PCA decomposes a signal (or image) into a set of additive orthogonal basis vectors or eigenvectors. The main difference is that, while Fourier analysis uses a fixed set of basis functions, the PCA basis vectors are learnt from the data set via unsupervised training. PCA can be applied to the task of face recognition by converting the pixels of an image into a number of eigenface feature vectors, which can then be compared to measure the similarity of two face images.

Note: This code requires the Statistics Toolbox. If you don’t have this, you could take a look at this excellent article by Matthew Dailey, which I discovered while writing this post. He implements the PCA functions manually, so his code doesn’t require any toolboxes.

Loading the images

The first step is to load the training images. You can obtain faces from a variety of publicly available face databases. In these examples, I have used a cropped version of the Caltech 1999 face database. The main requirements are that the faces images must be:

  • Greyscale images with a consistent resolution. If using colour images, convert them to greyscale first with rgb2gray. I used a resolution of 64 × 48 pixels.
  • Cropped to only show the face. If the images include background, the face recognition will not work properly, as the background will be incorporated into the classifier. I also usually try to avoid hair, since a persons hair style can change significantly (or they could wear a hat).
  • Aligned based on facial features. Because PCA is translation variant, the faces must be frontal and well aligned on facial features such as the eyes, nose and mouth. Most face databases have ground truth available so you don’t need to label these features by hand. The Image Processing Toolbox provides some handy functions for image registration.

Each image is converted into a column vector and then the images are loaded into a matrix of size n × m, where n is the number of pixels in each image and m is the total number of images. The following code reads in all of the PNG images from the directory specified by input_dir and scales all of the images to the size specified by image_dims:

input_dir = '/path/to/my/images';
image_dims = [48, 64];

filenames = dir(fullfile(input_dir, '*.png'));
num_images = numel(filenames);
images = [];
for n = 1:num_images
    filename = fullfile(input_dir, filenames(n).name);
    img = imread(filename);
    if n == 1
        images = zeros(prod(image_dims), num_images);
    end
    images(:, n) = img(:);
end

Training

Training the face detector requires the following steps (compare to the steps to perform PCA):

  1. Calculate the mean of the input face images
  2. Subtract the mean from the input images to obtain the mean-shifted images
  3. Calculate the eigenvectors and eigenvalues of the mean-shifted images
  4. Order the eigenvectors by their corresponding eigenvalues, in decreasing order
  5. Retain only the eigenvectors with the largest eigenvalues (the principal components)
  6. Project the mean-shifted images into the eigenspace using the retained eigenvectors

The code is shown below:

% steps 1 and 2: find the mean image and the mean-shifted input images
mean_face = mean(images, 2);
shifted_images = images - repmat(mean_face, 1, num_images);

% steps 3 and 4: calculate the ordered eigenvectors and eigenvalues
[evectors, score, evalues] = princomp(images');

% step 5: only retain the top 'num_eigenfaces' eigenvectors (i.e. the principal components)
num_eigenfaces = 20;
evectors = evectors(:, 1:num_eigenfaces);

% step 6: project the images into the subspace to generate the feature vectors
features = evectors' * shifted_images;

Steps 1 and 2 allow us to obtain zero-mean face images. Calculating the eigenvectors and eigenvalues in steps 3 and 4 can be achieved using the princomp function. This function also takes care of mean-shifting the input, so you do not need to perform this manually before calling the function. However, I have still performed the mean-shifting in steps 1 and 2 since it is required for step 6, and the eigenvalues are still calculated as they will be used later to investigate the eigenvectors. The output from step 4 is a matrix of eigenvectors. Since the princomp function already sorts the eigenvectors by their eigenvalues, step 5 is accomplished simply by truncating the number of columns in the eigenvector matrix. Here we will truncate it to 20 principal components, which is set by the variable num_eigenfaces; this number was selected somewhat arbitrarily, but I will show you later how you can perform some analysis to make a more educated choice for this value. Step 6 is achieved by projecting the mean-shifted input images into the subspace defined by our truncated set of eigenvectors. For each input image, this projection will generate a feature vector of num_eigenfaces elements.

Classification

Once the face images have been projected into the eigenspace, the similarity between any pair of face images can be calculated by finding the Euclidean distance  between their corresponding feature vectors and ; the smaller the distance between the feature vectors, the more similar the faces. We can define a simple similarity score  based on the inverse Euclidean distance:

To perform face recognition, the similarity score is calculated between an input face image and each of the training images. The matched face is the one with the highest similarity, and the magnitude of the similarity score indicates the confidence of the match (with a unit value indicating an exact match).

Given an input image input_image with the same dimensions image_dims as your training images, the following code will calculate the similarity score to each training image and display the best match:

% calculate the similarity of the input to each training image
feature_vec = evectors' * (input_image(:) - mean_face);
similarity_score = arrayfun(@(n) 1 / (1 + norm(features(:,n) - feature_vec)), 1:num_images);

% find the image with the highest similarity
[match_score, match_ix] = max(similarity_score);

% display the result
figure, imshow([input_image reshape(images(:,match_ix), image_dims)]);
title(sprintf('matches %s, score %f', filenames(match_ix).name, match_score));

Below is an example of a true positive match that was found on my training set with a score of 0.4425:

To detect cases where no matching face exists in the training set, you can set a minimum threshold for the similarity score and ignore any matches below this score.

Further analysis

It can be useful to take a look at the eigenvectors or “eigenfaces” that are generated during training:

% display the eigenvectors
figure;
for n = 1:num_eigenfaces
    subplot(2, ceil(num_eigenfaces/2), n);
    evector = reshape(evectors(:,n), image_dims);
    imshow(evector);
end

Above are the 20 eigenfaces that my training set generated. The subspace projection we performed in the final step of training generated a feature vector of 20 coefficients for each image. The feature vectors represent each image as a linear combination of the eigenfaces defined by the coefficients in the feature vector; if we multiply each eigenface by its corresponding coefficient and then sum these weighted eigenfaces together, we can roughly reconstruct the input image. The feature vectors can be thought of as a type of compressed representation of the input images.

Notice that the different eigenfaces shown above seem to accentuate different features of the face. Some focus more on the eyes, others on the nose or mouth, and some a combination of them. If we generated more eigenfaces, they would slowly begin to accentuate noise and high frequency features. I mentioned earlier that our choice of 20 principal components was somewhat arbitrary. Increasing this number would mean that we would retain a larger set of eigenvectors that capture more of the variance within the data set. We can make a more informed choice for this number by examining how much variability each eigenvector accounts for. This variability is given by the eigenvalues. The plot below shows the cumulative eigenvalues for the first 30 principal components:

% display the eigenvalues
normalised_evalues = evalues / sum(evalues);
figure, plot(cumsum(normalised_evalues));
xlabel('No. of eigenvectors'), ylabel('Variance accounted for');
xlim([1 30]), ylim([0 1]), grid on;

We can see that the first eigenvector accounts for 50% of the variance in the data set, while the first 20 eigenvectors together account for just over 85%, and the first 30 eigenvectors for 90%. Increasing the number of eigenvectors generally increases recognition accuracy but also increases computational cost. Note, however, that using too many principal components does not necessarily always lead to higher accuracy, since we eventually reach a point of diminishing returns where the low-eigenvalue components begin to capture unwanted within-class scatter. The ideal number of eigenvectors to retain will depend on the application and the data set, but in general a size that captures around 90% of the variance is usually a reasonable trade-off.

Closing remarks

The eigenfaces approach is now largely superceded in the face recognition literature. However, it serves as a good introduction to the many other similar subspace-base face recognition algorithms. Usually these algorithms differ in the objective function that is used to select the subspace projection. Some subspace-based techniques that are quite popular include:

129 Comments
  1. Ayesha Hakim permalink

    In step:
    shifted_images = images – repmat(mean_face, 1, num_images);
    it gives the error that matrix dimension must agree while subtracting. What may be the problem?

    • It’s hard to say – it could be the images were loaded incorrectly into the matrix, the mean face vector was calculated along the wrong dimension, the number of input images was miscounted, etc. When you run it, what are the dimensions of images and mean_face? And what is the value of num_images and what dimensions in pixels are the images you are using?

  2. Ayesha Hakim permalink

    Thanks, I solved the problem. I am looking for a tutorial for Fisher’s linear discriminant (FLD) and how to implement it with PCA. Your blog helps me a lot, can you please also publish similar blog for fisherfaces as well?

  3. nilufar permalink

    i have the same problem in line
    shifted_images = images – repmat(mean_face, 1, num_images);
    can you give some help,

    • I have updated the code, on the previous line it was calculating mean_face along the wrong dimension. It should work now.

  4. Naufal permalink

    Its good tutorial but I’m stuck.
    It gives error
    ??? Subscripted assignment dimension mismatch.

    Error in ==> eig at 13
    images(:, n) = img(:);
    May I know why?

    Is it OK I put input_dir = ‘C:\Program Files\MATLAB\R2009b\work\s1’;

    • The most likely cause is that your input images have inconsistent dimensions or you are not specifying the dimensions correctly. Check that:

      1. all of your input images are the same size and that they are greyscale images (use rgb2gray after loading if they are colour images)
      2. you are correctly setting image_dims to specify the dimensions of your input images in the format [height, width]

      To answer your other question, using Windows paths with spaces and backslashes is fine in MATLAB.

  5. fafa permalink

    i have a problem in line
    evectors = evectors(:, 1:num_eigenfaces);
    can you give me some help

    • You’ll need to explain the problem you’re having.

      • Sastry permalink

        Hey This is Sastry. I was trying to use your code. I find the following error.
        Index exceeds matrix dimensions.

        Error in Untitled2 (line 25)
        evectors = evectors(:, 1:num_eigenfaces);

      • Anamika Jain permalink

        Index exceeds matrix dimensions
        in the step 5 why? and what to do to resolve this.

    • myrtilo permalink

      Hello guys ,I just solved the problem
      Index exceeds matrix dimensions.
      evectors = evectors(:, 1:num_eigenfaces);

      Format->Windows x64->matlab x64
      Now i have as much memory as i need.

      • Henry permalink

        myrilo, what do you mean, Format->Windows x64->matlab x64

  6. weasels permalink

    Hi,
    I have a doubt: do we have to take all the training images at once to generate the eigen vectors(say e) or do we take the 1st individuals’ set of images and find eigen vectors(say e1) then find the 2nd individuals sets’ eigen vectors(say e2) and so on and then include all the eigen vectors into one eigen vector variable ( say e = [e1;e2;e3…] )
    because when i try to get first 20 eigen vectors, they are not the same as yours, instead i am getting the eigen faces as a collection of all and is not giving 20 unique faces as yours…hope u understood my problem (“cook1234567@gmail.com” ). i put all 449 images in 1 folder and am using it as a training database
    input_dir= “c:\myimages\” conatain all 449 caltech images
    input_image = c:\image_0450.jpg;
    now my output is not either of image_0430 to image_0449 rather it mathes image_0278

    • You should be training a single PCA classifier on all of your face images, not one per individual person. Did you test on more than one face? Did it match any faces correctly?

  7. weasels permalink

    Hi Alister,
    Yes, in fact i have tried using single pca classifier considering the whole database at once(one folder containing 20 face images each of 20 individuals).I could only get a 28% success. I tried with other databases like clouds ,leaves,MPEG 7 CE shapes images database but not much difference. So i tried improving it by transforming the images to wavelets and then subject to PCA and it worked well.Now i can get a proper classification with a 70% to 84% success depending upon the database.
    Thanks for your blog, it helped me a lot.

    • Possibly it could be related to the data set. It’s just a guess but because eigenfaces use absolute pixel intensities as their features, they are highly susceptible to lighting variations, whereas differential features such as wavelets are not. Do you have significant lighting variations in your face images? If so, you could try normalising the images first using a technique such as histogram equalisation. Anyway, glad to hear you got it working with wavelets anyway.

    • ankit permalink

      can u explain wavelet transformation plz

    • tri permalink

      hi weasels, may i know more about your way, i type code like alister, but with lucky, just 35% with high resolution picture.
      would u mind if u send me your code, plz. i’m having trouble with my final project.
      this is my mail: ngocquangcity@gmail.com

    • kavya permalink

      how did u transform it to wavelets pls tell me…. i hve run my code without a error bt test image is matching to some other person image in d training

  8. jyotsna permalink

    if i want to use these eigen vectors as a input to neural network how cn i use it…….cn u plz help me

    • Just use feature_vec as the input to your neural network. I used a simple classifier based on Euclidean distance, but neural networks are just another form of classifier and work basically the same way.

      • miranti permalink

        Its good tutorial but I’m stuck.
        It gives error??? Subscripted assignment dimension mismatch.

        Error in
        feature_vec = evectors’ * (image_dims(:) – mean_face);
        May I know why?

        Is it OK I put input_dir = ‘C:\Program Files\MATLAB\R2009b\work\s1’;

  9. irlmaks permalink

    Hi. I have used pics with background. I tried to do everything you described in the way it is. For the input image i have used one of the test images. But the result is rather horrible which gives out some arbitrary image number with a very low similarity score of about 0.0002. Also when displaying the result it is not the image corresponding to the detected number, rather a black and white image with very few black image pixels. Can you point out the possible mistake that i am making?

    Note: I am not decreasing the number of eigen vectors. As a precautionary measure to test, i have used all of them.

    I am posting the code which i have compiled

    %% Loading the Images
    clear all
    input_dir = ‘path of the training image files’;
    image_dims = [48 64];

    filenames = dir(fullfile(input_dir, ‘*.jpg’));
    num_images = numel(filenames);

    images = zeros(prod(image_dims),num_images);

    for n = 1:num_images
    filename = fullfile(input_dir, filenames(n).name);
    img = imread(filename);
    img = rgb2gray(img);
    img = imresize(img,image_dims);
    images(:, n) = img(:);
    end
    %% Training
    % steps 1 and 2: find the mean image and the mean-shifted input images
    mean_face = mean(images, 2);
    shifted_images = images – repmat(mean_face, 1, num_images);

    % steps 3 and 4: calculate the ordered eigenvectors and eigenvalues
    [evectors, score, evalues] = princomp(images’);

    % step 5: only retain the top ‘num_eigenfaces’ eigenvectors (i.e. the principal components)
    %num_eigenfaces = 1500;
    %evectors = evectors(:, 1:num_eigenfaces);

    % step 6: project the images into the subspace to generate the feature vectors
    features = evectors’*shifted_images;

    % calculate the similarity of the input to each training image
    input_image = imread(‘input.jpg’);
    input_image = rgb2gray(input_image);
    input_image = imresize(input_image,image_dims);
    input_image = im2double(input_image);
    feature_vec = evectors’ * (input_image(:) – mean_face);

    similarity_score = arrayfun(@(n) 1 / (1 + norm(features(:,n) – feature_vec)), 1:num_images);

    % find the image with the highest similarity
    [match_score, match_ix] = min(similarity_score);

    % display the result
    figure, imshow([input_image reshape(images(:,match_ix), image_dims)]);
    title(sprintf(‘matches %s, score %f’, filenames(match_ix).name, match_score));

    • rahul satija permalink

      hiii…
      i m facing the same problem.If u have got the solution of above problem then plzz share.That will be a great help.

    • pooja permalink

      nput_image = imread(‘input.jpg’);

      i have error in this line
      error is:

      Error using imread (line 350)
      File “input.gif” does not exist.

      Error in try2 (line 16)
      input_image = imread(‘input.gif’);

  10. I have a question. If I associate a parameter with each image in the training set, would I be able to use this technique for regression rather than classification with an unknown test set?

    • What parameter do you have in mind? It may be possible if the parameter has a direct relationship to the face image (e.g. the subject’s age). Its effectiveness would depend entirely on what the parameter represents, what its relationship is to the principal components/facial features and what type of regression you are using (simple linear regression vs. more complex non-linear methods like neural networks).

      • I am not using it for facial recognition. I would like to adapt this technique for another application. I have a bunch of images of galaxies, grouped into a test and training set. Each galaxy as a 2-tuple associated with it, which describes its ellipticity (e1, e2). Using the training images, I would like to predict the ellipticity (e1, e2) of the test set, for which it is unknown. To reduce dimension, i would use PCA to get a feature vector using the same method described above.

        However, I cannot find any literature about using feature vectors for regression. I’m very much new to Machine learning.

  11. hgg permalink

    I am doing comp engg in mumbai
    i have to complete this project in the coming months ..
    can any o u guys help …
    much appreciated .

  12. roni permalink

    hi,
    when I look at the eigenfaces I get from the program, I get nothing like yours. I very dark pictures, almost all black with a bit of gray lines.
    I am using the same database u used and I cropped the background out.
    so I have pics of faces without the background.

    • If you increase the contrast of the images can you make out anything?

      • roni permalink

        hi
        I think I found my mistake.
        The images in the database were without background but the faces were not aligned to the center, so I had different positions of black background giving me much more variance, which was represented by black spots. I believe if I try with center-alligned images it will work. Thanks.

    • Sastry permalink

      Hi roni,
      Can you tell me how did u center align the images?

  13. rahul satija permalink

    Hi.i am facing the same problem as Irlmaks. I have used pics with background. I tried to do everything you described in the way it is. For the input image i have used one of the test images. But the result is rather horrible which gives out some arbitrary image number with a very low similarity score of about 0.0002. Also when displaying the result it is not the image corresponding to the detected number, rather a black and white image with very few black image pixels. Can you point out the possible mistake that i am making?

  14. shashank permalink

    could u plz explain the significance of calculating the eigen vector in this algo????

  15. >> input_dir = ‘path’;
    >> image_dims = [200,100];
    >> filenames = dir(fullfile(input_dir,’*.jpg’));
    >> num_images = numel(filenames);
    >> images = [];
    >> for n = 1:num_images
    filename = fullfile(input_dir, filenames(n).name);
    img = imread(filename);
    if n == 1
    images = zeros(prod(image_dims),num_images);
    end
    images(:,n)=img(:)
    end
    ??? Subscripted assignment dimension mismatch.
    can u help me to solve this error… :)

    • rahul permalink

      hi dshivkiran87,
      the problem u r facing is that u r using rgb images…..first convert it into grayscale and then use them to make the training set.I m posting a part of my code .hope u will get help from it.

      n=1:no_images
      filename = fullfile(a, fn(n).name);
      img = imread(filename);
      img=rgb2gray(img);
      img = imresize(img,image_dims);

  16. radhika permalink

    hi,
    I have some problem in the PCA phase of the code. It is causing some memory error. The error is :

    ??? Error using ==> svd
    Out of memory. Type HELP MEMORY for your options.

    Error in ==> princomp at 85
    [U,sigma,coeff] = svd(x0,econFlag); % put in 1/sqrt(n-1) later

    Error in ==> bestofluck at 6
    [evectors, score, evalues] = princomp(images’);

    what is this error related to ? could you help please… ?

  17. prajakta permalink

    at the step 6-[evectors, score, evalues] = princomp(images’);, it is giving an err ” Out of MEMORY”….
    Can u plz help me to solve this err???

  18. mohamed alahmady permalink

    hi i have aproplem in this code, please quickly

    >> input_dir=’C:\Documents and Settings\m\My Documents\My Pictures\faces’;
    >> image_dims = [48, 64];
    >> filenames = dir(fullfile(input_dir, ‘*.png’));
    >> filenames = dir(fullfile(iput_dir, ‘*.png’));
    >> num_images = numel(filenames);
    >> images = [];
    >> for n = 1:num_images
    filename = fullfile(input_dir, filenames(n).name);
    img = imread(filename);
    if n == 1
    images = zeros(prod(image_dims), num_images);
    end
    images(:, n) = img(:);
    end
    >> mean_face = mean(images, 2);
    >> shifted_images = images – repmat(mean_face, 1, num_images);
    >> [evectors, score, evalues] = princomp(images’);
    >> num_eigenfaces = 20;
    >> evectors = evectors(:, 1:num_eigenfaces);
    ??? Index exceeds matrix dimensions.

    • tidusx permalink

      Hi I see your havn the same problem as me with this code.
      Just wondering did you manage to find a solution?

    • Mesh Malik permalink

      for index exceeds matrix dimension
      evectors = evectors( 1:num_eigenfaces)
      by using above statement ,no error

  19. tidusx permalink

    Hi, I am currently doing something similar in my college project and just testing out your code so that i can get some practice results.
    For some reason there is a problem with
    evectors = evectors(:, 1:num_eigenfaces);
    It keeps giving me a error saying
    “index exceeds matrix dimensions”.
    Can you help me out with this

  20. evya permalink

    it meens that num_eigenfaces is more than the number of columns in evectors Matrix

  21. Nicolas permalink

    Why do you use princomp(images’) and not princomp(shifted_images*shifted_images’) ? I thought PCA was using the eigenvectors of the symmetric matrix : shifted_images*shifted_images’ ?

  22. Hardik permalink

    I have an error at,

    evectors = evectors(:, 1:num_eigenfaces);
    also, evectors is not the any code in matlab 7.5.0. I saw that in help menu of matlab.
    can you suggest me,why this occur? what is the solution of it?

    • Sastry permalink

      Hardik,
      evectors is here only a variable. Its not a function of matlab.

  23. Sastry permalink

    Alister,
    I have the problem while displaying the eigenfaces. I get totally black squares where it is supposed to be the eigen faces. I have converted the rgb images to gray scale. Can you let me know whats the mistake?

  24. dataofviraj permalink

    Some of you guys are having problems displaying eigenfaces. You might be getting black like images.
    I had the same problem, so after converting the training set from RGB to grayscale. use this:

    img = im2double(img);

    this corrects the problem. I am not sure how btw.. :-P

    • Sastry permalink

      No I still have the same problem the Eigen faces are just a bunch of Black boxes. It does not show the Eigen faces :(

  25. matt permalink

    where do i download the full matlab code?

  26. margeo20 permalink

    Hi, I have tested your code above, but when it runs, it appears black boxes in the position of the eigenfaces and also the same applies when it tries to find the matced image near to the input_image. What should I do?
    Also is it possible to give us the full matlab code to test it without having errors?

    Thank you

  27. suchithra permalink

    i am currently doing this project and it was helpful for me. thank u very much

  28. CanerOdabas permalink

    hey guys! If you have trouble with further analysis, displaying eigenvectors part. You can try to change the “for” loop using fallowing code:

    for n = 1:num_eigenfaces
    subplot(2, ceil(num_eigenfaces/2), n);
    evector = reshape(evectors(:,n), image_dims);
    imagesc(evector);
    colormap(gray);
    end

    This piece of code I wrote solved my problem, I hope it will be useful for you too

    • Niloofar permalink

      hi,thank you very much for your solution,it was one of my problems and your code help me
      thank you

    • Loona permalink

      Hello CanerOdabas,
      Thanks for the comment, I have problem in this code how to display my eigenvectors as shown ib this blog :
      figure;
      for n = 1:num_eigenfaces
      subplot(2, ceil(num_eigenfaces/2), n);
      evector = reshape(evectors(:,n), image_dims);
      imagesc(evector);
      colormap(gray);
      end
      please what is the error? my faces database has 100 images, grayscalee and size 64×64 all.
      thanks,

  29. Nice post. Explains things really well.
    Would you please do a post on HOG feature pyramid also?

    -Thanks

  30. SAHIL permalink

    Error using reshape
    To RESHAPE the number of elements must not change.

    Error in trial (line 54)
    figure, imshow([input_image reshape(images(:,match_ix), image_dims)]);

    why i am getting this error??

    • Nikhil V Kapre permalink

      Sahil, please try the following syntax with one image only
      imshow(reshape(images(:,match_ix), image_dims), []);

  31. SAHIL permalink

    thnks Nikhil,dt wrked.
    Nw i am getting this error…pls help

    Index exceeds matrix dimensions.

    Error in trial (line 55)

    title(sprintf(‘matches %s, score %f’, filenames(match_ix).name, match_score));

  32. SAHIL permalink

    and i am not getting the matched image,instead of that i am getting blank(white)image

    • Nikhil V Kapre permalink

      Are the image dimensions Ok? Try reversing the dimensions. Once I too made a similar mistake.

  33. ucuzcuselahattin permalink

    I have a problem in step 6. it gives error. it is said that, “Inner matrix dimensions must agree.”

  34. ucuzcuselahattin permalink

    I have a problem in step 6. it gives error. it is said that, ” ??? Error using ==> mtimes
    Inner matrix dimensions must agree. “

  35. ucuzcuselahattin permalink

    I have a problem in step 6. it gives error. it is said that, ” ??? Error using ==> mtimes
    Inner matrix dimensions must agree. “

  36. Sahil permalink

    Can ne 1 pls explain wat this function does..??
    similarity_score = arrayfun(@(n) 1 / (1 + norm(features(:,n) – feature_vec)), 1:num_images);

    • stafik permalink

      First of all thanks for a really useful piece of code!
      I’ve been struggling with some errors for some time but finally I managed to run it successfully. Thanks to everybody for the comments that I used to track my problems.
      I used clear and full installation of Matlab R2010b.

      So here goes my version (AFAIR only two lines added to the original one):

      %% Loading the Images
      clear all
      input_dir = ‘D:\MATLAB R2012a\work’;
      image_dims = [300, 238];

      filenames = dir(fullfile(input_dir, ‘*.jpg’));
      num_images = numel(filenames);

      images = zeros(prod(image_dims),num_images);

      for n = 1:num_images
      filename = fullfile(input_dir, filenames(n).name);
      img = imread(filename);
      % img = rgb2gray(img); % I’ve done it manually before using IrfanView
      img = im2double(img); % Thanks to this line the best match is shown. Without it it was just a white space.
      img = imresize(img,image_dims);
      images(:, n) = img(:);
      end

      %% Training
      % steps 1 and 2: find the mean image and the mean-shifted input images
      mean_face = mean(images, 2);
      shifted_images = images – repmat(mean_face, 1, num_images);

      % steps 3 and 4: calculate the ordered eigenvectors and eigenvalues
      [evectors, score, evalues] = princomp(images’, ‘econ’);

      % step 5: only retain the top ‘num_eigenfaces’ eigenvectors (i.e. the principal components)
      num_eigenfaces = 11;
      evectors = evectors(:, 1:num_eigenfaces);

      % step 6: project the images into the subspace to generate the feature vectors
      features = evectors’*shifted_images;

      % calculate the similarity of the input to each training image
      input_image = imread(‘PC044879_cr.jpg’);
      % input_image = rgb2gray(input_image);
      input_image = imresize(input_image,image_dims);
      input_image = im2double(input_image);
      feature_vec = evectors’ * (input_image(:) – mean_face);

      similarity_score = arrayfun(@(n) 1 / (1 + norm(features(:,n) – feature_vec)), 1:num_images);

      % find the image with the highest similarity
      [match_score, match_ix] = min(similarity_score);

      % % display the result
      % figure, imshow([input_image reshape(images(:,match_ix), image_dims)]);
      % colormap(gray);
      % title(sprintf(‘matches %s, score %f’, filenames(match_ix).name, match_score));

      % display the result
      figure, imshow([input_image reshape(images(:,match_ix), image_dims)]);
      % colormap(gray);
      title(sprintf(‘matches %s, score %f’, filenames(match_ix).name, match_score));

      % display the eigenvectors
      figure;
      % for n = 1:num_eigenfaces
      % subplot(2, ceil(num_eigenfaces/2), n);
      % evector = reshape(evectors(:,n), image_dims);
      % imshow(evector);
      % end
      for n = 1:num_eigenfaces
      subplot(2, ceil(num_eigenfaces/2), n);
      evector = reshape(evectors(:,n), image_dims);
      imagesc(evector);
      colormap(gray); % Thanks to this line I’ve got all the eigenfaces instead of getting black rectangles
      end

      • Karim permalink

        I am having a problem in the results. I am always getting a completely different face from the input image, although the same face is in the training images `images`. Anyone can help?

      • Don’t you get error (about matrix) in

        num_eigenfaces = 11;
        evectors = evectors(:, 1:num_eigenfaces);

        ?

      • Namrata permalink

        Can anyone please help me to resolve the following error.

        ??? Out of memory. Type HELP MEMORY for your options.
        Error in ==> repmat at 78
        B = A(ones(siz(1), 1), :);
        Error in ==> princomp at 64
        x0 = x – repmat(mean(x,1),n,1);

        Thanks in advance….

      • Namrata permalink

        Hi stafik…. i tried using ur code i am still facing many problems… Few Queries..

        features = evectors’*shifted_images; % here what does the quote (‘) stands for???

        I face dimension mismatch problem during matrix multiplication ie.
        (no. of column MAT1 != no. of rows MAT2)
        Would it be correct if we write the above step like?
        features = shifted_images*evectors;

        Kindly revert….

      • mero permalink

        hell
        can you show me in video file your steps

      • Chaitra permalink

        How to solve the error “to reshape the number of elements must not change” in display eigenvector.

      • chaitra permalink

        please tell me how to find false acceptance rate and false rejection rate using this code

  37. avisek permalink

    can anyone tell the matlab code to create and read an image database

  38. Ravi Kumar mishra permalink

    i am getting an error in this line
    num_eigenfaces = 11;
    evectors = evectors(:, 1:num_eigenfaces);

    ERROR: “??? Index exceeds matrix dimensions.”

    and i am using 10 images in the directory so is it the problem with the “num_eigenfaces”.
    How much i should take???

  39. Ravi Kumar mishra permalink

    ??? Error using ==> reshape
    To RESHAPE the number of elements must not change.

    evector = reshape(evectors(:,n), image_dims)
    why this error is coming can anyone help….ty!!

  40. Calculate the mean of the input face images: mean_face = mean(images, 2);
    Here, why to calculate the row mean for the sample data? Since the column means the dimension, why no calculate the mean with col? like mean_face = mean(images) ?

  41. Afsaneh permalink

    hi
    Truly i couldn’t ignore saying thank you. it is a great tutorial that i could step in AI world with.
    I really appreciate your help. but I have problem in seeing images. could you please upload a pdf version. and what;s more may i please get your help about implementing some face recognition articles? thanks again.

  42. chetana permalink

    I have one problem related to face recognition using eigen face method.If we want to recognize the face from the crowded image then what technique should be used?

  43. akram permalink

    haw can i download the code source

  44. Karim permalink

    I am having a problem in the results. I am always getting a completely different face from the input image, although the same face is in the training images `images`. Anyone can help?

  45. dramon11 permalink

    Hi, as I can do so that when there is no face to me an error message … thanks ….

  46. dramon11 permalink

    Ok, is it !!!

    maximo = 0.15000;
    minimo = 0.11200;

    if max(similarity_score)>=maximo
    [match_score, match_ix] = max(similarity_score);

    display(‘ user succeeded’)

    else
    if min(similarity_score)<=minimo
    [match_score, match_ix] = min(similarity_score);

    display(' face failed')

    end
    end

    Buena Suerte !

  47. Loona permalink

    Hello everyone,
    Thanks for the nice code, but I have problem in displaying my eigenvectors , this is the code I am using :
    % display the eigenvectors
    figure,
    for n = 1:num_eigenfaces
    subplot(2, ceil(num_eigenfaces/2), n);
    evector = reshape(evectors(:,n), image_dims);
    imagesc(evector);
    colormap(gray);
    end

    please can anyone help me .Thanks in advance.

  48. singh permalink

    can you help me matlab code for lda

  49. Kumar permalink

    Hello,
    Thanks for the nice code and explanation. Please, I have done this perfect method of face recognition (PCA+Euclidean Distance) , I just want to calculate the accuracy of how PCA does the recognition using Eu dis?? So, please help me :)

    Kumar

  50. venus permalink

    hello i can not see eigenfaces as you display what can i do?
    my eigenfaces are black

    • Namrata permalink

      check this whether it helps or not…

      %% display the eigenvectors
      figure;
      normalised_evalues = evalues / sum(evalues);
      figure, plot(cumsum(normalised_evalues));
      xlabel(‘No. of eigenvectors’), ylabel(‘Variance accounted for’);
      xlim([1 30]), ylim([0 1]), grid on;
      for n = 1:num_eigenfaces
      subplot(2, ceil(num_eigenfaces/2), n);
      evector = reshape(evectors(:,n), image_dims);
      imagesc(evector);
      colormap(gray); % Thanks to this line I’ve got all the eigenfaces instead of getting black rectangles
      end

  51. vijay permalink

    sir pls can u send me code to vijaysb107@gmail.com.

  52. nehal permalink

    Can anyone please help me to resolve the following error.

    ??? Index exceeds matrix dimensions.

    Error in ==> Eigenfaces at 25
    evectors = evectors(:, 1:num_eigenfaces);

  53. Van permalink

    I hae a database in which the background is present, i don’t want to manually crop all the pictures to give only the face, is there a way to do this on matlab?

  54. osmantayfunyelim permalink

    Hello ,

    First of all , I wanna say this code has been very helpful.Thank you for that ,
    now when it comes to my problem , I am having hard time with the similarity score , I mean if I have only one image in the folder having images to be searched ,code works fine and gives a similarity score of 0.1 or close when the images are the same or very similar .However ,if I have more than one image in the folder ,code yields 2.5 *10^-3 or close as the similarity score . why does the code do that when there are more than one images in the folder having images to be searched.

    Thanks so much in advance. Have a nice day.

  55. This post is very informative but it took me a long time to find it in google.
    I found it on 21 spot, you should focus on quality backlinks building,
    it will help you to rank to google top 10. And i know how to help you, just search in google – k2
    seo tips

  56. Renata permalink

    ucuzcuselahattin , how yo solve the problem? pls help Inner matrix dimensions must agree

  57. Renata LopVel permalink

    feature_vec = evectors’ * (input_image(:) – mean_face); I have this error . pls help Inner matrix dimensions must agree. Do you have any idea how to solve this error. Thanks so much in advance. Have a nice day.

  58. dhvani permalink

    Hey…
    Plzzzz do tell me I hav jus used three images in my training set n I hav copies its location to input-dir so all the three different images will be processed..
    Then wat shall I write in
    num-eigenfaces=how can I solve inner matrix error

  59. Swarnava permalink

    Hi,
    Could you please tell me why the mean is calculated across each row ? And If I calculate the mean across the columns will it affect my final result ? Please reply

  60. amith permalink

    please anyone post the complete correct code here with out any error it will be really helpful thank you.
    amithgujjar@gmail.com

  61. SWATI permalink

    Dear all,

    I am using PCA and I completed with PCA. I found all the eigenfaces and feature vector but I dont know how to reconstruct the image using these eigenfaces.

    How to reconstruct image using PCA??

  62. zeina permalink

    Hello
    I’m having a problem displaying the eigenfaces

    ??? Error using ==> reshape
    To RESHAPE the number of elements must not change.

    Error in ==> eigen1 at 48
    evector = reshape(evectors(:,n), 100, 100);

    can anyone help?
    the thing is that I’m using 40 images, so the evectors size is (40,40)
    thus, I cannot display it in image_dims which are in my case 100*100

    thank you

  63. adashiah permalink

    Has anoyone been able to get this code to run with no errors? I have gotten multiple errors with the code.

  64. ejaz4616 permalink

    Hi sir
    i am simulating your code but it give error…can you please send me matlab file code…”ejaz4616@qq.com”…i will be very very thankful to you for this…thanks in advance..

  65. shiva permalink

    i got error :
    Error using –
    Matrix dimensions must agree.

    Error in secondPhase (line 39)
    feature_vec = evectors’ * (double(input_image(:)) – mean_face);

    what is input_image here

  66. Akhil kumar permalink

    I have a problem. can any one of you solve my problem??
    Let us assume a Scenario I have a video clip of 10 min. In that I would like to know presence (total time) of a particular person in the video. For that I extracted all the frames in the video and now I wanted to match the person with the input image and then calculate the frames in terms of time eg. say 24fps…if 500 pictures are matching 500/24 Sec. Is there any other way to find?? Thanks in advance

  67. Chaitra permalink

    Hai.. Can you tell me how to calculate feature for more than 1 image and where & how to store it separately.. Please suggest me..

  68. K4p7a1n H00k permalink

    @Akhil
    you can simply use the run and advance option in matlab. It’s gonna give you the runtime. you can then divide the number of images by that

  69. Farwa permalink

    I am having a problem in this code…When i run the code it shows the following error

    Subscripted assignment dimension mismatch.

    Error in featureExtraction (line 13)
    images(:, n) = img(:);

  70. Chaitra permalink

    Please can any one send me the code for feature ectraction using dct algorithm..

  71. S-3 Burton permalink

    The classification code is missing and/or not available to use – this crucial part of thew code is needed to complete classification and display the correct answer.

  72. kas permalink

    hey when I run your code till

    features = (evectors’ * shifted_images’)’;

    it does not creates eigenfaces as the accurate sequence of images in the shifted images

    evectors in my case is 360×360 and shifted)images is 10344×360

    360 IMAGES

    This creates diffuclty for me to use it for clsssification suing neural net.Can you please help me out!!

  73. Anamika Jain permalink

    got an error in last line:
    in step 5.
    steps 1 and 2: find the mean image and the mean-shifted input images
    mean_face = mean(images, 2);
    shifted_images = images – repmat(mean_face, 1, num_images);

    % steps 3 and 4: calculate the ordered eigenvectors and eigenvalues
    [evectors, score, evalues] = princomp(images’);

    % step 5: only retain the top ‘num_eigenfaces’ eigenvectors (i.e. the principal components)
    num_eigenfaces = 20;
    evectors = evectors(:, 1:num_eigenfaces);

    index exceeds matrix dimensions.

  74. Cristian Perez permalink

    Hola amigos estoy tratando de hacer funcionar este codigo y me arroja estos errores alguien me puede ayudar por favor

    0 0

    Index exceeds matrix dimensions.

    Error in fcn_eigenfaces (line 60)
    evectors=evectors(:,1:num_eigenvectors);%eigenvectors sin ruido

    Error in guide_final>procesar_cargada_Callback (line 206)
    [foto_ganadora,nombre_foto_ganadora]=fcn_eigenfaces(handles.img,’.\train’);

    Error in gui_mainfcn (line 95)
    feval(varargin{:});

    Error in guide_final (line 42)
    gui_mainfcn(gui_State, varargin{:});

    Error in
    matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)guide_final(‘procesar_cargada_Callback’,hObject,eventdata,guidata(hObject))
    Error while evaluating UIControl Callback

  75. Error:Subscripted assignment dimension mismatch.

    Error in Loading_images (line 14)
    images(:, n) = img(:);

  76. Md.Saddam Hossain permalink

    sir pls can u send me code for unknown face detection using PCA

  77. ashwini permalink

    Can you please mail me this code? my id is ashwinipatil29@gmail.com

    thank you so much in advance…

Trackbacks & Pingbacks

  1. please use MATLAB to construct the following code. Eigenfaces for Facial Recognition Facial… – n5

Leave a reply to Hardik Cancel reply