Win8Metro(C#)数字图像处理--2.9图像均值滤波

原文: Win8Metro(C#)数字图像处理--2.9图像均值滤波



[函数名称]

图像均值滤波函数MeanFilterProcess(WriteableBitmap src)

Win8Metro(C#)数字图像处理--2.9图像均值滤波_第1张图片

[函数代码]

       ///

       /// Mean filter process.

       ///

       ///Source image

       ///

       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;

           }  

       }

[图像效果]

你可能感兴趣的:(Win8Metro(C#)数字图像处理--2.9图像均值滤波)