# 【图像去噪】基于matlab邻域的小波图像去噪【含Matlab源码 1188期】

·  阅读 223

## 二、源代码

``````%基于邻域的小波去噪,窗口选择分别为3*3和5*5
%----------------------------------------------------------
%（1）基于邻域的小波去噪,窗口选择为3*3时
%----------------------------------------------------------
clear;
fnoisy=imnoise(f,'gaussian',0,0.01);%填加高斯白噪声
figure,subplot(121),imshow(f,[]),title('原图像')
subplot(122),imshow(fnoisy,[]),title('加噪图像')

%对图像进行三层小波分解
[c,s]=wavedec2(fnoisy,3,'db8');

[cH2,cV2,cD2]=detcoef2('all',c,s,2);
[cH1,cV1,cD1]=detcoef2('all',c,s,1);
delta=median(abs(cD1(:)))/0.6745;
n=size(f,1);
thr=delta*sqrt(2*log(n^2));%阈值
%对各级的H，V，D分量进行处理
%mov_win是滑窗函数，根据邻域窗口内小波系数的平方和大小来决定窗口中心的系数是收缩还是置零
cH3=mov_win(cH3,3,thr);
cH2=mov_win(cH2,3,thr);
cH1=mov_win(cH1,3,thr);
cV3=mov_win(cV3,3,thr);
cV2=mov_win(cV2,3,thr);
cV1=mov_win(cV1,3,thr);
cD3=mov_win(cD3,3,thr);
cD2=mov_win(cD2,3,thr);
cD1=mov_win(cD1,3,thr);

m1=size(cA3,1);m2=size(cA3,2);
m=m1*m2;
dnc0(1:m)=cA3(:);

m1=size(cH3,1);m2=size(cH3,2);
m=m1*m2;
dnc1(1:m)=cH3(:);

m1=size(cH2,1);m2=size(cH2,2);
m=m1*m2;
dnc2(1:m)=cH2(:);

m1=size(cH1,1);m2=size(cH1,2);
m=m1*m2;
dnc3(1:m)=cH1(:);

m1=size(cV3,1);m2=size(cV3,2);
m=m1*m2;
dnc4(1:m)=cV3(:);

m1=size(cV2,1);m2=size(cV2,2);
m=m1*m2;
dnc5(1:m)=cV2(:);

m1=size(cV1,1);m2=size(cV1,2);
m=m1*m2;
dnc6(1:m)=cV1(:);

m1=size(cD3,1);m2=size(cD3,2);
m=m1*m2;
dnc7(1:m)=cD3(:);

m1=size(cD2,1);m2=size(cD2,2);
m=m1*m2;
dnc8(1:m)=cD2(:);

m1=size(cD1,1);m2=size(cD1,2);
m=m1*m2;
dnc9(1:m)=cD1(:);

dnc10=[dnc0 dnc1 dnc4 dnc7 dnc2 dnc5 dnc8 dnc3 dnc6 dnc9];
dnc=waverec2(dnc10,s,'db8');%重构
dnc=uint8(dnc);
figure,subplot(121),imshow(dnc,[]),title('3*3邻域去噪后图像')

f=double(f);
dnc=double(dnc);
psnr3=cacupsnr(f,dnc)

%----------------------------------------------------------
%（2）基于邻域的小波去噪,窗口选择为5*5
%----------------------------------------------------------
fnoisy=imnoise(f,'gaussian',0,0.01);%填加高斯白噪声

%对图像进行三层小波分解
[c,s]=wavedec2(fnoisy,3,'db8');
cA3=appcoef2(c,s,'db8',3);%低频分量

thr=delta*sqrt(2*log(n^2));%阈值
%对各级的H，V，D分量进行处理
%mov_win是滑窗函数，根据邻域窗口内小波系数的平方和大小来决定窗口中心的系数是收缩还是置零
cH3=mov_win(cH3,5,thr);
cH2=mov_win(cH2,5,thr);
cH1=mov_win(cH1,5,thr);
cV3=mov_win(cV3,5,thr);
cV2=mov_win(cV2,5,thr);
cV1=mov_win(cV1,5,thr);
cD3=mov_win(cD3,5,thr);
cD2=mov_win(cD2,5,thr);
cD1=mov_win(cD1,5,thr);

m1=size(cA3,1);m2=size(cA3,2);
m=m1*m2;
dnc0(1:m)=cA3(:);

m1=size(cH3,1);m2=size(cH3,2);
m=m1*m2;
dnc1(1:m)=cH3(:);

m1=size(cH2,1);m2=size(cH2,2);
m=m1*m2;
dnc2(1:m)=cH2(:);

m1=size(cH1,1);m2=size(cH1,2);
m=m1*m2;
dnc3(1:m)=cH1(:);

m1=size(cV3,1);m2=size(cV3,2);
m=m1*m2;
dnc4(1:m)=cV3(:);

m1=size(cV2,1);m2=size(cV2,2);
m=m1*m2;
dnc5(1:m)=cV2(:);

m1=size(cV1,1);m2=size(cV1,2);
m=m1*m2;
dnc6(1:m)=cV1(:);

m1=size(cD3,1);m2=size(cD3,2);
m=m1*m2;
dnc7(1:m)=cD3(:);

m1=size(cD2,1);m2=size(cD2,2);
m=m1*m2;
dnc8(1:m)=cD2(:);

dnc10=[dnc0 dnc1 dnc4 dnc7 dnc2 dnc5 dnc8 dnc3 dnc6 dnc9];
dnc=waverec2(dnc10,s,'db8');%重构
dnc=uint8(dnc);
subplot(122),imshow(dnc,[]),title('5*5邻域去噪后图像')

function y=cacupsnr(f,g)   %计算PSNR
m=size(f,1);n=size(f,2);
d=f-g;
d=d.^2;r=g.^2;
mse=sum(d(:))/m/n;

psnr=10*log10(255^2/mse);
snr=10*log10(sum(r(:))/m/n/mse);
y.MSE=mse;y.RMSE=rmse;
y.PSNR=psnr;y.SNR=snr;