如何对图像应用主成分分析

How do i apply principal component analysis on an image

我正在尝试将 pca 应用于图像。分配新的 W 轴作为第一主成分,第二主成分作为 P 轴。在 W–P 轴上,图像被重新绘制。谁能告诉我怎么做?

我尝试了以下代码,但无法继续。请帮忙。

I2=;%grayscale image
arr=[];
for i=1:size(I2,1)
  for j=1:size(I2,2)
    arr=[arr;i,j,I2(i,j)];
  end
end
c=pca(arr);
c=c(:,1:2);%i select the first two components here. How do i proceed now

在PCA特征值矩阵中,最大的特征值代表最突出的特征,例如鼻子。考虑到这一点,定位这些值很简单,只需重塑图像以生成协方差矩阵并计算特征值并提取正确的特征向量列。完成后,您再次重塑矩阵并显示它..

下面附上基本示例代码:

% Reshape the image into a row vector where each row
% is placed next to the one above it. 
MD_image1_Row = reshape(MD_image1.',1,[]);

% multiplying the the row matrix with its transpose to form a very large
% matrix
cov_matrix = (1/3).*(MD_image1_Row * MD_image1_Row .');

% finding the eigenvalues and eigenvectors of the matrix formed above
[Vector, Value] = eig(cov_matrix);

% by looking at the eigenvalue matrix the three (depending on the image)
% largest eigenvalues are located and then the corresponding column in the 
% Eigenvector matrix is taken and manipulated to produce the Eigenface

% Extracting Eigenvector column
Eig_Vector1 = Vector(:,2803); %value is example

% reshaping Eigenvector into a matrix with the same dimensions as the
% original image
Eig_1_Matrix = reshape(Eig_Vector1.', 51,55); %value is example

% checking size of the EigenMatrix - this is to check the new image has the 
% same size as the original images
EigenMatrix = size(Eig_1_Matrix)

% displaying EigenMatrix image using specific limits so that the images are
% displayed correctly
figure, CC = imshow(Eig_1_Matrix,...
                    [min(Eig_1_Matrix(:)),...
                    max(Eig_1_Matrix(:))]);

提示:需要考虑主体的光照强度,还需要考虑图像方向才能正确显示。

一旦你确定了你想要的特征并提取了正确的特征向量列,你就可以标记它们并做任何你想做的事情。