Win8Metro(C#)数字图像处理--2.10图像中值滤波

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



[函数名称]

图像中值滤波函数MedianFilterProcess(WriteableBitmap src)

Win8Metro(C#)数字图像处理--2.10图像中值滤波

[函数代码]

       ///<summary>

       /// Median filter process.

       ///</summary>

       ///<param name="src">Source image.</param>

       ///<returns></returns>

       publicstaticWriteableBitmap MedianFilterProcess(WriteableBitmap src)////10中值滤波处理

       {

           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();

           int v1 = 0, v2 = 0, v3 = 0, v4 = 0, v5 = 0, v6 = 0, v7 = 0, v8 = 0, t = 0;

           for (int j = 1; j < h - 1; j++)

           {

               for (int i = 4; i < w * 4 - 4; i += 4)

               {

                   v1 = (int)(temp[i - 4 + (j - 1) * w * 4] * 0.114 + temp[i - 4 + 1 + (j - 1) * w * 4] * 0.587 + temp[i - 4 + 2 + (j - 1) * w * 4] * 0.299);

                   v2 = (int)(temp[i + (j - 1) * w * 4] * 0.114 + temp[i + 1 + (j - 1) * w * 4] * 0.587 + temp[i + 2 + (j - 1) * w * 4] * 0.299);

                   v3 = (int)(temp[i + 4 + (j - 1) * w * 4] * 0.114 + temp[i + 4 + 1 + (j - 1) * w * 4] * 0.587 + temp[i + 4 + 2 + (j - 1) * w * 4] * 0.299);

                   v4 = (int)(temp[i - 4 + j * w * 4] * 0.114 + temp[i - 4 + 1 + j * w * 4] * 0.587 + temp[i - 4 + 2 + j * w * 4] * 0.299);

                   v5 = (int)(temp[i + 4 + j * w * 4] * 0.114 + temp[i + 4 + 1 + j * w * 4] * 0.587 + temp[i + 4 + 2 + j * w * 4] * 0.299);

                   v6 = (int)(temp[i - 4 + (j + 1) * w * 4] * 0.114 + temp[i - 4 + 1 + (j + 1) *w * 4] * 0.587 + temp[i - 4 + 2 + (j + 1) * w * 4] * 0.299);

                   v7 = (int)(temp[i + (j + 1) * w * 4] * 0.114 + temp[i + 1 + (j + 1) * w * 4] * 0.587 + temp[i + 2 + (j + 1) * w * 4] * 0.299);

                   v8 = (int)(temp[i + 4 + (j + 1) * w * 4] * 0.114 + temp[i + 4 + 1 + (j + 1) * w * 4] * 0.587 + temp[i + 4 + 2 + (j + 1) * w * 4] * 0.299);

                   t = GetMedianValue(v1, v2, v3, v4, v5, v6, v7, v8);                 

                   if(t==v1)

                   {

                           temp[i + j * w * 4] = (byte)tempMask[i - 4 + (j - 1) * w * 4];

                           temp[i + 1 + j * w * 4] = (byte)tempMask[i - 4 + 1 + (j - 1) * w * 4];

                           temp[i + 2 + j * w* 4] = (byte)tempMask[i - 4 + 2 + (j - 1) * w * 4];

                   }

 

                   elseif(t==v2)

                   {

                           temp[i + j * w * 4] = (byte)tempMask[i + (j - 1) * w * 4];

                           temp[i + 1 + j * w * 4] = (byte)tempMask[i + 1 + (j - 1) * w * 4];

                           temp[i + 2 + j * w * 4] = (byte)tempMask[i + 2 + (j - 1) * w * 4];

                   }

                   elseif(t==v3)

                   {

                           temp[i + j * w * 4] = (byte)tempMask[i + 4 + (j - 1) * w * 4];

                           temp[i + 1 + j * w * 4] = (byte)tempMask[i + 1 + 4 + (j - 1) * w * 4];

                           temp[i + 2 + j * w * 4] = (byte)tempMask[i + 2 + 4 + (j - 1) * w * 4];

                   }

                   elseif(t==v4)

                   {

                           temp[i + j * w * 4] = (byte)tempMask[i - 4 + j * w * 4];

                           temp[i + 1 + j * w * 4] = (byte)tempMask[i - 4 + 1 + j * w * 4];

                           temp[i + 2 + j * w * 4] = (byte)tempMask[i - 4 + 2 + j * w * 4];

                   }

                   elseif(t==v5)

                   {

                           temp[i + j * w * 4] = (byte)tempMask[i + 4 + j * w * 4];

                           temp[i + 1 + j * w * 4] = (byte)tempMask[i + 4 + 1 + j * w * 4];

                           temp[i + 2 + j * w * 4] = (byte)tempMask[i + 4 + 2 + j * w * 4];

                   }

                   elseif(t==v6)

                   {

                           temp[i + j * w * 4] = (byte)tempMask[i - 4 + (j + 1) * w * 4];

                           temp[i + 1 + j * w * 4] = (byte)tempMask[i - 4 + 1 + (j + 1) * w * 4];

                           temp[i + 2 + j * w * 4] = (byte)tempMask[i - 4 + 2 + (j + 1) * w * 4];

                   }

                   elseif (t == v7)

                   {

                       temp[i + j * w * 4] = (byte)tempMask[i + (j + 1) * w * 4];

                       temp[i + 1 + j * w * 4] = (byte)tempMask[i + 1 + (j + 1) * w * 4];

                       temp[i + 2 + j * w * 4] = (byte)tempMask[i + 2 + (j + 1) * w * 4];

                   }

                   else

                   {

                       temp[i + j * w * 4] = (byte)tempMask[i + 4 + (j + 1) * w * 4];

                       temp[i + 1 + j * w * 4] = (byte)tempMask[i + 4 + 1 + (j + 1) * w * 4];

                       temp[i + 2 + j * w * 4] = (byte)tempMask[i + 4 + 2 + (j + 1) * w * 4];

                   }

                   v1 = 0; v2 = 0; v3 = 0; v4 = 0; v5 = 0; v6 = 0; v7 = 0; v8 = 0; t = 0;

               }

           }

           Stream sTemp = filterImage.PixelBuffer.AsStream();

           sTemp.Seek(0,SeekOrigin.Begin);

           sTemp.Write(temp, 0, w * 4 * h);

           return filterImage;

           }

           else

           {

               returnnull;

           }  

       }

       privatestaticint GetMedianValue(paramsint[] src)

       {

           int w = src.Length;

           int temp = src[0], m = 0;

           for (int i = 1; i < (int)(w / 2); i++)

           {

               if (src[i] < temp)

               {

                   m = src[i];

                   src[i] = temp;

                   temp = m;

               }

               else

                   continue;

           }

           return (int)((src[(int)(w / 2)] + src[(int)(-1 + w / 2)]) / 2);

       }

Win8Metro(C#)数字图像处理--2.10图像中值滤波


上一篇:简单3步,云盒子企业网盘的文档权限设置


下一篇:归纳一下:C#线程同步的几种方法