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

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


什么是中值滤波器?


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



此处展示的是一个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)



 /**
   * 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


Input Image:



Output Image:



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


你可能感兴趣的:(#,Image,Processing-Base)