Eigenface Face Recognition: Why do I keep getting "Subscripted assignment dimension mismatch"?

조회 수: 13 (최근 30일)
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.

채택된 답변

Walter Roberson
Walter Roberson 2016년 4월 10일
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
Jess Marcos
Jess Marcos 2016년 4월 10일
The next error I get is: "Index exceeds matrix dimensions." at:
evectors = evectors(:, 1:num_eigenfaces);
Walter Roberson
Walter Roberson 2016년 4월 10일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Neelesh Patel
Neelesh Patel 2017년 8월 10일
편집: Walter Roberson 2017년 8월 10일
in same code i have error occured in that line;
feature_vec = evectors' * (input_image(:) - mean_face);
**Matrix dimensions must agree.**
  댓글 수: 6
Neelesh Patel
Neelesh Patel 2017년 8월 10일
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
Walter Roberson 2017년 8월 10일
"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
Neelesh Patel 2017년 8월 16일
Hello sir Plzz help me for this error:
feature_vec = evectors' * (input_image(:) - mean_face); Error using - Matrix dimensions must agree.
  댓글 수: 1
Walter Roberson
Walter Roberson 2017년 8월 16일
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.

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by