热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

图像处理滤波器(二)——中值滤波器(MedianFilter)

描述:中值滤波器也是为了减少噪声,跟均值滤波器差不多,但是它比均值滤波器保留更多的细节。什么是中值滤波器?中值滤波器也是模板滤波器,不过此处的模板只是一个模板,里面没有数字而已。

描述:中值滤波器也是为了减少噪声,跟均值滤波器差不多,但是它比均值滤波器保留更多的细节。


什么是中值滤波器?


中值滤波器也是模板滤波器,不过此处的模板只是一个模板,里面没有数字而已。就想入下图所示:



此处展示的是一个3*3的模板,由图上可以看出中值滤波器就是取模板覆盖区域的排序之后的中间值作为该模板区域内中心的像素值。


Code:


  /**
* Takes a 2D input image array and a kernel and a pixel location and
* calculates the new pixel value by calculating the median of its
* neighbours.
*
* @param input the 2D image array
* @param kernel the kernel array
* @param w the width of the input image
* @param h the height of the input image
* @param x the x coordinate of the pixel at the centre of the neighbourhood
* @param y the y coordinate of the pixel at the centre of the neighbourhood
* @return the new pixel value
*/
public int medianNeighbour(int [][] input, int [][] kernel,
int w, int h, int x, int y) {

ArrayList values = new ArrayList();
for(int j=0;j<3;++j){
for(int i=0;i<3;++i){
if((kernel[i][j]==1) &&
((x-1+i)>=0) && ((y-1+j)>=0) && ((x-1+i) //System.out.println("Adding a value");
values.add(new Integer(input[x-1+i][y-1+j]));
}
}
}
//System.out.println(values.size());
int m = getMedian(values);
//System.out.println(m);
return m;
}



 /**
* Takes an image and a kernel and smoothes the image the specified number of
* iterations.
*
* @param input the input image array
* @param kernel the kernel array
* @param width of the image
* @param height of the image
* @param iterations to be carried out
* @return the new smoothed image array
*/
public int [][] smooth(int [][] input, int [][] kernel,
int width, int height,
int iterations){
int [][] outputArrays = new int [width][height];
for (int its=0;its for(int j=0;j for(int i=0;i outputArrays[i][j] = medianNeighbour(input,kernel,
width,height,i,j);
}
}
input = (int [][]) outputArrays.clone(); //copy output to input
}
return outputArrays;
}


Input Image:



Output Image:



总结:中值滤波器对于在减少噪声的应用一般要优于均值滤波器,特别是对于"salt and pepper noise"有更好的效果。



推荐阅读
author-avatar
淼淼妈妈的指国度an
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有