.net版高斯模糊算法
最近挺多人找高斯算法,本人贴上一个高斯模糊算法类,希望可以帮助到大家。算法的效率还是可以接受的。
#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版高斯模糊算法的更多相关文章
- JavaScript版排序算法
JavaScript版排序算法:冒泡排序.快速排序.插入排序.希尔排序(小数据时,希尔排序会比快排快哦) //排序算法 window.onload = function(){ var array = ...
- 数据结构C语言版 弗洛伊德算法实现
/* 数据结构C语言版 弗洛伊德算法 P191 编译环境:Dev-C++ 4.9.9.2 */ #include <stdio.h>#include <limits.h> # ...
- 简单的java高斯模糊算法
import java.awt.Color; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOEx ...
- SSE图像算法优化系列二:高斯模糊算法的全面优化过程分享(一)。
这里的高斯模糊采用的是论文<Recursive implementation of the Gaussian filter>里描述的递归算法. 仔细观察和理解上述公式,在forward过程 ...
- PHP版常用算法
PHP版常用算法最近准备面试的资料,顺便整理一下以前的基本算法,写个DEMO记录一下 //冒泡//逐行对比,满足条件则交换function bubbleSort($arrData,$sort = 'd ...
- C语言版数据结构算法
C语言版数据结构算法 C语言数据结构具体算法 https://pan.baidu.com/s/19oLoEVqV1I4UxW7D7SlwnQ C语言数据结构演示软件 https://pan.baidu ...
- PHP版DES算法加密数据(3DES)另附openssl_encrypt版本
PHP版DES算法加密数据(3DES) 可与java的DES(DESede/CBC/PKCS5Padding)加密方式兼容 <?php /** * Created by PhpStorm. * ...
- 高斯模糊算法的 C++ 实现
2008 年在一个 PS 讨论群里,有网友不解 Photoshop 的高斯模糊中的半径是什么含义,因此当时我写了这篇文章: 对Photoshop高斯模糊滤镜的算法总结: 在那篇文章中,主要讲解了高斯模 ...
- 朴素版和堆优化版dijkstra和朴素版prim算法比较
1.dijkstra 时间复杂度:O(n^2) n次迭代,每次找到距离集合S最短的点 每次迭代要用找到的点t来更新其他点到S的最短距离. #include<iostream> #inclu ...
随机推荐
- dsp28377控制DM9000收发数据
首先感谢上一篇转载文章的作者给出的参考,下面是一些自己在调试过程中的一些步骤: 首先把代码贴上来: //------------------------------------------------ ...
- apache服务器安装以及使用passenger插件部署rails应用
小例子可以部署在rails自带的WEBrick上,逐渐往后走还得上Apache. 安装apache服务器 命令是sudo apt-get install apache2 安装passenger插件 安 ...
- 特许金融分析师 (CFA) 持证人现在一般在做什么工作?职业分布是怎样的?
特许金融分析师 (CFA) 持证人现在一般在做什么工作?职业分布是怎样的? 陈雨桐 1. 全球范围: 根据 CFA 协会 2014 年 6 月的报告: CFA Institute has over ...
- Android BadgeView使用
BadgeView是第三方的插件,用来显示组件上面的标记,起到提醒的作用,下载地址如下:http://files.cnblogs.com/files/hyyweb/android-viewbadger ...
- oracle 存储过程创建及执行简单实例
1. 创建 CREATE OR REPLACE PROCEDURE getAplage(eNo IN NUMBER,salary OUT NUMBER) AS BEGIN SELECT AplAge ...
- django 模型
一.project 与app之间的关系 1个project中可包含多个app eg:包含两个app的project的结构
- AutoCAD Civil 3D 中缓和曲线的定义
本文对AutoCAD Civil 3D中缓和曲线的定义进行了整理. 原英文网页如下: https://knowledge.autodesk.com/support/autocad-civil-3d/l ...
- AndroidStudio NDK配置使用以及错误集合
Error:Execution failed for task ':app:transformNative_libsWithStripDebugSymbolForDebug'. > java.l ...
- Yii2使用教程
安装 中文文档:http://www.yiichina.com/doc/guide/2.0/start-installation 1,安装 这里我直接下载归档文件,压缩包安装了.composer各种麻 ...
- Configure Apache Virtual Hosts - CentOS 7
Difficulty: 2Time: 15 minutes Want to host websites on your server? Using Apache? Great. This articl ...