Noise Reduction Using a Median Filter(噪声去除的中值滤波方法)

来源:互联网 发布:网络病毒有哪些 编辑:程序博客网 时间:2024/05/16 01:40

    要:本实验主要实现图像的加噪和去噪。模拟数字图像的噪声主要由于噪声广泛存在于图像的产生和传输过程。图像传感器的工作情况受各种因素的影响,如图像的获取中的环境条件和传感器自身的质量。图像在传输过程中主要由于所用的传输信道的干扰而受到噪声污染。在本次实验的加噪过程中使用的是椒盐噪声,有时也被称为脉冲噪声、散粒噪声或尖峰噪声。脉冲噪声主要表现在成像中的短暂停留中,例如错误的开关操作。去噪过程中采用了3 3中值滤波算法。中值滤波法是一种非线性平滑技术,它将每一像素点的灰度值设置为该点某邻域窗口内的所有像素点灰度值的中值。该方法被广泛使用于各种随机噪声的去除。


一、技术论证


噪声模型

数字图像的噪声主要来源于图像的产生和传输过程。图像传感器的工作情况受各种因素的影响,如图像的获取中的环境条件和传感器自身的质量。图像在传输过程中主要由于所用的传输信道的干扰而受到噪声污染。


椒盐噪声及其概率密度函数

    首先定义椒盐噪声的概率密度函数:

    对于尺寸为 的输入图像,我们假设每个像素点变成胡椒噪声或盐粉噪声的概率分别为  

    噪声脉冲可以是正的,也可以是负的。标定通常是图像数字化过程的一部分。因为脉冲干扰通常与图像信号的强度相比较大,因此在一幅图像中,脉冲噪声总是数字化为最大值(纯白或纯黑)。这样,通常假设ab为饱和值。从某种意义上看,在数字化图像中,它们等于所允许的最大值和最小值。对于一个8位图像,这意味着a=255(白),a=0(黑)。

    在实际处理中,可以生成一个与尺寸为 的输入图像相对应的矩阵,该矩阵中每个元素都为01之间的随机数,这样可以规定  两个区间内的元素相对应的输入图像的元素分别变成 (白)和 (黑),在MATLAB中使用find()函数可以找出这些元素的坐标。

    图1给出添加了椒盐噪声的图像的直方图,当概率  取值越大,则各像素点取灰度值为0255的概率越大。

椒盐噪声图像及其直方图

顺序统计滤波器

    由于本实验所用的中值滤波器是顺序统计滤波器中的一种,因此需要介绍一下统计滤波器的相关知识。

    顺序统计滤波器这是一种非线性的空间滤波器,它的响应基于图像滤波器包围的图像区域中像素的排序,然后由统计排序结果决定的值代替中心像素的值。


中值滤波器

    一个数值集合的中值K 是这样的数值,即在数值集合中,有一半小于或等于K ,还有一半大于或等于K 。对于一幅图像,对某个像素点做中值滤波处理,必须先将掩膜内欲求的像素点及其领域的像素值排序,确定出中值,并将该中值赋予该像素点。例如,对于一个3×3的区域,其中值就是第五个值,而在一个5×5的区域中,中值就是第十三个值。而当一个领域中的所有像素值相同时,它们中的任何一个都可以作为中值。中值滤波器的主要功能是使拥有不同灰度的点看起来更接近于它的临近值,以下给出一个取中值的过程。

中值滤波中的排序过程

    对于一个3×3的矩阵,需要将其映射到一个一列或一行的矩阵再进行排序,在MATLAB中可以调用reshape(A,m,n)sort(X)函数,函数的功能如下所示:

        reshape(A,m,n)   功能:返回一个m*n的矩阵BB中元素是按列从A中得到的。

        sort(X)                功能:返回对向量X中的元素按列或按行升序排列的新向量。

    在调用reshape(A,m,n)时需注意A的表示。当第一个参数为A时表示按照列优先进行排列,若想按矩阵的每一行进行排列,需改为A',如图2所示。

2 reshape(A,m,n)的不同用法


         sort(X)的功能如图3所示。

3 sort(X)的用法


