Extract the principal components of the PCA transformed image (matlab code)

Reference:
https://blog.csdn.net/duanyule_cqu/article/details/54975867
This article mainly uses PCA to process an image, and extract and display the principal components of the image after PCA transformation.
The code is directly pasted here. I believe everyone has read a lot about the PCA principle. In fact, if you look at it and don’t practice a specific purpose, there are always some concepts that are not very clear.

Features of PCA for Image Processing

*Question:
Does the principal component ranked in the front after PCA transformation have no noise?
I don't think so. That is to say, the principal components in the front are not necessarily very "pure".
Through PCA transformation, the useful information in multi-band images can be concentrated into new principal component images with as few as possible, and these principal component images can be independent of each other, thus greatly reducing the total amount of data. However, PCA transformation is more sensitive to noise, that is, the principal component with a large amount of information, the signal-to-noise ratio (the ratio of signal to noise) is not necessarily high, when the variance of the noise contained in a principal component with a large amount of information is greater than the variance of the signal The image quality formed by the principal component component is poor. PCA transformation is not used for fusion processing to reduce noise, but through this transformation, the multispectral image has statistical independence in each band, that is, the data between these bands is mutually independent. are not relevant, so that the corresponding fusion strategies can be adopted respectively.

Extract and display the principal components of PCA

main code

>> mul = imread('PCA/low.jpg');
>> extract_pca(mul);

Code to implement PCA

function [vector ,value,tempMul] = my_pca(mul)
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % 
% % % 该函数用来实现多光谱图像的PCA算法
% % %    param:
% % %          mul--输入的多光谱图像也可以是多通道的
% % %          value--特征值从大到小排列
% % %          vector--特征向量对应特征值排列,一列是一个特征向量
% % %          tempMul--mul的reshape成pixels*bands形式的2维矩阵
% % % 
% % %  @author:chaolei
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % 
mul=double(mul)/255;
[r ,c ,bands]=size(mul);
pixels = r*c;
% reshape成pixels*channel
mul = reshape(mul, [pixels,bands]);
tempMul = mul;
% 求各通道的均值
meanValue =  mean(mul,1);

% 数据去中心化
mul = mul - repmat(meanValue,[r*c,1]);
% 求协方差矩阵
correlation = (mul'*mul)/pixels;
%求特征向量与特征值
[vector ,value] = eig(correlation);
% 特征值和特征向量从大到小排序
vector = fliplr(vector);
value = fliplr(value);
value = flipud(value);

end

Extract and display the principal components of PCA

function extract_pca(mul)
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % 
% % % 该函数用来抽取并显示pca的各个主成分
% % %   param:
% % %       mul--输入图像是多光谱图像或者高光谱图像
% % %  @author:chaolei
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % 
[r,c,bands]=size(mul);
[vector,~,tempMul]= my_pca(mul);
% Y=AX(X中列为样本,若X行为样本,则Y =XA)
% PCA正变换
PC = tempMul*vector; 
% 提取多光谱图像的各个主成分
for i = 1:bands
    outPic = PC(:,i);
    min_value = min(outPic);
    max_value = max(outPic);
    outPic = reshape(outPic,[r,c]);
    figure;
    str = sprintf('%s%d%s','第',i,'主成分');
    imshow(outPic,[min_value,max_value]);title(str);
%     filename = sprintf('%d%s',i,'.jpg');
%     imwrite(outPic,filename);
end
end

result

write picture description here
write picture description here
write picture description here
write picture description here

Implement PCA for compression

Originally, I didn't want to write this section, but I feel that I have never been clear about using PCA for compression. So let's go over it and write a little bit of code. As can be seen from the above analysis, since the image I input is 3-channel, that is, there are only 3 features, there are only 3 principal components.
So what we need to think about is how to perform image compression? For this example, the misunderstanding of the previous understanding is that even if I select 2 or 1 principal components, the image returned by my PCA inverse transformation is still the same size as the original image. So what is this called compression?
Later, in the process of helping Da Jinming to extract the principal components of PCA, he understood the concept of PCA compression. Suppose we select the first two principal components. It can be seen from the figure that these two principal components have included most of the information. Then our PCA compression implementation is to store these two principal components, and store the feature vectors corresponding to the two principal components. In this way, we actually store about one channel less data of the original image. That is, compression is achieved. Paste the code below.

function  pca_comp_show(mul,n)
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % 
% % % 该函数用来显示经过PCA压缩后的图像
% % %  param:
% % %     mul--输入的多光谱或者高光谱图像  
% % %     n--指定用多少个主成分
% % % @author:chaolei
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % 
[vector ,value,tempMul] = my_pca(mul);
% 使用其中最重要的n个主成分,并还原到原图像大小
% PCA逆变换
% 原始数据reshape*特征向量矩阵*特征向量矩阵'
re = tempMul*vector(:,1:n)*vector(:,1:n)';
[r,c,bands] =size(mul);
comp = reshape(re,[r,c,bands]);

str =sprintf('%d%s',n,'个主成分');
figure;imshow(comp);title(str);
end

write picture description here

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325877699&siteId=291194637