Win8 Metro(C#)数字图像处理--2.67图像最大值滤波器

时间:2023-03-10 02:13:56
Win8 Metro(C#)数字图像处理--2.67图像最大值滤波器

原文:Win8 Metro(C#)数字图像处理--2.67图像最大值滤波器



[函数名称]

  最大值滤波器WriteableBitmap MaxFilterProcess(WriteableBitmap src)

[算法说明]

  最大值滤波属于非线性滤波方法,它是一种基于排序统计理论的可有效抑制噪声的非线性平滑滤波

器,基本原理是把数字图像或数字序列中一点的值用该点的一个临域中各点值的最大值替换。

Win8 Metro(C#)数字图像处理--2.67图像最大值滤波器

[函数代码]

        /// <summary>
/// Max value filter.
/// </summary>
/// <param name="src">The source image.</param>
/// <returns></returns>
public static WriteableBitmap MaxFilterProcess(WriteableBitmap src)////最大值滤波器
{
if (src != null)
{
int w = src.PixelWidth;
int h = src.PixelHeight;
WriteableBitmap filterImage = new WriteableBitmap(w, h);
byte[] temp = src.PixelBuffer.ToArray();
byte[] tempMask = (byte[])temp.Clone();
double[] Gray = new double[9];
double gray = 0;
int tempr = 0, tempb = 0, tempg = 0;
for (int j = 1; j < h - 1; j++)
{
for (int i = 1; i < w - 1; i++)
{
tempb = 0;
tempg = 0;
tempr = 0;
gray = 0;
int[] B = new int[9] { tempMask[i * 4 + j * w * 4], tempMask[(i - 1) * 4 + (j - 1) * w * 4], tempMask[i * 4 + (j - 1) * w * 4], tempMask[(i + 1) * 4 + (j - 1) * w * 4], tempMask[(i - 1) * 4 + j * w * 4], tempMask[(i + 1) * 4 + j * w * 4], tempMask[(i - 1) * 4 + (j + 1) * w * 4], tempMask[i * 4 + (j + 1) * w * 4], tempMask[(i + 1) * 4 + (j + 1) * w * 4] };
int[] G = new int[9] { tempMask[i * 4 + 1 + j * w * 4], tempMask[(i - 1) * 4 + 1 + (j - 1) * w * 4], tempMask[i * 4 + 1 + (j - 1) * w * 4], tempMask[(i + 1) * 4 + 1 + (j - 1) * w * 4], tempMask[(i - 1) * 4 + 1 + j * w * 4], tempMask[(i + 1) * 4 + 1 + j * w * 4], tempMask[(i - 1) * 4 + 1 + (j + 1) * w * 4], tempMask[i * 4 + 1 + (j + 1) * w * 4], tempMask[(i + 1) * 4 + 1 + (j + 1) * w * 4] };
int[] R = new int[9] { tempMask[i * 4 + 2 + j * w * 4], tempMask[(i - 1) * 4 + 2 + (j - 1) * w * 4], tempMask[i * 4 + 2 + (j - 1) * w * 4], tempMask[(i + 1) * 4 + 2 + (j - 1) * w * 4], tempMask[(i - 1) * 4 + 2 + j * w * 4], tempMask[(i + 1) * 4 + 2 + j * w * 4], tempMask[(i - 1) * 4 + 2 + (j + 1) * w * 4], tempMask[i * 4 + 2 + (j + 1) * w * 4], tempMask[(i + 1) * 4 + 2 + (j + 1) * w * 4] };
for (int n = 0; n < 9; n++)
{
Gray[n] = (double)B[n] * 0.114 + (double)G[n] * 0.587 + (double)R[n] * 0.299;
if (gray < Gray[n])
{
gray = Gray[n];
tempb = B[n];
tempr = R[n];
tempg = G[n];
}
}
temp[i * 4 + j * w * 4] = (byte)tempb;
temp[i * 4 + 1 + j * w * 4] = (byte)tempg;
temp[i * 4 + 2 + j * w * 4] = (byte)tempr;
}
}
Stream sTemp = filterImage.PixelBuffer.AsStream();
sTemp.Seek(0, SeekOrigin.Begin);
sTemp.Write(temp, 0, w * 4 * h);
return filterImage;
}
else
{
return null;
}
}

[图像效果]

Win8 Metro(C#)数字图像处理--2.67图像最大值滤波器