📄 clrssimulation.m
字号:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Copy Right: the Computational Intelligence Laboratory
%% System name: PCA
%% File name: CLRSSimulation.m
%% Description: This is a simulation of the CRLS neural network.The first 4 principal
%% omponents will be extracted, and then the original image is reconstructed.
%%
%%
%% Author:
%% Date: 4/14/2004
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear all;
originalImage = imread('lina.bmp');
preserveOrigianl = originalImage;
[rr, cc] = size(originalImage);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% the image is digitized into the 64x4096 matrix.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Img = originalImage;
mc=256;
r = 8; c = 8 ; p = r*c ;
originalImage = blkM2vc(Img, [r c]) ;
[p N] = size(originalImage) ;
Xm = mean(originalImage')' ;
Xmax=max(max(abs(originalImage)));
originalImage = originalImage - Xm(:, ones(1, N)) ;
originalImage = originalImage/max(max(abs(originalImage))) ;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% the original image is showed.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
XX=originalImage*Xmax;
XX=XX+Xm(:, ones(1, N)) ;
Imr = vc2blkM(XX, r, rr) ;
Imr = round(mc*Imr/max(max(Imr))) ;
figure(1), set(1,'Name','the original picture'), image(Imr), colormap(gray(mc))
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% the 4 neurons network is trained until it converge.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
m=4;
time(m)=rem(Now,1);
W = 0.6*(rand(m, p)-0.5) ; % weight initialisation
er = .0001 ; % the length convergence error
e0=originalImage;
for j=1:m
for s=1:16
[rs rn] = sort(rand(1, N)) ;
for n=1:N
if n==1
eta(j)=sum(sum(e0.^2))/N;
end
x = e0(:, rn(n)) ; % randomised selection of patterns
y = W(j,:)*x ;
eta(j)=y^2+eta(j);
dW(j,:)=(y/eta(j))*(x' - W(j,:)*y);
W(j,:)=W(j,:)+dW(j,:);
if sqrt(sum(dW(j,:).^2))<er
break;
end
end
if sqrt(sum(dW(j,:).^2))<er
break;
end
end
y=W(j,:)*e0;
e0=e0-W(j,:)'*y;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% extracting the first 4 principal component.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
yy = W*originalImage ;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% the principal component is showed.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[rTem,cTem] = size(yy);
tV = rr/8;
for j = 1:rTem
rVc = 256*yy(j,:);
Imr = eigvcToMask(rVc,tV);
Imr = round(mc*Imr/max(max(Imr))) ;
Imr = round(Imr) ;
tempPicNo = 1+j;
strTitle = strcat('the extracted first ', int2str(j), ' Principal component');
figure(tempPicNo),set(tempPicNo,'Name',strTitle), image(Imr), colormap(gray(mc))
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% the wiegh vector mask is showed.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[rTem,cTem] = size(W);
tV = 64/8;
for j = 1:rTem
rVc = 256*W(j,:);
Imr = eigvcToMask(rVc,tV);
Imr = round(mc*Imr/max(max(Imr))) ;
Imr = round(Imr) ;
tempPicNo = 5+j;
strTitle = strcat('the first ', int2str(j),' weight vector mask');
figure(tempPicNo),set(tempPicNo,'Name',strTitle), image(Imr), colormap(gray(mc))
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% the image will be reconstructed.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Xr = W'*yy;
Xr=Xr*Xmax;
Xr = Xr + Xm(:, ones(1, N)) ;
Imr = vc2blkM(Xr, r, rr) ;
Imr = round(mc*Imr/max(max(Imr))) ;
Imr = round(Imr) ;
figure(10), set(10,'Name','the reconstructed picture'),image(Imr), colormap(gray(mc))
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% this is end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -