图像均值平滑
图像平滑用于去除图像中的噪声。均值平滑,就是将每个像素的灰度值用其领域的平均值代替。该算法简单,速度快,但不能完全消除椒盐噪声。
平滑模板:
// 均值平滑
// 1. pImageData 图像数据
// 2. nWidth 图像宽度
// 3. nHeight 图像高度
// 4. nWidthStep 图像行大小
BOOL SmoothBlur(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 };
for (j = 1; j < nHeight - 1; j++)
{
pLine[0 = pImageData + nWidthStep * (j - 1);
pLine[1 = pImageData + nWidthStep * j;
pLine[2 = pImageData + nWidthStep * (j + 1);
for (i = 1; i < nWidth - 1; i++)
{
nValue =
(pLine[0][i-1 + pLine[0][i + pLine[0][i+1 +
pLine[1][i-1 + pLine[1][i + pLine[1][i+1 +
pLine[2][i-1 + pLine[2][i + pLine[2][i+1]) / 9;
pLine[0][i-1 = (unsigned char) nValue;
}
}
return TRUE;
}
均值平滑效果:
平滑模板:
// 均值平滑
// 1. pImageData 图像数据
// 2. nWidth 图像宽度
// 3. nHeight 图像高度
// 4. nWidthStep 图像行大小
BOOL SmoothBlur(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 };
for (j = 1; j < nHeight - 1; j++)
{
pLine[0 = pImageData + nWidthStep * (j - 1);
pLine[1 = pImageData + nWidthStep * j;
pLine[2 = pImageData + nWidthStep * (j + 1);
for (i = 1; i < nWidth - 1; i++)
{
nValue =
(pLine[0][i-1 + pLine[0][i + pLine[0][i+1 +
pLine[1][i-1 + pLine[1][i + pLine[1][i+1 +
pLine[2][i-1 + pLine[2][i + pLine[2][i+1]) / 9;
pLine[0][i-1 = (unsigned char) nValue;
}
}
return TRUE;
}
均值平滑效果: