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

.net版高斯模糊算法

时间:2015-09-23 14:59:30      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:

最近挺多人找高斯算法,本人贴上一个高斯模糊算法类,希望可以帮助到大家。算法的效率还是可以接受的。

技术分享
  1 #region 高斯模糊算法
  2 /// <summary>
  3 /// 高斯模糊算法
  4 /// </summary>
  5 using System ;
  6 using System .Drawing ;
  7 public class Gaussian
  8 {
  9     public static double[,] Calculate1DSampleKernel(double deviation, int size)
 10     {
 11         double[,] ret = new double[size, 1];
 12         double sum = 0;
 13         int half = size / 2;
 14         for (int i = 0; i < size; i++)
 15         {
 16             ret[i, 0] = 1 / (Math.Sqrt(2 * Math.PI) * deviation) * Math.Exp(-(i - half) * (i - half) / (2 * deviation * deviation));
 17             sum += ret[i, 0];
 18         }
 19         return ret;
 20     }
 21     public static double[,] Calculate1DSampleKernel(double deviation)
 22     {
 23         int size = (int)Math.Ceiling(deviation * 3) * 2 + 1;
 24         return Calculate1DSampleKernel(deviation, size);
 25     }
 26     public static double[,] CalculateNormalized1DSampleKernel(double deviation)
 27     {
 28         return NormalizeMatrix(Calculate1DSampleKernel(deviation));
 29     }
 30     public static double[,] NormalizeMatrix(double[,] matrix)
 31     {
 32         double[,] ret = new double[matrix.GetLength(0), matrix.GetLength(1)];
 33         double sum = 0;
 34         for (int i = 0; i < ret.GetLength(0); i++)
 35         {
 36             for (int j = 0; j < ret.GetLength(1); j++)
 37                 sum += matrix[i, j];
 38         }
 39         if (sum != 0)
 40         {
 41             for (int i = 0; i < ret.GetLength(0); i++)
 42             {
 43                 for (int j = 0; j < ret.GetLength(1); j++)
 44                     ret[i, j] = matrix[i, j] / sum;
 45             }
 46         }
 47         return ret;
 48     }
 49     public static double[,] GaussianConvolution(double[,] matrix, double deviation)
 50     {
 51         double[,] kernel = CalculateNormalized1DSampleKernel(deviation);
 52         double[,] res1 = new double[matrix.GetLength(0), matrix.GetLength(1)];
 53         double[,] res2 = new double[matrix.GetLength(0), matrix.GetLength(1)];
 54         //x-direction
 55         for (int i = 0; i < matrix.GetLength(0); i++)
 56         {
 57             for (int j = 0; j < matrix.GetLength(1); j++)
 58                 res1[i, j] = processPoint(matrix, i, j, kernel, 0);
 59         }
 60         //y-direction
 61         for (int i = 0; i < matrix.GetLength(0); i++)
 62         {
 63             for (int j = 0; j < matrix.GetLength(1); j++)
 64                 res2[i, j] = processPoint(res1, i, j, kernel, 1);
 65         }
 66         return res2;
 67     }
 68     private static double processPoint(double[,] matrix, int x, int y, double[,] kernel, int direction)
 69     {
 70         double res = 0;
 71         int half = kernel.GetLength(0) / 2;
 72         for (int i = 0; i < kernel.GetLength(0); i++)
 73         {
 74             int cox = direction == 0 ? x + i - half : x;
 75             int coy = direction == 1 ? y + i - half : y;
 76             if (cox >= 0 && cox < matrix.GetLength(0) && coy >= 0 && coy < matrix.GetLength(1))
 77             {
 78                 res += matrix[cox, coy] * kernel[i, 0];
 79             }
 80         }
 81         return res;
 82     }
 83     /// <summary>
 84     /// 对颜色值进行灰色处理
 85     /// </summary>
 86     /// <param name="cr"></param>
 87     /// <returns></returns>
 88     private Color grayscale(Color cr)
 89     {
 90         return Color.FromArgb(cr.A, (int)(cr.R * .3 + cr.G * .59 + cr.B * 0.11),
 91                               (int)(cr.R * .3 + cr.G * .59 + cr.B * 0.11),
 92                               (int)(cr.R * .3 + cr.G * .59 + cr.B * 0.11));
 93     }
 94     /// <summary>
 95     /// 对图片进行高斯模糊
 96     /// </summary>
 97     /// <param name="d">模糊数值,数值越大模糊越很</param>
 98     /// <param name="image">一个需要处理的图片</param>
 99     /// <returns></returns>
100     public Bitmap FilterProcessImage(double d, Bitmap image)
101     {
102         Bitmap ret = new Bitmap(image.Width, image.Height);
103         Double[,] matrixR = new Double[image.Width, image.Height];
104         Double[,] matrixG = new Double[image.Width, image.Height];
105         Double[,] matrixB = new Double[image.Width, image.Height];
106         for (int i = 0; i < image.Width; i++)
107         {
108             for (int j = 0; j < image.Height; j++)
109             {
110                 //matrix[i, j] = grayscale(image.GetPixel(i, j)).R;
111                 matrixR[i, j] = image.GetPixel(i, j).R;
112                 matrixG[i, j] = image.GetPixel(i, j).G;
113                 matrixB[i, j] = image.GetPixel(i, j).B;
114             }
115         }
116         matrixR = Gaussian.GaussianConvolution(matrixR, d);
117         matrixG = Gaussian.GaussianConvolution(matrixG, d);
118         matrixB = Gaussian.GaussianConvolution(matrixB, d);
119         for (int i = 0; i < image.Width; i++)
120         {
121             for (int j = 0; j < image.Height; j++)
122             {
123                 Int32 R = (int)Math.Min(255, matrixR[i, j]);
124                 Int32 G = (int)Math.Min(255, matrixG[i, j]);
125                 Int32 B = (int)Math.Min(255, matrixB[i, j]);
126                 ret.SetPixel(i, j, Color.FromArgb(R, G, B));
127             }
128         }
129         return ret;
130     }
131 }
132 #endregion
View Code

 

.net版高斯模糊算法

标签:

原文地址:http://www.cnblogs.com/CJSTONE/p/4831945.html

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