特征脸在matlab中显示空白图像

时间:2013-05-11 20:30:42

标签: matlab

我有一组17张面部灰度图片。当我试图查看它时,我得到的是黑色图像,而不是像图片一样的幽灵。

matlab image

    input_dir = 'images';
image_dims = [60, 60];

filenames = dir(fullfile(input_dir, '*.jpg'));
num_images = numel(filenames);
images = [];
for n = 1:num_images
    filename = fullfile(input_dir, filenames(n).name);
    img = imresize(imread(filename),[60,60]);
    if n == 1
        images = zeros(prod(image_dims), num_images);
    end
    images(:, n) = img(:);
end

% Trainig

% 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;

并查看我使用此代码的eignevalues

figure;
for n = 1:num_eigenfaces
    subplot(2, ceil(num_eigenfaces/2), n);
    evector = reshape(evectors(:,n), image_dims);
    imshow(evector);
end

我认为这不应该是这样的。谁能指出我做错了什么?

3 个答案:

答案 0 :(得分:0)

您应该检查代码中的每一步,并确保它们通过完整性检查。我的猜测是这个

features = evectors' * shifted_images;

应该是这个

features = shifted_images * evectors;

这让我想知道shifted_images是否具有正确的尺寸。 evectors应该是一个矩阵,其中每列代表一个分量向量。矩阵将是[pics x n]。移位的图像应该是[pixcount x pics]矩阵。 “pixcount”是每个图片中的像素量,“pics”是图片的数量。如果evectors' * shifted_images在没有尺寸错误的情况下工作,我想知道是否有一个数量没有正确计算。我认为这个转置是罪魁祸首:

princomp(images');

答案 1 :(得分:0)

我看到了类似的帖子,答案已经回答[link] eigenfaces are not showing correctly and are very dark

答案 2 :(得分:0)

尝试缩放图像:

for i=1:num_eigenfaces
    subplot(1,7,i);
    image=reshape(evectors(:,i), image_dims);
    image=image';
    %scale image to full scale
    imshow(image, []);
end