Eigenface Face Recognition: Why do I keep getting "Subscripted assignment dimension mismatch"?
Ältere Kommentare anzeigen
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.
Akzeptierte Antwort
Weitere Antworten (2)
Neelesh Patel
am 10 Aug. 2017
Bearbeitet: Walter Roberson
am 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 Kommentare
Walter Roberson
am 10 Aug. 2017
input_image is not the same size as the images you trained on.
Neelesh Patel
am 10 Aug. 2017
Bearbeitet: Walter Roberson
am 10 Aug. 2017
hello sir i m trying to execute this code:
%%Loading the Images
input_dir ='E:\TARGET FOLDER';
image_dims = [64, 30];
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, map] = imread(filename);
if isempty(img)
error('Somehow file "%s" has an empty image!', filename);
end
if ~isempty(map)
error('Somehow file "%s" is a pseudocolor JPEG image, which is not possible in standard JPEG!', filename);
end
if ndims(img) > 2
img = zeros(image_dims);
% warning('File "%s" was an RGB JPEG image (as nearly all JPEG images are), but the code expects it to one of the rare grayscale JPEG images. Substituting black image instead.', filename);
else
img = im2double(imresize(img, image_dims));
img = rgb2gray(img)
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;
% calculate the similarity of the input to each training image
input_image = imread('E:\TARGET FOLDER\0.jpg');
input_image = imresize(input_image,[64, 30]);
input_image = im2double(input_image);
input_image = rgb2gray(input_image);
input_image = size(input_image);
feature_vec = evectors' * (input_image(:) - mean_face);
i m getting error in last step of this code
plzzz plzz help to solve it out....thanx sir in advance
Walter Roberson
am 10 Aug. 2017
What are you expecting the line
input_image = size(input_image);
to do for you?
Neelesh Patel
am 10 Aug. 2017
Because of this error i have used that size function plzz sir solve it out because i m hunger for the result......
feature_vec = evectors' * (input_image(:) - mean_face); Error using * Inner matrix dimensions must agree.
Neelesh Patel
am 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
am 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
am 16 Aug. 2017
0 Stimmen
Hello sir Plzz help me for this error:
feature_vec = evectors' * (input_image(:) - mean_face); Error using - Matrix dimensions must agree.
1 Kommentar
Walter Roberson
am 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.
Kategorien
Mehr zu Image Category Classification finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!