【问题标题】:Face recognition in MATLABMATLAB中的人脸识别
【发布时间】: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


【解决方案1】:

该错误似乎是由于您在开始时指定的图像尺寸与从您目录中的图像中实际读取的尺寸不一致而引起的。我建议您在读取第一张图像时动态读取图像尺寸。顺便说一句,这将在您的目录中所有图像的尺寸相同的情况下才有效。此外,这是假设您所有的图像都是灰度的。但是,如果图像大小不同,我可以将代码放在适当的位置,我们可以使用第一张图像作为参考尺寸。如果任何图片的尺寸与您读入的第一张图片不同,我们将调整图片大小以使其符合该尺寸。

如果您有任何彩色图像,您需要先将它们转换为灰度,然后再运行代码。因此,将您的第一个 for 循环代码(您在图像中读取的位置)修改为如下所示。这基本上是您的代码,但在我修改的任何地方,您都会在我放置的每一行代码旁边看到一个%// NEW 语句:

image_dims = []; %// NEW - Set to this instead of [48,64]

for n = 1:num_images

    filename = fullfile(input_dir, filenames(n).name);
    img = imread(filename);
    if size(img,3) == 3 %// NEW - Convert to gray scale if necessary
        img = rgb2gray(img); %// Use rgb2gray if colour
    end
    if n == 1
        image_dims = size(img); %// NEW - Read in image dimensions here
        images = zeros(prod(image_dims), num_images);
    else
        %// NEW - If the image read in is not the same dimensions
        %// as the first image read in, resize the image accordingly
        if size(img,1) ~= image_dims(1) || size(img,2) ~= image_dims(2)
            img = imresize(img, image_dims, 'bilinear');
        end
    end

    images(:, n) = img(:);
end

【讨论】:

  • 我认为之前的问题已经解决了,谢谢。现在我遇到问题'错误使用 * 内矩阵尺寸必须一致。人脸识别错误(第 32 行)特征 = evectors * shift_images;'
  • @Anonymous - 您忘记了 ' 字符。在您的原始代码中,evectors 被转置。因此,请执行以下操作:features = evectors' * shifted_images;.
  • @Anonymous - evectorsshifted_images 的大小是多少?使用size 并为我打印出这两个变量。该乘法不起作用,因为矩阵的维度不兼容。
  • 这是尺寸。 >>人脸识别向量 68,shifted_images 20 向量 530432,shifted_images 68
  • @Anonymous - 好的,请为我执行此操作:[rows1 cols1 dim1] = size(evectors) [rows2 cols2 dim2] = size(shifted_images)。打印出所有这些变量,然后将它们放在这里。
【解决方案2】:

错误表示images(:, n)的大小与img(:)的大小不一样。您应该在该行放置一个断点,并找出原因。

【讨论】:

    猜你喜欢
    • 2012-02-13
    • 2014-07-20
    • 2011-12-14
    • 1970-01-01
    • 2022-06-24
    • 2015-03-17
    • 2018-06-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多