Win8 Metro(C#)数字图像处理--2.64图像高斯滤波算法
原文: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图像高斯滤波算法的更多相关文章
- Win8 Metro(C#)数字图像处理--2.51图像统计滤波算法
原文:Win8 Metro(C#)数字图像处理--2.51图像统计滤波算法 [函数名称] 图像统计滤波 WriteableBitmap StatisticalFilter(Writeab ...
- Win8 Metro(C#)数字图像处理--2.44图像油画效果算法
原文:Win8 Metro(C#)数字图像处理--2.44图像油画效果算法 [函数名称] 图像油画效果 OilpaintingProcess(WriteableBitmap src ...
- Win8 Metro(C#)数字图像处理--2.43图像马赛克效果算法
原文:Win8 Metro(C#)数字图像处理--2.43图像马赛克效果算法 [函数名称] 图像马赛克效果 MosaicProcess(WriteableBitmap src, i ...
- Win8 Metro(C#)数字图像处理--2.35图像肤色检测算法
原文:Win8 Metro(C#)数字图像处理--2.35图像肤色检测算法 [函数名称] 肤色检测函数SkinDetectProcess(WriteableBitmap src) [算法说明] ...
- Win8 Metro(C#)数字图像处理--3.2图像方差计算
原文:Win8 Metro(C#)数字图像处理--3.2图像方差计算 /// <summary> /// /// </summary>Variance computing. / ...
- Win8 Metro(C#)数字图像处理--3.3图像直方图计算
原文:Win8 Metro(C#)数字图像处理--3.3图像直方图计算 /// <summary> /// Get the array of histrgram. /// </sum ...
- Win8 Metro(C#)数字图像处理--3.4图像信息熵计算
原文:Win8 Metro(C#)数字图像处理--3.4图像信息熵计算 [函数代码] /// <summary> /// Entropy of one image. /// </su ...
- Win8 Metro(C#)数字图像处理--3.5图像形心计算
原文:Win8 Metro(C#)数字图像处理--3.5图像形心计算 /// <summary> /// Get the center of the object in an image. ...
- Win8 Metro(C#)数字图像处理--3.1图像均值计算
原文:Win8 Metro(C#)数字图像处理--3.1图像均值计算 /// <summary> /// Mean value computing. /// </summary> ...
随机推荐
- Android程序解析XML文件的方法及使用PULL解析XML案例
一.一般解析XML文件的方法有SAX和DOM.PULL (1)DOM(JAXP Crimson解析器) DOM是用与平台和语言无关的方式表示XML文档的官方W3C标准.DOM是以层次结构组织的节点或信 ...
- Indy10 控件的使用(2)TidTCpServer组件学习
以下来自英文原版帮助文件,文桓英语不好,翻译了老半天.有错误的地方见谅,别骂我. TIdTCPServer = class(TIdComponent) Description TIdTCPServer ...
- Eclipse Che安装入门和使用(一)
Eclipse Che序列博文如下: 安装和调试篇:Eclipse Che安装入门和使用(一) Web进阶篇:Eclipse Che开发Spring Web应用(入门) (二) 本文摘要: Eclip ...
- javascript中window对象 部分操作
<!--引用javascript外部脚本--> <script src="ss.js"></script> <script> //警 ...
- Android中使用achartengine生成图表
今天在做项目的时候用到了图表功能,记录下来 achartengine是google的一个开源项目,可以在https://code.google.com/p/achartengine/ 下载技术文档,j ...
- 【hdu2222】【poj2945】AC自动机入门题
HDU2222 传送门 题目分析 裸题:注意构建自动机用的是模式串,思想和kmp很类似. code: #include<iostream> #include<cstdio> # ...
- 【27.40%】【codeforces 599D】Spongebob and Squares
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- Windows 7 X64位平台下,VC6调试运行程序,中断调试无法退出
用VC6在64位Windows7下调试的时候,如果中断(Shift+F5)调试,程序无法退出. 问题描述: 当点击F5开始一个项目的调试时,程序在设置的断点处停止,这时按下Shift+F5后,vc6可 ...
- 全文检索(elasticsearch入门)
Elasticsearch篇: Elasticsearch是一个采用java语言开发的,基于Lucene构造的开源,分布式的搜索引擎. 设计用于云计算中,能够达到实时搜索,稳定可靠. Elastics ...
- 【64.22%】【codefoces round 382A】Ostap and Grasshopper
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...