二、实验结果讨论

    编写函数salt_pepper(ima,a,b)产生椒盐噪声,其中ima为输入图像,ab分别是两个噪声分量的概率值。本实验要求ab0.2,即各像素点产生胡椒噪声和盐粉噪声的概率均为20%

    在程序中通过定义两个区间,当随机矩阵A中元素值在0a区间时判定为胡椒微粒,在(1-b)1区间时判定为盐粉微粒,并使用find()函数求出这些元素的位置,映射到原图像ima上,输出带有椒盐噪声的图像imt,图4给出ab取不同值时的加噪图像的效果图。

4  ab取不同值时的加噪图像效果图


    对比图4中的噪声图像易知,当ab的取值越大,图像上叠加的胡椒和盐粉噪声越多,在实际使用中可以根据需要调整ab的取值。当ab取值过大,图像的有用信号将被淹没在噪声信号中。

    下面验证中值滤波法滤除椒盐噪声的效果,噪声系数为0.2

    由图5所示,对加噪图像进行一次3×3中值滤波处理后,噪点明显减少,且图像能保留大部分的细节,但噪声污染依然严重,因此验证对图像进行多次中值滤波的可行性。而从图6可以看出,多次中值滤波可以有效滤除椒盐噪声点,图像经过多次滤波后效果越来越好。

    像素点原始值包含在中值的计算结果中,中值滤波器的应用也非常普遍。对于很多种随机噪声,它都有良好的去噪能力,而且在相同尺寸下比起线性平滑滤波器引起的模糊较少。正如本实验输出图像所示,中值滤波器对椒盐噪声干扰的图像有非常好的处理效果。

    但是,中值滤波存在一个无法解决的问题:图像的边缘部分噪声无法滤除。对于尺寸为M×N的图像,中值滤波算法只作用于[2,M-1][2,N-1]区间的像素点,而无法改善边缘一圈的像素点。


三、实验结果图片

    本实验使用冈萨雷斯主编数字图像处理第三版中的素材Fig5.07(a).jpg进行加噪处理,并对噪声图像进行中值滤波,验证中值滤波法的有效性

对加噪图像进行一次3×3中值滤波的效果

对加噪图像进行多次中值滤波


下面给出简单的MATLAB代码:

