原文:Win8 Metro(C#)数字图像处理--2.64图像高斯滤波算法



[函数名称]

  高斯平滑滤波器      GaussFilter(WriteableBitmap src,int radius,double sigma)

[算法说明]

  高斯滤波器实质上是一种信号的滤波器,其用途是信号的平滑处理。它是一类根据高斯函数的

形状来选择权重的线性平滑滤波器,该滤波器对于抑制服从正态分布的噪声非常有效。高斯函数

的公式如下所示:

private static double[,] GaussFuc(int r, double sigma)
{
int size = 2 * r + 1;
double[,] gaussResult = new double[size, size];
double k = 0.0;
for (int y = -r, h = 0; y <= r; y++, h++)
{
for (int x = -r, w = 0; x <= r; x++, w++)
{
gaussResult[w, h] = (1.0 / (2.0 * Math.PI * sigma * sigma)) * (Math.Exp(-((double)x * (double)x + (double)y * (double)y) / (2.0 * sigma * sigma)));
k += gaussResult[w, h];
}
}
return gaussResult;
}

我们设置参数r=1,sigma=1.0,则得到一个3*3的高斯模板如下:

这个公式可以理解为先对图像按行进行一次一维高斯滤波,在对结果图像按列进行一次一维高斯滤波,这样速度将大大提高。

一维高斯滤波代码如下(包含归一化):

private static double[] GaussKernel1D(int r, double sigma)
{
double[] filter = new double[2 * r + 1];
double sum = 0.0;
for (int i = 0; i < filter.Length; i++)
{
filter[i] = Math.Exp((double)(-(i - r) * (i - r)) / (2.0 * sigma * sigma));
sum += filter[i];
}
for (int i = 0; i < filter.Length; i++)
{
filter[i] = filter[i] / sum;
}
return filter;
}

[函数代码]

        private static double[] GaussKernel(int radius, double sigma)
{
int length=2*radius+1;
double[] kernel = new double[length];
double sum = 0.0;
for (int i = 0; i < length; i++)
{
kernel[i] = Math.Exp((double)(-(i - radius) * (i - radius)) / (2.0 * sigma * sigma));
sum += kernel[i];
}
for (int i = 0; i < length; i++)
{
kernel[i] = kernel[i] / sum;
}
return kernel;
}
/// <summary>
/// Gauss filter process
/// </summary>
/// <param name="src">The source image.</param>
/// <param name="radius">The radius of gauss kernel,from 0 to 100.</param>
/// <param name="sigma">The convince of gauss kernel, from 0 to 30.</param>
/// <returns></returns>
public static WriteableBitmap GaussFilter(WriteableBitmap src,int radius,double sigma) ////高斯滤波
{
if (src != null)
{
int w = src.PixelWidth;
int h = src.PixelHeight;
WriteableBitmap srcImage = new WriteableBitmap(w, h);
byte[] srcValue = src.PixelBuffer.ToArray();
byte[] tempValue=(byte[])srcValue.Clone();
double[] kernel = GaussKernel(radius, sigma);
double tempB = 0.0, tempG = 0.0, tempR = 0.0;
int rem = 0;
int t = 0;
int v = 0;
double K = 0.0;
for (int y = 0; y < h; y++)
{
for (int x = 0; x < w; x++)
{
tempB = tempG = tempR = 0.0;
for (int k = -radius; k <= radius; k++)
{
rem = (Math.Abs(x + k) % w);
t = rem * 4 + y * w * 4;
K=kernel[k+radius];
tempB += srcValue[t] * K;
tempG += srcValue[t + 1] * K;
tempR += srcValue[t + 2] * K;
}
v = x * 4 + y * w * 4;
tempValue[v] = (byte)tempB;
tempValue[v + 1] = (byte)tempG;
tempValue[v + 2] = (byte)tempR;
}
}
for (int x = 0; x < w; x++)
{
for (int y = 0; y < h; y++)
{
tempB = tempG = tempR = 0.0;
for (int k = -radius; k <= radius; k++)
{
rem = (Math.Abs(y + k) % h);
t = rem * w * 4 + x * 4;
K = kernel[k + radius];
tempB += tempValue[t] * K;
tempG += tempValue[t + 1] * K;
tempR += tempValue[t + 2] * K;
}
v = x * 4 + y * w * 4;
srcValue[v] = (byte)tempB;
srcValue[v + 1] = (byte)tempG;
srcValue[v + 2] = (byte)tempR;
}
}
Stream sTemp = srcImage.PixelBuffer.AsStream();
sTemp.Seek(0, SeekOrigin.Begin);
sTemp.Write(srcValue, 0, w * 4 * h);
return srcImage;
}
else
{
return null;
}
}



最后,分享一个专业的图像处理网站(微像素),里面有很多源代码下载:

Win8 Metro(C#)数字图像处理--2.64图像高斯滤波算法的更多相关文章

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

    原文:Win8 Metro(C#)数字图像处理--2.51图像统计滤波算法  [函数名称]   图像统计滤波   WriteableBitmap StatisticalFilter(Writeab ...

  2. Win8 Metro(C#)数字图像处理--2.44图像油画效果算法

    原文:Win8 Metro(C#)数字图像处理--2.44图像油画效果算法  [函数名称]   图像油画效果      OilpaintingProcess(WriteableBitmap src ...

  3. Win8 Metro(C#)数字图像处理--2.43图像马赛克效果算法

    原文:Win8 Metro(C#)数字图像处理--2.43图像马赛克效果算法  [函数名称] 图像马赛克效果        MosaicProcess(WriteableBitmap src, i ...

  4. Win8 Metro(C#)数字图像处理--2.35图像肤色检测算法

    原文:Win8 Metro(C#)数字图像处理--2.35图像肤色检测算法  [函数名称] 肤色检测函数SkinDetectProcess(WriteableBitmap src) [算法说明] ...

  5. Win8 Metro(C#)数字图像处理--3.2图像方差计算

    原文:Win8 Metro(C#)数字图像处理--3.2图像方差计算 /// <summary> /// /// </summary>Variance computing. / ...

  6. Win8 Metro(C#)数字图像处理--3.3图像直方图计算

    原文:Win8 Metro(C#)数字图像处理--3.3图像直方图计算 /// <summary> /// Get the array of histrgram. /// </sum ...

  7. Win8 Metro(C#)数字图像处理--3.4图像信息熵计算

    原文:Win8 Metro(C#)数字图像处理--3.4图像信息熵计算 [函数代码] /// <summary> /// Entropy of one image. /// </su ...

  8. Win8 Metro(C#)数字图像处理--3.5图像形心计算

    原文:Win8 Metro(C#)数字图像处理--3.5图像形心计算 /// <summary> /// Get the center of the object in an image. ...

  9. Win8 Metro(C#)数字图像处理--3.1图像均值计算

    原文:Win8 Metro(C#)数字图像处理--3.1图像均值计算 /// <summary> /// Mean value computing. /// </summary> ...

随机推荐

  1. PatentTips - Integrated circuit well bias circuitry

    1. Field of the Invention This invention relates in general to an integrated circuit and more specif ...

  2. Qt 学习: 视图选择 (QItemSelectionModel)

    博主QQ:1356438802 选择是视图中经常使用的一个操作.在列表.树或者表格中,通过鼠标点击能够选中某一项,被选中项会变成高亮或者反色.在 Qt 中,选择也是使用了一种模型.在 model/vi ...

  3. 最新国内外可用SVN托管仓库有哪些

    最新国内外可用SVN托管仓库哪些 一.总结 一句话总结:用SVNBucket和SourceForge 二.最新国内外可用SVN托管仓库推荐 这几年很多SVN托管平台都基本不维护或者直接关闭了,我翻遍了 ...

  4. 30行js rem弹性布局适配所有分辨率

    <script> /* # 按照宽高比例设定html字体, width=device-width initial-scale=1版 # @pargam win 窗口window对象 # @ ...

  5. [SVG] Add an SVG as an Embedded Background Image

    Learn how to set an elements background image to embedded SVG. This method has an added benefit of n ...

  6. 【codeforces 787A】The Monster

    [题目链接]:http://codeforces.com/contest/787/problem/A [题意] 把b一直加a->得到x 把d一直加c->得到y 然后问你x和y可不可能有相同 ...

  7. 配置ANDROID_HOME

    原文:配置ANDROID_HOME 1.在环境变量中设置一个名为ANDROID_HOME,变量值为SDK路径 2.添加至Path中 备注:ANDROID_HOME的变量值仅允许一个

  8. Vue挂载元素的替换

    Vue根组件已有挂载DOM'#app',在render又引进一个组件,该组件最外层也是用了'#app',为何根组件的DOM'#app'会被替换掉. //main.js import Vue from ...

  9. 一:redis 的string类型 - 相关操作

    *redisclient使用: =============一类:string的方法================ 介绍:string是redis的最简单类型,一个key相应一个value,strin ...

  10. JScript分割字符串

    作者:朱金灿 来源:http://blog.csdn.net/clever101 不废话了,直接用代码说明吧: try { var ss = new Array(); var str="12 ...