Error while using PCA for image recognition?

1 view (last 30 days)
Anjana Rao
Anjana Rao on 11 Mar 2014
Commented: suchetha n on 1 Jan 2021
Hello!
I've used this code for image recognition using PCA. I keep getting this error:
Error using -
Integers can only be combined with integers of the same class, or scalar doubles.
Error in pirnccompanal (line 43)
feature_vec = evectors' * (input_image(:) - mean_face);
----------------------------------------------------------------------------------------------
Can someone tell me what is wrong with the code? All the images used have dimensions of 60x60. gest1Cell is a cell of 30 images in grayscale (60x60).
Thank you for your time!
clc;
clear all;
gestdatabase;
input_image = gest1Cell{25};
images = [];
image_dims = [60, 60];
num_images = 30;
for n = 1:30
img = gest1Cell{n};
if n == 1
images = zeros(prod(image_dims), num_images);
end
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] = pca(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;
% TESTING AND CLASSIFICATION
% 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));

Answers (1)

Image Analyst
Image Analyst on 11 Mar 2014
Try casting everything to double before doing the math.

Community Treasure Hunt

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

Start Hunting!