Eigenface Face Recognition: Why do I keep getting "Subscripted assignment dimension mismatch"?
11 views (last 30 days)
Show older comments
Jess Marcos
on 10 Apr 2016
Commented: Walter Roberson
on 16 Aug 2017
I was working on a code that I got from https://blog.cordiner.net/2010/12/02/eigenfaces-face-recognition-matlab/. I was able to make it work. Then after taking a break, I came back to the code and started getting "Subscripted assignment dimension mismatch."
Here's the code:
%%Loading the Images
clear all
close all
clc
input_dir = 'C:\Users\Nadlei\Desktop\dlsufacereg\Training';
image_dims = [30, 64];
filenames = dir(fullfile(input_dir, '*.jpg'));
num_images = 30;
images = zeros(prod(image_dims),num_images);
for n = 1:num_images
filename = fullfile(input_dir, filenames(n).name);
img = imread(filename);
%img = imresize(img,[30,64]);
img = im2double(img);
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;
% calculate the similarity of the input to each training image
input_image = imread('input.jpg');
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));
The error occurs at: "images(:,n) = img(:);" I know that it means that the matrix assignments don't match, but how do I fix it? All my input images are already in grayscale and resized to Width:30 and Height:64. Someone please help me.
0 Comments
Accepted Answer
Walter Roberson
on 10 Apr 2016
One of your images is not the same size as the others. You have an imresize() there that would take care of that problem, but you have that imresize() commented out.
5 Comments
Walter Roberson
on 10 Apr 2016
The only possibility that comes to mind is if you only had a single image then you would be sending a row vector to pca and perhaps it treats row vectors differently?
Ah, there is another explanation: your num_images might be 0, so your images variable might be empty.
Put in a breakpoint and look at the size of images and evectors and let us know what you found.
More Answers (2)
Neelesh Patel
on 10 Aug 2017
Edited: Walter Roberson
on 10 Aug 2017
in same code i have error occured in that line;
feature_vec = evectors' * (input_image(:) - mean_face);
**Matrix dimensions must agree.**
6 Comments
Neelesh Patel
on 10 Aug 2017
else give me another code which gives me specific result because its a project of 'POLICE' in india so i have to design it with full effort so plz help sir....................
Walter Roberson
on 10 Aug 2017
"because its a project of 'POLICE' in india"
I am not able to assist with police or military related projects in any country. (Not even my own -- my security clearance lapsed about a month ago.)
You need to assume that anyone without a security clearance for your country, who provides you with code for facial recognition for police or military use, is a foreign or criminal antagonist with an interest in making the project fail in hidden ways.
You need to re-read the documentation on size() and you need to learn how to use the debugger to solve this problem for yourself.
Neelesh Patel
on 16 Aug 2017
Hello sir Plzz help me for this error:
feature_vec = evectors' * (input_image(:) - mean_face); Error using - Matrix dimensions must agree.
1 Comment
Walter Roberson
on 16 Aug 2017
You need to re-read the documentation on size() and you need to learn how to use the debugger to solve this problem for yourself.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!