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

简介: 原文:Win8Metro(C#)数字图像处理--2.10图像中值滤波  [函数名称] 图像中值滤波函数MedianFilterProcess(WriteableBitmap src) [函数代码]        ///        /// Median filter process.
原文: Win8Metro(C#)数字图像处理--2.10图像中值滤波



[函数名称]

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

[函数代码]

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

       }


目录
相关文章
|
7月前
|
监控 API 开发工具
Baumer工业相机堡盟工业相机如何通过NEOAPI SDK获取每张图像的微秒时间和FrameID功能(C#)
Baumer工业相机堡盟工业相机如何通过NEOAPI SDK获取每张图像的微秒时间和FrameID功能(C#)
94 0
|
7月前
|
存储 传感器 开发工具
Baumer工业相机堡盟工业相机如何通过NEOAPI SDK修改图像像素格式Mono8或者Mono10(C#)
Baumer工业相机堡盟工业相机如何通过NEOAPI SDK修改图像像素格式Mono8或者Mono10(C#)
189 0
|
7月前
|
存储 数据处理 开发工具
Baumer工业相机堡盟工业相机如何通过NEOAPI SDK实现相机的高速图像保存(C#)
Baumer工业相机堡盟工业相机如何通过NEOAPI SDK实现相机的高速图像保存(C#)
73 0
|
7月前
|
监控 算法 开发工具
Baumer工业相机堡盟工业相机如何联合NEOAPI SDK和OpenCV实现获取图像并对图像进行边缘检测(C#)
Baumer工业相机堡盟工业相机如何联合NEOAPI SDK和OpenCV实现获取图像并对图像进行边缘检测(C#)
75 1
|
7月前
|
存储 监控 开发工具
Baumer工业相机堡盟工业相机如何联合NEOAPI SDK和OpenCV实现相机图像转换为AVI视频格式(C#)
Baumer工业相机堡盟工业相机如何联合NEOAPI SDK和OpenCV实现相机图像转换为AVI视频格式(C#)
72 0
|
7月前
|
数据采集 API 开发工具
Baumer工业相机堡盟工业相机如何通过BGAPI SDK实现Raw格式的图像保存(C#)
Baumer工业相机堡盟工业相机如何通过BGAPI SDK实现Raw格式的图像保存(C#)
71 0
|
7月前
|
存储 传感器 监控
Baumer工业相机堡盟工业相机如何通过BGAPISDK将相机图像高速保存到电脑内存(C#)
Baumer工业相机堡盟工业相机如何通过BGAPISDK将相机图像高速保存到电脑内存(C#)
105 0
|
3月前
|
数据采集 JavaScript C#
C#图像爬虫实战:从Walmart网站下载图片
C#图像爬虫实战:从Walmart网站下载图片
|
7月前
|
存储 数据处理 开发工具
Baumer工业相机堡盟工业相机如何通过NEOAPI SDK实现相机图像转换由Mono10转换为Mono8(C#)
Baumer工业相机堡盟工业相机如何通过NEOAPI SDK实现相机图像转换由Mono10转换为Mono8(C#)
90 0
|
7月前
|
存储 新制造 开发工具
Baumer工业相机堡盟工业相机如何联合NEOAPI SDK和OpenCV实现相机图像转换为Mat图像格式(C#)
Baumer工业相机堡盟工业相机如何联合NEOAPI SDK和OpenCV实现相机图像转换为Mat图像格式(C#)
76 0