均值滤波是典型的线性滤波算法,它是指在图像上对目标像素给一个模板,该模板包括了其周围的临近像素(以目标象素为中心的周围8个像素,构成一个滤波模板,即去掉目标像素本身),再用模板中的全体像素的平均值来代替原来像素值。均值滤波本身存在着固有的缺陷,即它不能很好地保护图像细节,在图像去噪的同时也破坏了图像的细节部分,从而使图像变得模糊,不能很好地去除噪声点。
图像平滑用于去除图像中的噪声。高斯平滑,就是将每个像素的灰度值用其领域的加权平均值代替。该算法简单,能够有效去除高斯噪声。
高斯平滑模板:

//高斯平滑 中值滤波 均值滤波
#include<cv.h>
#include<highgui.h>
// 高斯平滑
// 1. pImageData 图像数据
// 2. nWidth 图像宽度
// 3. nHeight 图像高度
// 4. nWidthStep 图像行大小
bool SmoothGauss(unsigned char *pImageData, int nWidth, int nHeight, int nWidthStep)
{
int i = 0;
int j = 0;
int nValue = 0;
unsigned char *pLine[3] = { NULL, NULL, NULL };
int nTemplate[9] =
{
1, 2, 1,
2, 4, 2,
1, 2, 1
};
for (j = 1; j < nHeight - 1; j++)
{
pLine[0] = pImageData + nWidthStep * (j - 1); //对应3行3列高斯模板矩阵中的 3行列。
pLine[1] = pImageData + nWidthStep * j;
pLine[2] = pImageData + nWidthStep * (j + 1);
for (i = 1; i < nWidth - 1; i++)
{
nValue =
(pLine[0][i - 1] * nTemplate[0] + //对应3行3列矩阵中的各个点。
pLine[0][i] * nTemplate[1