1.均值、中值、高斯、维纳图像去噪
clc
clear
g = rgb2gray(imread('chuzao.jpg'));
zyy = imread('y.jpg');
g1 = imfilter(g, fspecial('average'));%均值滤波
g2 = medfilt2(g, [3 3]); %3*3中值滤波
h_gaosi1 = fspecial('gaussian',3,1);
g3 = imfilter(g, h_gaosi1); %高斯平滑滤波
g4 = wiener2(g,[5 5]); %5*5维纳滤波
subplot(3, 2, 1), imshow(zyy), title('原图');
subplot(3, 2, 2), imshow(g), title('待处理噪声');
subplot(3, 2, 3), imshow(g1), title('均值滤波');
subplot(3, 2, 4), imshow(g2), title('中值滤波');
subplot(3, 2, 5), imshow(g3), title('高斯平滑滤波');
subplot(3, 2, 6), imshow(g4), title('维纳滤波');
%下面是计算PSNR和SSIM的程序口令
psnr(zyy,g)
psnr(zyy,g1)
psnr(zyy,g2)
psnr(zyy,g3)
psnr(zyy,g4)
ssim(zyy,g1)
ssim(zyy,g2)
ssim(zyy,g3)
ssim(zyy,g4)
2.小波重构除噪
clc
clear;
zyy = imread('y.jpg'); %读取原图像
subplot(221);
imshow(zyy);
title('原图');
X=imread('chuzao.jpg');
X=rgb2gray(X);
subplot(222);
imshow(X);
title('待处理噪声');
X=double(X);
%用小波函数coif2对图像X进行2层
% 分解
[c,l]=wavedec2(X,2,'coif2');
% 设置尺度向量
n=[1,2];
% 设置阈值向量 , 对高频小波系数进行阈值处理
p=[10.28,24.08];
nc=wthcoef2('h',c,l,n,p,'s');
% 图像的二维小波重构
X1=waverec2(nc,l,'coif2');
subplot(223);
imshow(uint8(X1));
%colormap(map);
title(' 小波第一次消噪后的图像 ');
%再次对高频小波系数进行阈值处理
mc=wthcoef2('v',nc,l,n,p,'s');
% 图像的二维小波重构
X2=waverec2(mc,l,'coif2');
subplot(224);
imshow(uint8(X2));
title(' 小波重构第二次消噪后的图像 ');
3.NL-means除噪
clc
clear
g = rgb2gray(imread('chuzao.jpg')); %读取待去噪图
zyy = imread('y.jpg');%读取原图
g1 = double(g);
%进行NL-means除噪
[m,n]=size(g1);
ds=2;% block size for calculate weight
Ds=5;% search block
h=10;% decay factor
offset=ds+Ds;
PaddedImg = padarray(g1,[ds+Ds,ds+Ds],'symmetric','both');% 扩展图像,便于处理
%距离加权核
%非均值核
[x,y]=meshgrid(-ds:ds,-ds:ds);
kernel=1./(x.*x+y.*y+1);
%均值核
% kernel=ones(2*ds+1,2*ds+1);
kernel=kernel./((2*ds+1)*(2*ds+1));
dst=zeros(m,n);% output
%---------------------------%
% Non-Local Means Denoising
%---------------------------%
for i=1:m
for j=1:n
%当前点坐标和邻域窗口
i1=i+offset;
j1=j+offset;
W1=PaddedImg(i1-ds:i1+ds,j1-ds:j1+ds);
%加权因子矩阵和图像
weight=zeros(2*Ds+1,2*Ds+1);
image=PaddedImg(i1-Ds:i1+Ds,j1-Ds:j1+Ds);
for r=-Ds:Ds
for s=-Ds:Ds
%跳过当前点
if(r==0&&s==0)
continue;
end
%待加权点坐标和邻域窗口
i2=i1+r;
j2=j1+s;
W2=PaddedImg(i2-ds:i2+ds,j2-ds:j2+ds);
%核加权的距离和加权因子
distance=sum(sum(kernel.*(W1-W2).*(W1-W2)));
weight(r+Ds+1,s+Ds+1)=exp(-distance/(h*h));
end
end
%最大权重赋给当前点,归一化权重
weight(Ds+1,Ds+1)=max(max(weight));
weight=weight/(sum(weight(:)));
dst(i,j)=sum(sum(image.*weight));
end
end
%---------------------------%
% output
%---------------------------%
subplot(121),imshow(g1,[]),title('待处理');
subplot(122),imshow(dst,[]),title('NL-means除噪');
psnr(zyy,uint8(g1))
psnr(zyy,uint8(dst))
ssim(zyy,uint8(g1))
ssim(zyy,uint8(dst))
4.均值、中值、高斯、维纳、小波、NL-means的PSNR、SSIM对比
|
待除噪图 |
均值滤波 |
中值除噪 |
高斯除噪 |
维纳除噪 |
小波除噪 |
NL-除噪 |
PSNR |
20.7648 |
23.724 |
24.2671 |
24.0838 |
26.0139 |
22.3159 |
26.8054 |
SSIM |
0.3504 |
0.5863 |
0.5518 |
0.6006 |
0.7006 |
0.3979 |
0.6385 |