动态二维码中值滤波处理_【摘录】中值滤波器 ( Median Filter ) C++ 实现

本文介绍了中值滤波器的工作原理,通过比较均值滤波,阐述了中值滤波在图像平滑和降噪方面的优势,特别是针对椒盐噪声的消除。提供了一段C++代码实现动态二维码图像的中值滤波处理,展示处理前后的对比效果,证实了中值滤波在去除噪声方面的优越性。
摘要由CSDN通过智能技术生成

有了前面一个均值滤波器的基础, 在看中值滤波器就不是很容易继续了。均值滤波是像素周围的3*3的像素做平均值操作, 那么中值就是在3*3中的像素中寻找中值。 来看这样一个描述图(无图无真相)

sample1633824919249612500.png

这把可以清晰地看到, 这里有6,2,0,3,97,4,19,3,10这些像素, 然后中间的这些像素值就被这些像素的中位数也就是中值取代了。为了满足和前面一篇文章的格式相对应, 我们马上进入下一个单元, 来看看在平滑和降噪方面的功效!

原图1                                                                           中值滤波之后

sample_raw.png 

sample_raw_median.png

噪声图(5%)                                                                中值滤波后:

sample_cor.png 

sample_cor_median.png

非常impressive的一点在这里就可以看出来了, 很明显中值滤波不仅是图像变得平滑,同时去除了椒盐噪声(图像最外圈的像素没有去除掉只是因为我没有从0-width处理而已)。从这里中值的逻辑来看, 我们做中值操作的时候, 那么白色(255)和黑色(0)因为是最大最小值, 除非周围的颜色都是黑色或者白色,不然一般都会被剔除掉, 这就是和均值最大的不同! 所以在效果上要好很多。一般来说这个中值滤波是去除椒盐噪声的非常理想的选择。

一样的,最后还是贴一段我运行的代码:

/**

** method to remove noise from the corrupted image by median value

* @param corrupted input grayscale binary array with corrupted info

* @param smooth output data for smooth result, the memory need to be allocated outside of the function

* @param width width of the input grayscale image

* @param height height of the input grayscale image

*/

voidmedianFilter (unsignedchar* corrupted, unsignedchar* smooth,intwidth,intheight)

{

memcpy ( smooth, corrupted, width*height*sizeof(unsignedchar) );

for(intj=1;j

{

for(inti=1;i

{

intk = 0;

unsigned charwindow[9];

for(intjj = j - 1; jj 

for(intii = i - 1; ii 

window[k++] = corrupted[jj * width + ii];

//   Order elements (only half of them)

for(intm = 0; m 

{

intmin = m;

for(intn = m + 1; n 

if(window[n] 

min = n;

//   Put found minimum element in its place

unsigned chartemp = window[m];

window[m] = window[min];

window[min] = temp;

}

smooth[ j*width+i ] = window[4];

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值