[函数名称]
图像均值滤波函数MeanFilterProcess(WriteableBitmap src)
[函数代码]
///<summary>
/// Mean filter process.
///</summary>
///<param name="src">Source image</param>
///<returns></returns>
publicstaticWriteableBitmap MeanFilterProcess(WriteableBitmap src)////9均值滤波处理
{
if(src!=null )
{
int w = src.PixelWidth;
int h = src.PixelHeight;
WriteableBitmap filterImage =newWriteableBitmap(w,h);
byte[] temp = src.PixelBuffer.ToArray();
byte[] tempMask = (byte[])temp.Clone();
for (int j = 1; j < h - 1; j++)
{
for (int i = 4; i < w * 4 - 4; i += 4)
{
temp[i + j * w * 4] = (byte)((tempMask[i - 4 + (j - 1) * w * 4] + tempMask[i + (j - 1) * w * 4] + tempMask[i + 4 + (j - 1) * w * 4]
+ tempMask[i - 4 + j * w * 4] + tempMask[i + 4 + j * w * 4] + tempMask[i - 4 + (j + 1) * w * 4] + tempMask[i + (j + 1) * w * 4]
+ tempMask[i + 4 + (j + 1) * w * 4]) / 8);
temp[i + 1 + j * w * 4] = (byte)((tempMask[i - 4 + 1 + (j - 1) * w * 4] + tempMask[i + 1 + (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 + 1 + (j + 1) * w * 4]
+ tempMask[i + 1 + 4 + (j + 1) * w * 4]) / 8);
temp[i + 2 + j * w * 4] = (byte)((tempMask[i + 2 - 4 + (j - 1) * w * 4] + tempMask[i + 2 + (j - 1) * w * 4] + tempMask[i + 2 + 4 + (j - 1) * w * 4]
+ tempMask[i + 2 - 4 + j * w * 4] + tempMask[i + 2 + 4 + j * w * 4] + tempMask[i + 2 - 4 + (j + 1) * w * 4] + tempMask[i + 2 + (j + 1) * w * 4]
+ tempMask[i + 2 + 4 + (j + 1) * w * 4]) / 8);
}
}
Stream sTemp = filterImage.PixelBuffer.AsStream();
sTemp.Seek(0, SeekOrigin.Begin);
sTemp.Write(temp, 0, w * 4 * h);
return filterImage;
}
else
{
returnnull;
}
}
[图像效果]