数字图像去噪典型算法及matlab实现
J=imnoise(I,\’gaussian\’,0,0.005);%加入均值为0,方差为0.005的高斯噪声
subplot(2,3,1);imshow(I);
title(\’原始图像\’);
subplot(2,3,2); imshow(J);
title(\’加入高斯噪声之后的图像\’);
%采用MATLAB中的函数filter2对受噪声干扰的图像进行均值滤波
K1=filter2(fspecial(\’average\’,3),J)/255; %模板尺寸为3
K2=filter2(fspecial(\’average\’,5),J)/255;% 模板尺寸为5
K3=filter2(fspecial(\’average\’,7),J)/255; %模板尺寸为7
K4= filter2(fspecial(\’average\’,9),J)/255; %模板尺寸为9
subplot(2,3,3);imshow(K1);
title(\’改进后的图像1\’);
subplot(2,3,4); imshow(K2);
title(\’改进后的图像2\’);
subplot(2,3,5);imshow(K3);
title(\’改进后的图像3\’);
subplot(2,3,6);imshow(K4);
title(\’改进后的图像4\’);
h = fspecial(type)
h = fspecial(type,parameters)
参数type制定算子类型,parameters指定相应的参数,具体格式为:
type=\’average\’,为均值滤波,参数为n,代表模版尺寸,用向量表示,默认值为[3,3]。
type= \’gaussian\’,为高斯低通滤波器,参数有两个,n表示模版尺寸,默认值为[3,3],sigma表示滤波器的标准差,单位为像素,默认值为
type= \’laplacian\’,为拉普拉斯算子,参数为alpha,用于控制拉普拉斯算子的形状,取值范围为[0,1],默认值为0.2。
type= \’log\’,为拉普拉斯高斯算子,参数有两个,n表示模版尺寸,默认值为[3,3],sigma为滤波器的标准差,单位为像素,默认值为0.5
type= \’prewitt\’,为prewitt算子,用于边缘增强,无参数。
type= \’sobel\’,为著名的sobel算子,用于边缘提取,无参数。
type= \’unsharp\’,为对比度增强滤波器,参数alpha用于控制滤波器的形状,范围为[0,1],默认值为0.2。
J=imnoise(I,\’gaussian\’,0,0.005); %加入均值为0,方差为0.005的高斯噪声
K2=wiener2(J,[3 3]); %对加噪图像进行二维自适应维纳滤波
K2=wiener2(J,[5 5]); %对加噪图像进行二维自适应维纳滤波
K2=wiener2(J,[7 7]); %对加噪图像进行二维自适应维纳滤波
K2=wiener2(J,[9 9]); %对加噪图像进行二维自适应维纳滤波
subplot(2,3,1);imshow(I);
title(\’原始图像\’);
subplot(2,3,2);imshow(J);
title(\’加噪图像\’);
subplot(2,3,3);imshow(K1);
title(\’恢复图像1\’);
subplot(2,3,4);imshow(K2);
title(\’恢复图像2\’);
subplot(2,3,5);imshow(K3);
title(\’恢复图像3\’);
subplot(2,3,6);imshow(K4);
title(\’恢复图像3\’);
J = imnoise(I,type)
J = imnoise(I,type,parameters)
其中J = imnoise(I,type)返回对原始图像I添加典型噪声的有噪图像J。
参数type和parameters用于确定噪声的类型和相应的参数。
下面的命令是对图像1.gif分别加入高斯噪声、椒盐噪声和乘性噪声,其结果如图所示:
实验三:对加入椒盐噪声的图像分别作均值、中值和维纳滤波
J1=imnoise(I,\’gaussian\’,0,0.02);
J2=imnoise(I,\’salt & pepper\’,0.02);
J3=imnoise(I,\’speckle\’,0.02);
运行效果见图2
I=imread(\’C:\Documents and Settings\Administrator\桌面\1.gif\’);
J=imnoise(I,\’salt & pepper\’,0.02);
%h=ones(3,3)/9;%产生3*3的全1数组
%B=conv2(J,h);%卷积运算
K2=filter2(fspecial(\’average\’,3),J)/255; %均值滤波模板尺寸为3
K= medfilt2(J);%采用二维中值滤波函数medfilt2对受椒盐噪声干扰的图像滤波
K1=wiener2(J,[3 3]); %对加噪图像进行二维自适应维纳滤波
subplot(2,3,1);imshow(I);
title(\’原始图像\’);
subplot(2,3,2);imshow(J);
title(\’加噪图像\’);
subplot(2,3,3);imshow(K2);
title(\’均值滤波后的图像\’);
subplot(2,3,4);imshow(K);
title(\’中值滤波后的图像\’);
subplot(2,3,5);imshow(K1);
title(\’维纳滤波后的图像\’);
C = conv2(A,B)
C = conv2(A,B)返回矩阵A和B的二维卷积C。若A为ma×na的矩阵,B为mb×nb的矩阵,则C的大小为(ma+mb+1)×(na+nb+1)。
Y = filter2(h,X)
其中Y = filter2(h,X)返回图像X经算子h滤波后的结果,默认返回图像Y与输入图像X大小相同。例如:
其实filter2和conv2是等价的。MATLAB在计算filter2时先将卷积核旋转180度,再调用conv2函数进行计算。
Fspecial函数用于创建预定义的滤波算子,其语法格式为:
h = fspecial(type)
h = fspecial(type,parameters)
参数type制定算子类型,parameters指定相应的参数,具体格式为前文已有叙述。
ones(a,b)产生a行b列全1数组
ones(a)产生a行a列全1叔祖
J1=imnoise(I,\’salt & pepper\’,0.004);
subplot(2,3,1);imshow(I);
title(\’原始图像\’);
subplot(2,3,2);imshow(J1);
title(\’加椒盐噪声后的图像\’);
J= ordfilt2(J1,5,ones(3,4));% 进行二维统计顺序过滤
subplot(2,3,3);imshow(J);
title(\’椒盐噪声滤波后的图像\’);
J2=imnoise(I,\’gaussian\’,0,0.004);
subplot(2,3,4);imshow(J2);
title(\’加高斯噪声后的图像\’);
J3= ordfilt2(J2,5,ones(3,4));
subplot(2,3,5);imshow(J3);
title(\’高斯噪声滤波后的图像\’);
J=imnoise(I,\’gaussian\’,0,0.005);
[c,l]=wavedec2(J,2,\’sym4\’);
J1= wrcoef2(\’a\’,c,l,\’sym4\’,1);
J2= wrcoef2(\’a\’,c,l,\’sym4\’,2);
subplot(2,2,1);imshow(I);
title(\’原始图像\’);
subplot(2,2,2);imshow(J);
title(\’含噪图像\’);
subplot(2,2,3);imshow(J1,[]);
title(\’第一次去噪图像\’);
subplot(2,2,4);imshow(J2,[]);
title(\’第二次去噪图像\’);
I=im2double(I);
subplot(2,2,1);imshow(I);
title(\’原始图像\’);
J=imnoise(I,\’gaussian\’,0,0.05);
subplot(2,2,2);imshow(J);
title(\’含噪图像\’);
thr=0.1;sorh=\’s\’;
crit=\’shannon\’;
keepapp=0;
J1=wpdencmp(J,sorh,3,\’sym4\’,crit,thr,keepapp);
subplot(2,2,3);imshow(J1);
title(\’全局阈值去噪图像\’);
J2=medfilt2(J1);
subplot(2,2,4);imshow(J2);
title(\’第二次去噪图像\’);
Medfilt2函数的语法格式为:
B = medfilt2(A) 用3×3的滤波窗口对图像A进行中值滤波。
B = medfilt2(A,[m n]) 用指定大小为m×n的窗口对图像A进行中值滤波。