% 装载图像信号
load belmont2;
% 完成图像的单层次小波分解
[swa,swh,swv,swd] = swt2(X,1,'db1');
% 显示低频和高频系数
map = pink(size(map,1));
figure(1)
colormap(map)
nbcol = size(map,1)
subplot(2,2,1), image(wcodemat(swa,nbcol));
title('低频系数swa')
subplot(2,2,2), image(wcodemat(swh,nbcol));
title('水平方向高频系数swh')
subplot(2,2,3), image(wcodemat(swv,nbcol));
title('垂直方向高频系数swv')
subplot(2,2,4), image(wcodemat(swd,nbcol));
title('对角方向高频系数swd')
% 通过平稳小波逆变换重构图像
A0 = iswt2(swa,swh,swv,swd,'db1');
nulcfs = zeros(size(swa));
% 由分解系数重构第一层的低频和高频部分
A1 = iswt2(swa,nulcfs,nulcfs,nulcfs,'db1');
H1 = iswt2(nulcfs,swh,nulcfs,nulcfs,'db1');
V1 = iswt2(nulcfs,nulcfs,swv,nulcfs,'db1');
D1 = iswt2(nulcfs,nulcfs,nulcfs,swd,'db1');
% 显示第一层的分解结果,包括显示低频和高频部分
figure(2)
colormap(map)
subplot(2,2,1), image(wcodemat(A1,nbcol));
title('低频 A1')
subplot(2,2,2), image(wcodemat(H1,nbcol));
title('水平高频H1')
subplot(2,2,3), image(wcodemat(V1,nbcol));
title('垂直高频V1')
subplot(2,2,4), image(wcodemat(D1,nbcol));
title('对角高频D1')
% 图像的多层二维离散平稳小波分解
[swa,swh,swv,swd] = swt2(X,3,'db1');
% 显示多层二维离散平稳小波分解结果
figure(3)
colormap(map)
kp = 0;
for i = 1:3
subplot(3,4,kp+1), image(wcodemat(swa(:,:,i),nbcol));
title(['低频系数:level ',num2str(i)])
subplot(3,4,kp+2), image(wcodemat(swh(:,:,i),nbcol));
title(['水平高频系数:level ',num2str(i)])
subplot(3,4,kp+3), image(wcodemat(swv(:,:,i),nbcol));
title(['垂直高频系数:level ',num2str(i)])
subplot(3,4,kp+4), image(wcodemat(swd(:,:,i),nbcol));
title(['对角高频系数:level ',num2str(i)])
kp = kp + 4;
end
% 从系数中重构第3层的低频信号
mzero = zeros(size(swd));
A = mzero;
A(:,:,3) = iswt2(swa,mzero,mzero,mzero,'db1');
% 由系数重构第1、2、3层的高频信号
H = mzero; V = mzero;
D = mzero;
for i = 1:3
swcfs = mzero; swcfs(:,:,i) = swh(:,:,i);
H(:,:,i) = iswt2(mzero,swcfs,mzero,mzero,'db1');
swcfs = mzero; swcfs(:,:,i) = swv(:,:,i);
V(:,:,i) = iswt2(mzero,mzero,swcfs,mzero,'db1');
swcfs = mzero; swcfs(:,:,i) = swd(:,:,i);
D(:,:,i) = iswt2(mzero,mzero,mzero,swcfs,'db1');
end
% 重构第1、2层的低频部分
A(:,:,2) = A(:,:,3) + H(:,:,3) + V(:,:,3) + D(:,:,3);
A(:,:,1) = A(:,:,2) + H(:,:,2) + V(:,:,2) + D(:,:,2);
figure(4)
% 显示第1、2、3层的低频和高频部分
colormap(map)
kp = 0;
for i = 1:3
subplot(3,4,kp+1), image(wcodemat(A(:,:,i),nbcol));
title(['低频:level ',num2str(i)])
subplot(3,4,kp+2), image(wcodemat(H(:,:,i),nbcol));
title(['水平高频:level ',num2str(i)])
subplot(3,4,kp+3), image(wcodemat(V(:,:,i),nbcol));
title(['垂直高频:level ',num2str(i)])
subplot(3,4,kp+4), image(wcodemat(D(:,:,i),nbcol));
title(['对角高频:level ',num2str(i)])
kp = kp + 4;
end