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

 #region 高斯模糊算法
/// <summary>
/// 高斯模糊算法
/// </summary>
using System ;
using System .Drawing ;
public class Gaussian
{
public static double[,] Calculate1DSampleKernel(double deviation, int size)
{
double[,] ret = new double[size, ];
double sum = ;
int half = size / ;
for (int i = ; i < size; i++)
{
ret[i, ] = / (Math.Sqrt( * Math.PI) * deviation) * Math.Exp(-(i - half) * (i - half) / ( * deviation * deviation));
sum += ret[i, ];
}
return ret;
}
public static double[,] Calculate1DSampleKernel(double deviation)
{
int size = (int)Math.Ceiling(deviation * ) * + ;
return Calculate1DSampleKernel(deviation, size);
}
public static double[,] CalculateNormalized1DSampleKernel(double deviation)
{
return NormalizeMatrix(Calculate1DSampleKernel(deviation));
}
public static double[,] NormalizeMatrix(double[,] matrix)
{
double[,] ret = new double[matrix.GetLength(), matrix.GetLength()];
double sum = ;
for (int i = ; i < ret.GetLength(); i++)
{
for (int j = ; j < ret.GetLength(); j++)
sum += matrix[i, j];
}
if (sum != )
{
for (int i = ; i < ret.GetLength(); i++)
{
for (int j = ; j < ret.GetLength(); j++)
ret[i, j] = matrix[i, j] / sum;
}
}
return ret;
}
public static double[,] GaussianConvolution(double[,] matrix, double deviation)
{
double[,] kernel = CalculateNormalized1DSampleKernel(deviation);
double[,] res1 = new double[matrix.GetLength(), matrix.GetLength()];
double[,] res2 = new double[matrix.GetLength(), matrix.GetLength()];
//x-direction
for (int i = ; i < matrix.GetLength(); i++)
{
for (int j = ; j < matrix.GetLength(); j++)
res1[i, j] = processPoint(matrix, i, j, kernel, );
}
//y-direction
for (int i = ; i < matrix.GetLength(); i++)
{
for (int j = ; j < matrix.GetLength(); j++)
res2[i, j] = processPoint(res1, i, j, kernel, );
}
return res2;
}
private static double processPoint(double[,] matrix, int x, int y, double[,] kernel, int direction)
{
double res = ;
int half = kernel.GetLength() / ;
for (int i = ; i < kernel.GetLength(); i++)
{
int cox = direction == ? x + i - half : x;
int coy = direction == ? y + i - half : y;
if (cox >= && cox < matrix.GetLength() && coy >= && coy < matrix.GetLength())
{
res += matrix[cox, coy] * kernel[i, ];
}
}
return res;
}
/// <summary>
/// 对颜色值进行灰色处理
/// </summary>
/// <param name="cr"></param>
/// <returns></returns>
private Color grayscale(Color cr)
{
return Color.FromArgb(cr.A, (int)(cr.R * . + cr.G * . + cr.B * 0.11),
(int)(cr.R * . + cr.G * . + cr.B * 0.11),
(int)(cr.R * . + cr.G * . + cr.B * 0.11));
}
/// <summary>
/// 对图片进行高斯模糊
/// </summary>
/// <param name="d">模糊数值,数值越大模糊越很</param>
/// <param name="image">一个需要处理的图片</param>
/// <returns></returns>
public Bitmap FilterProcessImage(double d, Bitmap image)
{
Bitmap ret = new Bitmap(image.Width, image.Height);
Double[,] matrixR = new Double[image.Width, image.Height];
Double[,] matrixG = new Double[image.Width, image.Height];
Double[,] matrixB = new Double[image.Width, image.Height];
for (int i = ; i < image.Width; i++)
{
for (int j = ; j < image.Height; j++)
{
//matrix[i, j] = grayscale(image.GetPixel(i, j)).R;
matrixR[i, j] = image.GetPixel(i, j).R;
matrixG[i, j] = image.GetPixel(i, j).G;
matrixB[i, j] = image.GetPixel(i, j).B;
}
}
matrixR = Gaussian.GaussianConvolution(matrixR, d);
matrixG = Gaussian.GaussianConvolution(matrixG, d);
matrixB = Gaussian.GaussianConvolution(matrixB, d);
for (int i = ; i < image.Width; i++)
{
for (int j = ; j < image.Height; j++)
{
Int32 R = (int)Math.Min(, matrixR[i, j]);
Int32 G = (int)Math.Min(, matrixG[i, j]);
Int32 B = (int)Math.Min(, matrixB[i, j]);
ret.SetPixel(i, j, Color.FromArgb(R, G, B));
}
}
return ret;
}
}
#endregion

