码迷,mamicode.com
首页 > 编程语言 > 详细

Win8 Metro(C#)数字图像处理--2.51图像统计滤波算法

时间:2018-03-13 11:04:28      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:href   elb   div   adjust   double   enter   sqrt   ref   else   

原文:Win8 Metro(C#)数字图像处理--2.51图像统计滤波算法

??

[函数名称]

??图像统计滤波???WriteableBitmap?StatisticalFilter(WriteableBitmap?src,double?T)

技术分享图片

/// <summary>
        /// Statistical filtering process.
        /// </summary>
        /// <param name="src">The source image.</param>
        /// <param name="T">The threshould to adjust filter effect.</param>
        /// <returns></returns>
        public static WriteableBitmap StatisticalFilter(WriteableBitmap src,double T)////图像统计滤波
        {
            if (src != null)
            {
                int w = src.PixelWidth;
                int h = src.PixelHeight;
                WriteableBitmap srcImage = new WriteableBitmap(w, h);
                byte[] temp = src.PixelBuffer.ToArray();
                byte[] tempMask = (byte[])temp.Clone();
                double mean = 0;
                double std = 0;
                int[] windowValue = new int[9];
                double mR = 0;
                double mG = 0;
                double mB = 0;
                for (int j = 1; j < h-1; j++)
                {
                    for (int i = 1; i < w-1; i++)
                    {
                        windowValue[0] = (int)(tempMask[(i - 1) * 4 + (j - 1) * w * 4] * 0.114 + tempMask[(i - 1) * 4 + 1 + (j - 1) * w * 4] * 0.587 + tempMask[(i - 1) * 4 + 2 + (j - 1) * w * 4] * 0.299);
                        windowValue[1] = (int)(tempMask[i * 4 + (j - 1) * w * 4] * 0.114 + tempMask[i * 4 + 1 + (j - 1) * w * 4] * 0.587 + tempMask[i * 4 + 2 + (j - 1) * w * 4] * 0.299);
                        windowValue[2] = (int)(tempMask[(i + 1) * 4 + (j - 1) * w * 4] * 0.114 + tempMask[(i + 1) * 4 + 1 + (j - 1) * w * 4] * 0.587 + tempMask[(i + 1) * 4 + 2 + (j - 1) * w * 4] * 0.299);
                        windowValue[3] = (int)(tempMask[(i-1) * 4 + j * w * 4] * 0.114 + tempMask[(i-1) * 4 + 1 + j * w * 4] * 0.587 + tempMask[(i - 1) * 4 + 2 + j * w * 4] * 0.299);
                        windowValue[4] = (int)(tempMask[i * 4 + j * w * 4] * 0.114 + tempMask[i * 4 + 1 + j * w * 4] * 0.587 + tempMask[i * 4 + 2 + j * w * 4] * 0.299);
                        windowValue[5] = (int)(tempMask[(i + 1) * 4 + j * w * 4] * 0.114 + tempMask[(i + 1) * 4 + 1 + j * w * 4] * 0.587 + tempMask[(i + 1) * 4 + 2 + j * w * 4] * 0.299);
                        windowValue[6] = (int)(tempMask[(i - 1) * 4 + (j + 1) * w * 4] * 0.114 + tempMask[(i - 1) * 4 + 1 + (j + 1) * w * 4] * 0.587 + tempMask[(i - 1) * 4 + 2 + (j + 1) * w * 4] * 0.299);
                        windowValue[7] = (int)(tempMask[i * 4 + (j + 1) * w * 4] * 0.114 + tempMask[i * 4 + 1 + (j + 1) * w * 4] * 0.587 + tempMask[i * 4 + 2 + (j + 1) * w * 4] * 0.299);
                        windowValue[8] = (int)(tempMask[(i + 1) * 4 + (j + 1) * w * 4] * 0.114 + tempMask[(i + 1) * 4 + 1 + (j + 1) * w * 4] * 0.587 + tempMask[(i + 1) * 4 + 2 + (j + 1) * w * 4] * 0.299);
                        for (int n = 0; n < 9; n++)
                        {
                            mean += (double)((double)(windowValue[n]) / 9);
                        }
                        for (int n = 0; n < 9; n++)
                        {
                            std += Math.Pow((double)(windowValue[n]) - mean, 2)/9;
                        }
                        if (windowValue[4] >= T * Math.Sqrt(std))
                        {
                            for (int k = -1; k < 2; k++)
                            {
                                for (int m = -1; m < 2; m++)
                                {
                      mB += (double)tempMask[(i + m) * 4 + (j + k) * w * 4] / 9;
                      mG += (double)tempMask[(i + m) * 4 + 1 + (j + k) * w * 4] / 9;
                      mR += (double)tempMask[(i + m) * 4 + 2 + (j + k) * w * 4] / 9;
                                }
                            }
                            temp[i * 4 + j * w * 4] = (byte)mB;
                            temp[i * 4 + 1 + j * w * 4] = (byte)mG;
                            temp[i * 4 + 2 + j * w * 4] = (byte)mR;
                        }
                        mean = 0;
                        std = 0;
                        mR = mG = mB = 0;
                    }
                }
                Stream sTemp = srcImage.PixelBuffer.AsStream();
                sTemp.Seek(0, SeekOrigin.Begin);
                sTemp.Write(temp, 0, w * 4 * h);
                return srcImage;
            }
            else
            {
                return null;
            }
        }
技术分享图片

Win8 Metro(C#)数字图像处理--2.51图像统计滤波算法

标签:href   elb   div   adjust   double   enter   sqrt   ref   else   

原文地址:https://www.cnblogs.com/lonelyxmas/p/8554506.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!