作者:淼淼妈妈的指国度an | 来源:互联网 | 2023-05-18 17:47
描述:中值滤波器也是为了减少噪声,跟均值滤波器差不多,但是它比均值滤波器保留更多的细节。什么是中值滤波器?中值滤波器也是模板滤波器,不过此处的模板只是一个模板,里面没有数字而已。
描述:中值滤波器也是为了减少噪声,跟均值滤波器差不多,但是它比均值滤波器保留更多的细节。
什么是中值滤波器?
中值滤波器也是模板滤波器,不过此处的模板只是一个模板,里面没有数字而已。就想入下图所示:
此处展示的是一个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"有更好的效果。