.net版高斯模糊算法的更多相关文章

  1. JavaScript版排序算法

    JavaScript版排序算法:冒泡排序.快速排序.插入排序.希尔排序(小数据时,希尔排序会比快排快哦) //排序算法 window.onload = function(){ var array = ...

  2. 数据结构C语言版 弗洛伊德算法实现

    /* 数据结构C语言版 弗洛伊德算法  P191 编译环境:Dev-C++ 4.9.9.2 */ #include <stdio.h>#include <limits.h> # ...

  3. 简单的java高斯模糊算法

    import java.awt.Color; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOEx ...

  4. SSE图像算法优化系列二:高斯模糊算法的全面优化过程分享(一)。

    这里的高斯模糊采用的是论文<Recursive implementation of the Gaussian filter>里描述的递归算法. 仔细观察和理解上述公式,在forward过程 ...

  5. PHP版常用算法

    PHP版常用算法最近准备面试的资料,顺便整理一下以前的基本算法,写个DEMO记录一下 //冒泡//逐行对比,满足条件则交换function bubbleSort($arrData,$sort = 'd ...

  6. C语言版数据结构算法

    C语言版数据结构算法 C语言数据结构具体算法 https://pan.baidu.com/s/19oLoEVqV1I4UxW7D7SlwnQ C语言数据结构演示软件 https://pan.baidu ...

  7. PHP版DES算法加密数据(3DES)另附openssl_encrypt版本

    PHP版DES算法加密数据(3DES) 可与java的DES(DESede/CBC/PKCS5Padding)加密方式兼容 <?php /** * Created by PhpStorm. * ...

  8. 高斯模糊算法的 C++ 实现

    2008 年在一个 PS 讨论群里,有网友不解 Photoshop 的高斯模糊中的半径是什么含义,因此当时我写了这篇文章: 对Photoshop高斯模糊滤镜的算法总结: 在那篇文章中,主要讲解了高斯模 ...

  9. 朴素版和堆优化版dijkstra和朴素版prim算法比较

    1.dijkstra 时间复杂度:O(n^2) n次迭代,每次找到距离集合S最短的点 每次迭代要用找到的点t来更新其他点到S的最短距离. #include<iostream> #inclu ...

随机推荐

  1. zoj3806Incircle and Circumcircle

    链接 自己的本本没有装画图软件,先借用两张图片..博客园不让贴源地址... 可以想到对于一个确定的外接圆的三角形来说内切圆最大的时候为等边三角形,如下图: 确定有合法的解之后,接下来就是去找这个解,解 ...

  2. winform右下角弹窗

    网页是否经常在电脑右下角弹窗显示消息?其实Winform也是可以实现的.下面介绍两种方法. 第一步:设计窗体 第二步:实现代码 第一种方法 引用user32 声明常量 窗体Load事件 窗体FormC ...

  3. laravel select 传参

    传值: $params['select'] = 'taobao_id,title,image,price,coupon_deduct,coupon_condition'; 接受参数 $result = ...

  4. Webpack、Browserify和Gulp

    https://www.zhihu.com/question/37020798 https://www.zhihu.com/question/35479764

  5. PBOC金融IC卡,卡片与终端交互的13个步骤,简介-第一组(转)

    两个PPT结合起来--一些基础介绍--每一步的详细细节还要去研读文档 EMV-全球标准PBOC-国内标准 ----------------------一:必选:应用选择应用选择的方法:目录选择法.AI ...

  6. Evolutionary Computing: 5. Evolutionary Strategies(2)

    Resource: Introduction to Evolutionary Computing, A.E.Eliben Outline recombination parent selection ...

  7. restore database

    RESTORE DATABASE CTSDW FROM DISK = '\\detego-ctsetl\Backup\CTSDW\CTSDW_backup_20160722110003_Full.ba ...

  8. 关于unity碰撞检测器的用法

    今天已经是我第三次忘记了这两种碰撞检测的用法,混淆了.特意整理一下 首先把今天要解决涉及到的东西列出来 碰撞方法: public void OnTriggerEnter(Collider other) ...

  9. Python anaconda links to GOMP_4.0 and throws error

    ImportError: /usr/progtools/anaconda2/bin/../lib/libgomp.so.1: version `GOMP_4.0' not found (require ...

  10. js效果-多选只能选两项,如果超出自动取消第一次选的

    这个效果很有意思,个人觉得难点在于点击选中状态的多选的数组操作,以下是代码,感谢落梨 <!DOCTYPE> <html> <head> <title> ...