MATLAB: Eigenface Face Recognition: Why do I keep getting “Subscripted assignment dimension mismatch”

eigenfacesubscripted assignment dimension mismatch

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.

Best Answer

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.
Related Question