如何在图像上应用主成分分析

时间:2015-04-27 14:23:02

标签: matlab image-processing computer-vision pca

我正在尝试在图像上应用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

1 个答案:

答案 0 :(得分:5)

在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(:))]);

提示:需要考虑拍摄对象的光线强度,还需要考虑图像方向以正确显示。

一旦确定了您想要的特征并提取了正确的特征向量列,就可以标记它们并按照您的意愿行事。