0

i have a set of 17 face grayscale pictures..and when try to view it i get a black images instead of ghost like pictures.

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;

and to see the eignevalues i used this code

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

i dont think it was suppose to be like this. can someone point out what i did wrong?

4

2 回答 2

0

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

features = evectors' * shifted_images;

应该是这个

features = shifted_images * evectors;

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

princomp(images');
于 2013-05-11T21:26:31.943 回答
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
于 2013-05-13T04:22:20.060 回答