用MATLAB对一图象分别用不同小波分解,观察高低频部分进行重构,比较重构误差,再进行阈值消噪,比较消噪前后图象。
- load noiswom; %装载图像信号
- whos
- [swa,swh,swv,swd] = swt2(X,1,'db1'); %完成图像的单层次小波分解
- whos %观察各个系数向量的结构
- figure(1);
- map = pink(size(map,1)); %显示低频和高频系数
- colormap(map)
- subplot(2,2,1), image(wcodemat(swa,192));
- title('Approximation swa')
- subplot(2,2,2), image(wcodemat(swh,192));
- title('Horiz. Detail swh')
- subplot(2,2,3), image(wcodemat(swv,192));
- title('Vertical Detail swv')
- subplot(2,2,4), image(wcodemat(swd,192));
- title('Diag. Detail swd')
- A0 = iswt2(swa,swh,swv,swd,'db1'); %通过平稳小波逆变换重构图像
- err = max(max(abs(X-A0))) %检查重构误差
- nulcfs = zeros(size(swa)); %从第三步中产生的系数swa、swh、swv和swd构造第一层的低频和高频(A1、H1、V1和D1)部分
- 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,192));
- title('Approximation A1')
- subplot(2,2,2), image(wcodemat(H1,192));
- title('Horiz. Detail H1')
- subplot(2,2,3), image(wcodemat(V1,192));
- title('Vertical Detail V1')
- subplot(2,2,4), image(wcodemat(D1,192));
- title('Diag. Detail D1')
- [swa,swh,swv,swd] = swt2(X,3,'db1'); %采用db1来完成多尺度二维离散平稳小波分解
- clear A0 A1 D1 H1 V1 err nulcfs %观察swa,swh,swv,swd的存储结构
- whos
- figure(3); %显示多尺度二维平稳小波分解结果
- colormap(map)
- kp = 0;
- for i = 1:3
- subplot(3,4,kp+1), image(wcodemat(swa(:,:,i),192));
- title(['Approx. cfs level ',num2str(i)])
- subplot(3,4,kp+2), image(wcodemat(swh(:,:,i),192));
- title(['Horiz. Det. cfs level ',num2str(i)])
- subplot(3,4,kp+3), image(wcodemat(swv(:,:,i),192));
- title(['Vert. Det. cfs level ',num2str(i)])
- subplot(3,4,kp+4), image(wcodemat(swd(:,:,i),192));
- title(['Diag. Det. cfs level ',num2str(i)])
- kp = kp + 4;
- end
- mzero = zeros(size(swd)); %从系数中重构第3层的低频信号
- A = mzero;
- A(:,:,3) = iswt2(swa,mzero,mzero,mzero,'db1');
- H = mzero; V = mzero; %重构第1、2、3层的高频信号
- 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
- A(:,:,2) = A(:,:,3) + H(:,:,3) + V(:,:,3) + D(:,:,3); %通过第3层的低频部分和第1、2、3层的高频部分重构第1、2层的低频部分
- 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),192));
- title(['Approx. level ',num2str(i)])
- subplot(3,4,kp+2), image(wcodemat(H(:,:,i),192));
- title(['Horiz. Det. level ',num2str(i)])
- subplot(3,4,kp+3), image(wcodemat(V(:,:,i),192));
- title(['Vert. Det. level ',num2str(i)])
- subplot(3,4,kp+4), image(wcodemat(D(:,:,i),192));
- title(['Diag. Det. level ',num2str(i)])
- kp = kp + 4;
- end;
- thr = 44.5; %阈值消噪
- sorh = 's';
- dswh = wthresh(swh,sorh,thr);
- dswv = wthresh(swv,sorh,thr);
- dswd = wthresh(swd,sorh,thr);
- clean = iswt2(swa,dswh,dswv,dswd,'db1');
- thr = 44.5;
- sorh = 's';
- dswh = wthresh(swh,sorh,thr);
- dswv = wthresh(swv,sorh,thr);
- dswd = wthresh(swd,sorh,thr);
- clean = iswt2(swa,dswh,dswv,dswd,'db1');
- figure(5); %对比消噪前后的图像
- colormap(map)
- subplot(1,2,1), image(wcodemat(X,192));
- title('Original image')
- subplot(1,2,2), image(wcodemat(clean,192));
- title('De-noised image')
- end
复制代码 |