MATLAB Answers

Ravi Mishra

I am getting an error in disply the eigenvectors the 4 th last line reshape can anyone help me out???

Asked by Ravi Mishra
on 17 Feb 2013
Latest activity Commented on by ISRAA
on 13 Oct 2013
%% 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(:);
%% 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
for n = 1:num_eigenfaces
subplot(2, ceil(num_eigenfaces/2), n);
evector = reshape(evectors(:,n), image_dims);
colormap(gray); % Thanks to this line I’ve got all the eigenfaces instead of getting black rectangles



No products are associated with this question.

1 Answer

Answer by the cyclist
on 17 Feb 2013
Edited by the cyclist
on 17 Feb 2013
 Accepted answer

What is the error message you are getting?

My guess is that the total number of elements in evectors(:,n):


is not equal to the product of the dimensions you have specified:


and therefore the reshape cannot be done. If that's the case, you should be able to trace back why that's happening, using debug mode.


the cyclist
on 26 Feb 2013

I suggest you type

>> dbstop if error

before executing your code. Then, run your code. It will halt in debug mode when it reaches the error. Then, you can see exactly the size of each variable at that time.

Is input_image(:) the same size as mean_face?

Is the first dimension of that difference the same length as the first dimension of evectors(i.e. the second dimension of the transpose)?

on 28 Apr 2013

Hi, Im also working on the above code. But the error im getting is in the line

feature_vec = evectors' * (input_image(:) - mean_face);

Basically the dimensions of evectors and the term inside the bracket dont match. Cld u pls suggect hw to correct it?

on 13 Oct 2013

Hello, I have used this nice code ,, but the results are black rectangles .. Please, can any body suggest any idea? I am not getting any clear msg error in the command window but the result are not the eigenvectors as written. Thanks in advance.

Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!