中值滤波器 ( Median Filter ) C++ 实现
2015-08-02 23:11
573 查看
有了前面一个均值滤波器 的基础, 在看中值滤波器就不是很容易继续了。均值滤波是像素周围的3*3的像素做平均值操作, 那么中值就是在3*3中的像素中寻找中值。 来看这样一个描述图(无图无真相)
这把可以清晰地看到, 这里有6,2,0,3,97,4,19,3,10这些像素, 然后中间的这些像素值就被这些像素的中位数也就是中值取代了。为了满足和前面一篇文章的格式相对应, 我们马上进入下一个单元, 来看看在平滑和降噪方面的功效!
原图1 中值滤波之后
噪声图(5%) 中值滤波后:
非常impressive的一点在这里就可以看出来了, 很明显中值滤波不仅是图像变得平滑,同时去除了椒盐噪声(图像最外圈的像素没有去除掉只是因为我没有从0-width处理而已)。从这里中值的逻辑来看, 我们做中值操作的时候, 那么白色(255)和黑色(0)因为是最大最小值, 除非周围的颜色都是黑色或者白色,不然一般都会被剔除掉, 这就是和均值最大的不同! 所以在效果上要好很多。一般来说这个中值滤波是去除椒盐噪声的非常理想的选择。
一样的,最后还是贴一段我运行的代码:
[cpp] view
plaincopy
/**
** 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
*/
void medianFilter (unsigned char* corrupted, unsigned char* smooth, int width, int height)
{
memcpy ( smooth, corrupted, width*height*sizeof(unsigned char) );
for (int j=1;j<height-1;j++)
{
for (int i=1;i<width-1;i++)
{
int k = 0;
unsigned char window[9];
for (int jj = j - 1; jj < j + 2; ++jj)
for (int ii = i - 1; ii < i + 2; ++ii)
window[k++] = corrupted[jj * width + ii];
// Order elements (only half of them)
for (int m = 0; m < 5; ++m)
{
int min = m;
for (int n = m + 1; n < 9; ++n)
if (window
< window[min])
min = n;
// Put found minimum element in its place
unsigned char temp = window[m];
window[m] = window[min];
window[min] = temp;
}
smooth[ j*width+i ] = window[4];
}
}
}
这把可以清晰地看到, 这里有6,2,0,3,97,4,19,3,10这些像素, 然后中间的这些像素值就被这些像素的中位数也就是中值取代了。为了满足和前面一篇文章的格式相对应, 我们马上进入下一个单元, 来看看在平滑和降噪方面的功效!
原图1 中值滤波之后
噪声图(5%) 中值滤波后:
非常impressive的一点在这里就可以看出来了, 很明显中值滤波不仅是图像变得平滑,同时去除了椒盐噪声(图像最外圈的像素没有去除掉只是因为我没有从0-width处理而已)。从这里中值的逻辑来看, 我们做中值操作的时候, 那么白色(255)和黑色(0)因为是最大最小值, 除非周围的颜色都是黑色或者白色,不然一般都会被剔除掉, 这就是和均值最大的不同! 所以在效果上要好很多。一般来说这个中值滤波是去除椒盐噪声的非常理想的选择。
一样的,最后还是贴一段我运行的代码:
[cpp] view
plaincopy
/**
** 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
*/
void medianFilter (unsigned char* corrupted, unsigned char* smooth, int width, int height)
{
memcpy ( smooth, corrupted, width*height*sizeof(unsigned char) );
for (int j=1;j<height-1;j++)
{
for (int i=1;i<width-1;i++)
{
int k = 0;
unsigned char window[9];
for (int jj = j - 1; jj < j + 2; ++jj)
for (int ii = i - 1; ii < i + 2; ++ii)
window[k++] = corrupted[jj * width + ii];
// Order elements (only half of them)
for (int m = 0; m < 5; ++m)
{
int min = m;
for (int n = m + 1; n < 9; ++n)
if (window
< window[min])
min = n;
// Put found minimum element in its place
unsigned char temp = window[m];
window[m] = window[min];
window[min] = temp;
}
smooth[ j*width+i ] = window[4];
}
}
}
相关文章推荐
- 高斯平滑 高斯模糊 高斯滤波器 ( Gaussian Smoothing, Gaussian Blur, Gaussian Filter ) C++ 实现
- hdu5339-Untitled // BestCoder Round #49 ($) 1001 (搜索)
- 图像锐化算法 C++ 实现
- A Simple Problem with Integers(线段树,区间求和)
- Sliding Window
- C语言:返回两个数组中第一个元素的指针,并输出这个值
- I Hate It
- C语言的标识符
- c++中的函数重载
- 关于C++ const 的全面总结
- iOS开发分分钟搞定C语言 —— 结构体和枚举
- 挖掘机技术哪家强(c++实现)
- C++ 函数的重载 ,重写,隐藏
- C++ 输入函数 cin>>、cin.getline()和cin.get()区别
- C++ 读取键盘输入(cin/cin.getline()/cin.get()/cin.clear())
- 用数组实现队列(C语言版)
- C++对象生命周期(未完)
- 【effective c++读书笔记】【第4章】设计与声明(1)
- 【effective c++读书笔记】【第4章】设计与声明(1)
- 重载重写_覆盖重定义隐藏.cpp