matlab练习程序(PCA

时间:2022-02-15 03:51:44

clear all;
close all;
clc;
img1=imread('Corner.png');
img2=imread('Corner1.png');
img3=imread('Corner2.png');

img1=imresize(img1,[35 90]); %Matlab的svd不支持太大的数据,我把数据变小了.
img2=imresize(img2,[35 90]);
img3=imresize(img3,[35 90]);

[height width]=size(img2);
a=reshape(double(img1),1,35*90);
b=reshape(double(img2),1,35*90);
c=reshape(double(img3),1,35*90);

ma=mean(a);
mb=mean(b);
mc=mean(c);

a=a-ma;
b=b-mb;
c=c-mc;

x=[a' b' c'];
x=double(x);
%y=x'*x/(height*width);

%[v d]=eig(y);
[u s w]=svd(x); %w相当于排序后的d,用svd就不用求x'*x了

ed=x*w;%v;

img4=zeros(height,width);
img4=reshape(ed(:,1),height,width);

img5=zeros(height,width);
img5=reshape(ed(:,2),height,width);

img6=zeros(height,width);
img6=reshape(ed(:,3),height,width);

imshow(mat2gray(img4));
figure,imshow(mat2gray(img5));
figure,imshow(mat2gray(img6));

参考:

1.http://iiec.cqu.edu.cn/wiki/index.php/SVD%E4%B8%8EPCA%E7%9A%84%E7%93%9C%E8%91%9B