matlab程序100例-第73例 图像过滤(1)

第73例 图像过滤(1)--SALT&PEPPER噪声及中值过滤

一、程序源代码

其中,修改了三个地方:

1.删除u1的backgroundcolor属性;

1.删除u11的backgroundcolor属性;

1.删除u12的backgroundcolor属性;

删除上述3个地方的原因是uimenu没有backgroundcolor属性。

%过滤图像
h0=figure('toolbar','none',...
    'position',[198 56 350 468],...
    'name','实例73');
h1=axes('parent',h0,...
    'position',[0.25 0.45 0.5 0.5],...
    'visible','off');
I=imread('eight.tif');
imshow(I)
u1=uimenu('parent',h0,...
    'tag','u1',...
    'label','添加噪声');
u11=uimenu('parent',u1,...
    'tag','u11',...
    'label','SALT&PEPPER噪声',...
    'callback',[...
        'set(u11,''checked'',''on'');,',...
        'set(u12,''checked'',''off'');,',...
        'cla,',...
        'I=imnoise(I,''salt & pepper'',0.02);,',...
        'imshow(I)']);
 u12=uimenu('parent',u1,...
     'tag','u12',...
     'label','GAUSSIAN噪声',...
     'callback',[...
        'set(u12,''checked'',''on'');,',...
        'set(u11,''checked'',''off'');,',...
        'cla,',...
        'I=imnoise(I,''gaussian'',0,0.005);,',...
        'imshow(I)']);
 b1=uicontrol('parent',h0,...
    'units','points',...
    'tag','b1',...
    'style','pushbutton',...
    'string','均平过滤',...
    'backgroundcolor',[0.75 0.75 0.75],...
    'position',[30 100 50 20],...
    'callback',[
        'cla,',...
        'J=filter2(fspecial(''average'',3),I)/255;,',...
        'imshow(J)']);
 b2=uicontrol('parent',h0,...
    'units','points',...
    'tag','b2',...
    'style','pushbutton',...
    'string','中值过滤',...
    'backgroundcolor',[0.75 0.75 0.75],...
    'position',[100 100 50 20],...
    'callback',[
        'cla,',...
        'J=medfilt2(I,[3,3]);,',...
        'imshow(J)']);
  b3=uicontrol('parent',h0,...
    'units','points',...
    'tag','b3',...
    'style','pushbutton',...
    'string','自适应过滤',...
    'backgroundcolor',[0.75 0.75 0.75],...
    'position',[170 100 50 20],...
    'callback',[
        'cla,',...
        'J=wiener2(I,[5,5]);,',...
        'imshow(J)']);   
  b4=uicontrol('parent',h0,...
    'units','points',...
    'tag','b4',...
    'style','pushbutton',...
    'string','关闭',...
    'fontsize',15,...
    'backgroundcolor',[0.75 0.75 0.75],...
    'position',[90 50 70 30],...
    'callback','close');      

二、界面分析

      运行程序后,会出现下图所示的界面,其中的eight.tif文件是matlab内部文件,当程序目录在MATLAB\R2016a\bin目录下,不需要自己添加。

       其中,在工具栏添加了“添加噪声”选项,其下包括SALTPEPPER噪声和GAUSSIAN噪声。


三、相关分析

1.SALT&PEPPER噪声

       椒盐噪声(salt-and-pepper noise)又称脉冲噪声,它随机改变一些像素值,在二值图像上表现为使一些像素点变白,一些像素点变黑,去除脉冲干扰及椒盐噪声最常用的算法是中值滤波。

实验:

添加噪声→SALTPEPPER噪声,图形上回随机出现一些黑色和白色的点。

源代码分析

u11=uimenu('parent',u1,...
    'tag','u11',...
    'label','SALT&PEPPER噪声',...
    'callback',[...
        'set(u11,''checked'',''on'');,',...
        'set(u12,''checked'',''off'');,',...
        'cla,',...
        'I=imnoise(I,''salt & pepper'',0.02);,',...
        'imshow(I)']);

其中,关键的一行为

'I=imnoise(I,''salt & pepper'',0.02);,',...

imnoise为向图片增加噪声。

J = imnoise(I,'salt & pepper',d) adds salt and pepper noise to the image I, where d is the noise density. This affects approximately d*numel(I) pixels. The default for d is 0.05.

本处的噪声密度为0.02,即2%.


接下来是对噪声进行过滤,中值过滤能够滤除Salt&Pepper噪声。

源代码分析:

 b2=uicontrol('parent',h0,...
    'units','points',...
    'tag','b2',...
    'style','pushbutton',...
    'string','中值过滤',...
    'backgroundcolor',[0.75 0.75 0.75],...
    'position',[100 100 50 20],...
    'callback',[
        'cla,',...
        'J=medfilt2(I,[3,3]);,',...
        'imshow(J)']);

其中,关键的一行为:

'J=medfilt2(I,[3,3]);,',...

medfilt2为2维中值滤波器,对图形I进行中值滤波,邻域区间为3*3。如下图所示,像素5的新灰度值为9个像素灰度值的中值。

下图为过滤后的图形。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值