为什么第一个主成分在我的PCA中显示出最小的差异?

时间:2016-02-15 18:25:15

标签: matlab image-processing pca eigenvector eigenvalue

我在MatLab的一组面部图像上使用PCA。 创建一个平均面部并随机化其他人工作正常。

在我的函数vectorComparison中,我希望在使用标准偏差时看到每个主成分矢量的差异。但是当我使用eig_face_index = 1时,我看到的差异比使用2或3等时少。

较高的索引似乎也会增加更多的颜色,这可能是由于特征脸中的噪音,因为我正在使用RGB空间。

为什么我的初始矢量显示差异最小。不应该反过来吗?

以下是我正在使用的所有代码:

的main.m

clear;clc;close all;
[imvecs,img] = loadImages();
meanval = meanValue(imvecs);
[T, D] = covarianceMatrix(imvecs, meanval);
[eigvecs, eigvals] = findEigVecs(imvecs, T, D);
eigenfaces = createEigenFaces(eigvecs, imvecs, img); 

%%
[mean_image] = createAverageFace(meanval, img);
%%
[stdev_vec] = createRandomFace(eigvals, eigvecs, imvecs, meanval, img);
%%
vectorComparison(meanval, eigvecs, stdev_vec, img, mean_image);

loadImages.m

function [imvecs,img] = loadImages()
images = dir('D:My\Path\*.png');  
imgPath = 'D:My\Path\';
img=imread([imgPath images(1).name]);
n=length(images);

for i = 1:n
    img = imread([imgPath images(i).name]);
    imvecs{i} = double(img(:));
end
return

meanValue.m

function meanval = meanValue(imvecs, imageNr)
    %Creates the mean value from our images.
    sumvec=imvecs{1};

    for i = 2:(size(imvecs,2))
        sumvec = sumvec + imvecs{i};
    end
    meanval = sumvec ./(size(imvecs,2));
return

covarianceMatrix.m

function [T, D] = covarianceMatrix(imvecs, meanval)
    D = [];
    for i = 1:size(imvecs,2),
       diff = imvecs{i} - meanval;
       D = [D, diff];
    end
    %Dimensionality reduction
    T = (D' * D) ./ (size(imvecs,2));
return

findEigVecs.m

function [eigvecs, eigvals] = findEigVecs(imvecs, T, D)
    [U,eigvals,V] = svd( T );
    eigvecs = [];
    for i = 1:size(imvecs,2),
        eigvec = D * U(:,i);
        eigvec = eigvec ./ sum(eigvec);
        eigvecs = [eigvecs, eigvec];
    end
return

createEigenFaces.m

function [eigenfaces] = createEigenFaces(eigvecs, imvecs, img)
    for i = 1:size(imvecs,2),
        eigface =  reshape(eigvecs( : , i), size(img));
        eigface = eigface - min(min(min((eigface))));
        eigface = eigface ./ max(max(max((eigface))));
        eigenfaces{i}=eigface;
        %figure;imagesc(eigface);
    end
return

createAverageFace.m

function [mean_image] = createAverageFace(meanval, img)
    mean_image = reshape(meanval, size(img));
    figure;imagesc(mean_image./255);
    title('Average Face')
return

createRandomFace.m

function [stdev_vec] = createRandomFace(eigvals, eigvecs, imvecs, meanval, img)
    stdev_vec = sqrt(diag(eigvals));
    t = (100 * rand(size(imvecs,2),1) - 50) .* stdev_vec;
    new_face1 = meanval + (eigvecs * t);
    new_face1 = reshape(new_face1, size(img));

    figure;imagesc(new_face1./255);
    title('Random Face')
return

vectorComparison.m

function [] = vectorComparison(meanval, eigvecs, stdev_vec, img, mean_image)
    t = zeros(17,1);
    eig_face_index = 1;
    t(eig_face_index) = 1000;
    t = t.*stdev_vec;

    new_face1 = meanval + (eigvecs * t);
    new_face1 = reshape(new_face1, size(img));

    new_face2 = meanval - (eigvecs * t);
    new_face2 = reshape(new_face2, size(img));

    figure;
    title('PCA Comparison')
    subplot(3,1,1), subimage(new_face1./255)
    subplot(3,1,2), subimage(mean_image./255)
    subplot(3,1,3), subimage(new_face2./255)
return

1 个答案:

答案 0 :(得分:1)

我发现这里有什么问题。 在我的函数 findEigVals 中,我通过使用向量本身的 sum 来使每个向量成为一个单位向量(长度为1)。只要向量的内容确实是正条目的范围,这是可能的。 但是,由于我们无法知道我们是否有pos或neg方向的向量(两者同样有效),我们不能在这里使用 sum

相反,我们需要用 norm 替换它,这是matlab规范归一化的方式。

function [eigvecs, eigvals] = findEigVecs(imvecs, T, D)
    [U,eigvals,V] = svd( T );
    eigvecs = [];
    for i = 1:size(imvecs,2),
        eigvec = D * U(:,i);
        eigvec = eigvec ./ norm(eigvec);
        eigvecs = [eigvecs, eigvec];
    end
return

如果有人使用上面代码的版本,请注意随机面部函数将在rgb-space中给出太强的值。 将 createRandomFace 替换为以下内容:

stdev_vec = sqrt(diag(eigvals));
min_range = 0;
max_range = 2;
t = ((max_range - min_range)*rand(n,1)) .* stdev_vec + min_range;
new_face1 = meanval + (eigvecs * t);
new_face1 = reshape(new_face1, size(img));

figure;imagesc(new_face1./255);