特征面在matlab中显示空缺图像

特征面在matlab中显示空缺图像,matlab,Matlab,我有一套17张脸的灰度图片,当我试图查看它时,我会得到一张黑色的图片,而不是像幽灵一样的图片 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, filena

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

    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;

为了查看这些值,我使用了这个代码

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

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

你应该检查代码中的每个步骤,并确保它们通过了健全性检查。我猜是这样

features = evectors' * shifted_images;
应该是这个吗

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

princomp(images');
尝试缩放图像:

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

移位图像的尺寸是多少Sam不确定这是否是你需要的…但是matlab说(3600X17),我有17个训练图像。我试过了,我得到了这个错误,使用*内部矩阵尺寸必须一致<代码>PCA(第32行)特征中的错误=移位的图像*向量