描述:均值滤波器是图像处理中一种常见的滤波器,它主要应用于平滑噪声。它的原理主要是利用某像素点周边像素的平均值来打到平滑噪声的效果。
常用的均值核如下图所示:
图像滤波器操作实际上就是模板操作,对于模板操作我们应该注意边界问题:
什么是边界问题?
对于边界问题就是当图像处理边界像素的时候,卷积核与图像使用区域不能匹配,计算出现问题。
处理方法:
1、忽略边界像素,即丢掉不能匹配的像素
2、保留边界像素,即复制源图像的不能匹配的边界像素到输出图像
Code:
/**
* Calculates the mean of a 3x3 pixel neighbourhood (including centre pixel).
*
* @param input the input image 2D array
* @param kernel the kernel 2D array
* @param w the image width
* @param h the image height
* @param x the x coordinate of the centre pixel of the array
* @param y the y coordinate of the centre pixel of the array