<a target=_blank id="L1" href="http://blog.csdn.net/liyuefeilong/article/details/43226555#L1" rel="#L1" style="color: rgb(102, 102, 102); text-decoration: none;">  1</a><a target=_blank id="L2" href="http://blog.csdn.net/liyuefeilong/article/details/43226555#L2" rel="#L2" style="color: rgb(102, 102, 102); text-decoration: none;">  2</a><a target=_blank id="L3" href="http://blog.csdn.net/liyuefeilong/article/details/43226555#L3" rel="#L3" style="color: rgb(102, 102, 102); text-decoration: none;">  3</a><a target=_blank id="L4" href="http://blog.csdn.net/liyuefeilong/article/details/43226555#L4" rel="#L4" style="color: rgb(102, 102, 102); text-decoration: none;">  4</a><a target=_blank id="L5" href="http://blog.csdn.net/liyuefeilong/article/details/43226555#L5" rel="#L5" style="color: rgb(102, 102, 102); text-decoration: none;">  5</a><a target=_blank id="L6" href="http://blog.csdn.net/liyuefeilong/article/details/43226555#L6" rel="#L6" style="color: rgb(102, 102, 102); text-decoration: none;">  6</a><a target=_blank id="L7" href="http://blog.csdn.net/liyuefeilong/article/details/43226555#L7" rel="#L7" style="color: rgb(102, 102, 102); text-decoration: none;">  7</a><a target=_blank id="L8" href="http://blog.csdn.net/liyuefeilong/article/details/43226555#L8" rel="#L8" style="color: rgb(102, 102, 102); text-decoration: none;">  8</a><a target=_blank id="L9" href="http://blog.csdn.net/liyuefeilong/article/details/43226555#L9" rel="#L9" style="color: rgb(102, 102, 102); text-decoration: none;">  9</a><a target=_blank id="L10" href="http://blog.csdn.net/liyuefeilong/article/details/43226555#L10" rel="#L10" style="color: rgb(102, 102, 102); text-decoration: none;"> 10</a><a target=_blank id="L11" href="http://blog.csdn.net/liyuefeilong/article/details/43226555#L11" rel="#L11" style="color: rgb(102, 102, 102); text-decoration: none;"> 11</a><a target=_blank id="L12" href="http://blog.csdn.net/liyuefeilong/article/details/43226555#L12" rel="#L12" style="color: rgb(102, 102, 102); text-decoration: none;"> 12</a><a target=_blank id="L13" href="http://blog.csdn.net/liyuefeilong/article/details/43226555#L13" rel="#L13" style="color: rgb(102, 102, 102); text-decoration: none;"> 13</a><a target=_blank id="L14" href="http://blog.csdn.net/liyuefeilong/article/details/43226555#L14" rel="#L14" style="color: rgb(102, 102, 102); text-decoration: none;"> 14</a><a target=_blank id="L15" href="http://blog.csdn.net/liyuefeilong/article/details/43226555#L15" rel="#L15" style="color: rgb(102, 102, 102); text-decoration: none;"> 15</a><a target=_blank id="L16" href="http://blog.csdn.net/liyuefeilong/article/details/43226555#L16" rel="#L16" style="color: rgb(102, 102, 102); text-decoration: none;"> 16</a><a target=_blank id="L17" href="http://blog.csdn.net/liyuefeilong/article/details/43226555#L17" rel="#L17" style="color: rgb(102, 102, 102); text-decoration: none;"> 17</a><a target=_blank id="L18" href="http://blog.csdn.net/liyuefeilong/article/details/43226555#L18" rel="#L18" style="color: rgb(102, 102, 102); text-decoration: none;"> 18</a><a target=_blank id="L19" href="http://blog.csdn.net/liyuefeilong/article/details/43226555#L19" rel="#L19" style="color: rgb(102, 102, 102); text-decoration: none;"> 19</a><a target=_blank id="L20" href="http://blog.csdn.net/liyuefeilong/article/details/43226555#L20" rel="#L20" style="color: rgb(102, 102, 102); text-decoration: none;"> 20</a><a target=_blank id="L21" href="http://blog.csdn.net/liyuefeilong/article/details/43226555#L21" rel="#L21" style="color: rgb(102, 102, 102); text-decoration: none;"> 21</a><a target=_blank id="L22" href="http://blog.csdn.net/liyuefeilong/article/details/43226555#L22" rel="#L22" style="color: rgb(102, 102, 102); text-decoration: none;"> 22</a><a target=_blank id="L23" href="http://blog.csdn.net/liyuefeilong/article/details/43226555#L23" rel="#L23" style="color: rgb(102, 102, 102); text-decoration: none;"> 23</a><a target=_blank id="L24" href="http://blog.csdn.net/liyuefeilong/article/details/43226555#L24" rel="#L24" style="color: rgb(102, 102, 102); text-decoration: none;"> 24</a><a target=_blank id="L25" href="http://blog.csdn.net/liyuefeilong/article/details/43226555#L25" rel="#L25" style="color: rgb(102, 102, 102); text-decoration: none;"> 25</a><a target=_blank id="L26" href="http://blog.csdn.net/liyuefeilong/article/details/43226555#L26" rel="#L26" style="color: rgb(102, 102, 102); text-decoration: none;"> 26</a><a target=_blank id="L27" href="http://blog.csdn.net/liyuefeilong/article/details/43226555#L27" rel="#L27" style="color: rgb(102, 102, 102); text-decoration: none;"> 27</a><a target=_blank id="L28" href="http://blog.csdn.net/liyuefeilong/article/details/43226555#L28" rel="#L28" style="color: rgb(102, 102, 102); text-decoration: none;"> 28</a><a target=_blank id="L29" href="http://blog.csdn.net/liyuefeilong/article/details/43226555#L29" rel="#L29" style="color: rgb(102, 102, 102); text-decoration: none;"> 29</a><a target=_blank id="L30" href="http://blog.csdn.net/liyuefeilong/article/details/43226555#L30" rel="#L30" style="color: rgb(102, 102, 102); text-decoration: none;"> 30</a><a target=_blank id="L31" href="http://blog.csdn.net/liyuefeilong/article/details/43226555#L31" rel="#L31" style="color: rgb(102, 102, 102); text-decoration: none;"> 31</a><a target=_blank id="L32" href="http://blog.csdn.net/liyuefeilong/article/details/43226555#L32" rel="#L32" style="color: rgb(102, 102, 102); text-decoration: none;"> 32</a><a target=_blank id="L33" href="http://blog.csdn.net/liyuefeilong/article/details/43226555#L33" rel="#L33" style="color: rgb(102, 102, 102); text-decoration: none;"> 33</a><a target=_blank id="L34" href="http://blog.csdn.net/liyuefeilong/article/details/43226555#L34" rel="#L34" style="color: rgb(102, 102, 102); text-decoration: none;"> 34</a><a target=_blank id="L35" href="http://blog.csdn.net/liyuefeilong/article/details/43226555#L35" rel="#L35" style="color: rgb(102, 102, 102); text-decoration: none;"> 35</a><a target=_blank id="L36" href="http://blog.csdn.net/liyuefeilong/article/details/43226555#L36" rel="#L36" style="color: rgb(102, 102, 102); text-decoration: none;"> 36</a><a target=_blank id="L37" href="http://blog.csdn.net/liyuefeilong/article/details/43226555#L37" rel="#L37" style="color: rgb(102, 102, 102); text-decoration: none;"> 37</a><a target=_blank id="L38" href="http://blog.csdn.net/liyuefeilong/article/details/43226555#L38" rel="#L38" style="color: rgb(102, 102, 102); text-decoration: none;"> 38</a><a target=_blank id="L39" href="http://blog.csdn.net/liyuefeilong/article/details/43226555#L39" rel="#L39" style="color: rgb(102, 102, 102); text-decoration: none;"> 39</a><a target=_blank id="L40" href="http://blog.csdn.net/liyuefeilong/article/details/43226555#L40" rel="#L40" style="color: rgb(102, 102, 102); text-decoration: none;"> 40</a><a target=_blank id="L41" href="http://blog.csdn.net/liyuefeilong/article/details/43226555#L41" rel="#L41" style="color: rgb(102, 102, 102); text-decoration: none;"> 41</a><a target=_blank id="L42" href="http://blog.csdn.net/liyuefeilong/article/details/43226555#L42" rel="#L42" style="color: rgb(102, 102, 102); text-decoration: none;"> 42</a><a target=_blank id="L43" href="http://blog.csdn.net/liyuefeilong/article/details/43226555#L43" rel="#L43" style="color: rgb(102, 102, 102); text-decoration: none;"> 43</a><a target=_blank id="L44" href="http://blog.csdn.net/liyuefeilong/article/details/43226555#L44" rel="#L44" style="color: rgb(102, 102, 102); text-decoration: none;"> 44</a><a target=_blank id="L45" href="http://blog.csdn.net/liyuefeilong/article/details/43226555#L45" rel="#L45" style="color: rgb(102, 102, 102); text-decoration: none;"> 45</a><a target=_blank id="L46" href="http://blog.csdn.net/liyuefeilong/article/details/43226555#L46" rel="#L46" style="color: rgb(102, 102, 102); text-decoration: none;"> 46</a><a target=_blank id="L47" href="http://blog.csdn.net/liyuefeilong/article/details/43226555#L47" rel="#L47" style="color: rgb(102, 102, 102); text-decoration: none;"> 47</a><a target=_blank id="L48" href="http://blog.csdn.net/liyuefeilong/article/details/43226555#L48" rel="#L48" style="color: rgb(102, 102, 102); text-decoration: none;"> 48</a><a target=_blank id="L49" href="http://blog.csdn.net/liyuefeilong/article/details/43226555#L49" rel="#L49" style="color: rgb(102, 102, 102); text-decoration: none;"> 49</a><a target=_blank id="L50" href="http://blog.csdn.net/liyuefeilong/article/details/43226555#L50" rel="#L50" style="color: rgb(102, 102, 102); text-decoration: none;"> 50</a><a target=_blank id="L51" href="http://blog.csdn.net/liyuefeilong/article/details/43226555#L51" rel="#L51" style="color: rgb(102, 102, 102); text-decoration: none;"> 51</a><a target=_blank id="L52" href="http://blog.csdn.net/liyuefeilong/article/details/43226555#L52" rel="#L52" style="color: rgb(102, 102, 102); text-decoration: none;"> 52</a><a target=_blank id="L53" href="http://blog.csdn.net/liyuefeilong/article/details/43226555#L53" rel="#L53" style="color: rgb(102, 102, 102); text-decoration: none;"> 53</a><a target=_blank id="L54" href="http://blog.csdn.net/liyuefeilong/article/details/43226555#L54" rel="#L54" style="color: rgb(102, 102, 102); text-decoration: none;"> 54</a><a target=_blank id="L55" href="http://blog.csdn.net/liyuefeilong/article/details/43226555#L55" rel="#L55" style="color: rgb(102, 102, 102); text-decoration: none;"> 55</a><a target=_blank id="L56" href="http://blog.csdn.net/liyuefeilong/article/details/43226555#L56" rel="#L56" style="color: rgb(102, 102, 102); text-decoration: none;"> 56</a><a target=_blank id="L57" href="http://blog.csdn.net/liyuefeilong/article/details/43226555#L57" rel="#L57" style="color: rgb(102, 102, 102); text-decoration: none;"> 57</a><a target=_blank id="L58" href="http://blog.csdn.net/liyuefeilong/article/details/43226555#L58" rel="#L58" style="color: rgb(102, 102, 102); text-decoration: none;"> 58</a><a target=_blank id="L59" href="http://blog.csdn.net/liyuefeilong/article/details/43226555#L59" rel="#L59" style="color: rgb(102, 102, 102); text-decoration: none;"> 59</a><a target=_blank id="L60" href="http://blog.csdn.net/liyuefeilong/article/details/43226555#L60" rel="#L60" style="color: rgb(102, 102, 102); text-decoration: none;"> 60</a>
% 编写函数salt_pepper()产生椒盐噪声
% ima为,a,b分别是两个噪声分量的概率值
function imt = salt_pepper(ima,a,b)
imt=ima;
[m,n]=size(ima);
A=rand(m,n); % 生成一个m*n尺寸的矩阵,每个元素为[0,1]之间的随机值
% 当A中元素值小于a时判定为胡椒微粒
B=find(A<=a); % find()函数找出符合条件的元素的位置,B存储这些位置
imt(B)=0; % 属于位置B中的元素产生胡椒微粒
% 当A中元素值大于1-b时判定为盐分微粒
B=find(A>=1-b);
imt(B)=255; % 属于位置B中的元素产生盐分微粒
return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 中值滤波主程序
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function proj05()
ima=imread('Fig5.07(a).jpg');
subplot(2,3,1),imshow(ima),title('输入图像');
sp_imt = salt_pepper(ima,0.1,0.1); % 调用自编函数,两个噪声分量的概率值均为0.2
%subplot(2,3,2),
imshow(sp_imt),title('输入参数0.2,0.2的椒盐噪声图像');
% sp_imt = salt_pepper(ima,0.05,0.05); % 调用自编函数,两个噪声分量的概率值均为0.2
% subplot(1,3,2),imshow(sp_imt),title('输入参数0.05,0.05的椒盐噪声图像');
%
% sp_imt = salt_pepper(ima,0.02,0.02); % 调用自编函数,两个噪声分量的概率值均为0.2
% subplot(1,3,3),imshow(sp_imt),title('输入参数0.02,0.02的椒盐噪声图像');
output = sp_imt; % output为待处理图像
[xs,ys]=size(sp_imt);
% 3*3中值滤波
for i=2:xs-1 % 在图像的[2,xs-1],[2,ys-1]区域取3×3的方格求中值
for j=2:ys-1
m=1;
n=1;
for x=i-1:i+1
for y=j-1:j+1
block(m,n)=sp_imt(x,y); % 求出点(i,j)处的3×3方格,存在block中
n=n+1;
end
m=m+1;
n=1;
end
% 将3×3矩阵块排列成一维数组,然后再进行大小排列和中值提取
medianfilter = reshape(block',9,1); % block为列优先,在排列时要求行优先,需改为block'
queue = sort(medianfilter); % 返回对向量One中的元素按列升序排列的新向量。
median = queue(5); % 在3*3模版中,第五个值为中值
output(i,j)=median; % 将各点中值赋给输出图像output
end
end
%subplot(2,3,3);
imshow(output),title('3*3中值滤波后的图像');
 来自CODE的代码片

0 0