【发布时间】:2014-09-10 16:25:43
【问题描述】:
我有一个错误,说:
下标分配维度不匹配。
Error in facerecognition (line 14) images(:, n) = img(:);
有人可以帮忙吗?我写的代码如下:
input_dir = 'D:\C.S\FYP\Matlab Projects\DIP Applications\Face recognition\Faces\';
image_dims = [48, 64];
filenames = dir(fullfile(input_dir, '*.jpg'));
num_images = numel(filenames);
images = [];
for n = 1:num_images
filename = fullfile(input_dir, filenames(n).name);
img = imread(filename);
if n == 1
images = zeros(prod(image_dims), num_images);
end
images(:, n) = img(:);
end
% 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] = princomp(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
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))
;
【问题讨论】:
-
我有一个错误,说下标分配维度不匹配。人脸识别错误(第 14 行) images(:, n) = img(:);任何人都可以帮忙吗?谢谢
标签: matlab image-processing pca