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

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



[函数名称]

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

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

[函数代码]

       ///<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;

           }  

       }

[图像效果]

Win8Metro(C#)数字图像处理--2.9图像均值滤波
上一篇:行内元素居中


下一篇:Exchange企业实战技巧(14)配置Exchange 2010存档邮箱