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

1 view (last 30 days)
%%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);
colormap(gray); % Thanks to this line I’ve got all the eigenfaces instead of getting black rectangles
end

Accepted Answer

the cyclist
the cyclist on 17 Feb 2013
Edited: the cyclist on 17 Feb 2013
What is the error message you are getting?
My guess is that the total number of elements in evectors(:,n):
numel(evectors(:,n))
is not equal to the product of the dimensions you have specified:
prod(image_dims)
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.
  6 Comments
Rachana
Rachana on 28 Apr 2013
Edited: Rachana 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?
ISRAA
ISRAA 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.

Sign in to comment.

More Answers (0)

Categories

Find more on Images in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!