/// <summary>
/// 图像模糊化
/// </summary>
/// <param name="bitmap">原始图像</param>
/// <returns>模糊化后的图像</returns>
public static Bitmap Blur(Bitmap bitmap, int radius, float sigma)
{
if (bitmap == null)
{
return null;
}
int width = bitmap.Width;
int height = bitmap.Height;
float pa = (float)(1 / (Math.Sqrt(2 * Math.PI) * sigma));
float pb = -1.0f / (2 * sigma * sigma);
// generate the Gauss Matrix
float[] gaussMatrix = new float[radius * 2 + 1];
float gaussSum = 0f;
for (int i = 0, x = -radius; x <= radius; ++x, ++i)
{
float g = (float)(pa * Math.Exp(pb * x * x));
gaussMatrix[i] = g;
gaussSum += g;
}
for (int i = 0, length = gaussMatrix.Length; i < length; ++i)
{
gaussMatrix[i] /= gaussSum;
}
try
{
Bitmap bmpReturn = new Bitmap(width, height, PixelFormat.Format24bppRgb);
BitmapData srcBits = bitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
BitmapData targetBits = bmpReturn.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);
unsafe
{
byte* pSrcBits = (byte*)srcBits.Scan0.ToPointer();
byte* pTargetBits = (byte*)targetBits.Scan0.ToPointer();
int stride = srcBits.Stride;
byte* pTemp;
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
if (x == 0 || x == width - 1 || y == 0 || y == height - 1)
{
//最边上的像素不处理
pTargetBits[0] = pSrcBits[0];
pTargetBits[1] = pSrcBits[1];
pTargetBits[2] = pSrcBits[2];
}
else
{
//取周围9点的值
int r1, r2, r3, r4, r5, r6, r7, r8, r9;
int g1, g2, g3, g4, g5, g6, g7, g8, g9;
int b1, b2, b3, b4, b5, b6, b7, b8, b9;
float fR, fG, fB;
//左上
pTemp = pSrcBits - stride - 3;
r1 = pTemp[2];
g1 = pTemp[1];
b1 = pTemp[0];
//正上
pTemp = pSrcBits - stride;
r2 = pTemp[2];
g2 = pTemp[1];
b2 = pTemp[0];
//右上
pTemp = pSrcBits - stride + 3;
r3 = pTemp[2];
g3 = pTemp[1];
b3 = pTemp[0];
//左侧
pTemp = pSrcBits - 3;
r4 = pTemp[2];
g4 = pTemp[1];
b4 = pTemp[0];
//右侧
pTemp = pSrcBits + 3;
r5 = pTemp[2];
g5 = pTemp[1];
b5 = pTemp[0];
//右下
pTemp = pSrcBits + stride - 3;
r6 = pTemp[2];
g6 = pTemp[1];
b6 = pTemp[0];
//正下
pTemp = pSrcBits + stride;
r7 = pTemp[2];
g7 = pTemp[1];
b7 = pTemp[0];
//右下
pTemp = pSrcBits + stride + 3;
r8 = pTemp[2];
g8 = pTemp[1];
b8 = pTemp[0];
//自己
pTemp = pSrcBits;
r9 = pTemp[2];
g9 = pTemp[1];
b9 = pTemp[0];
fR = (float)(r1 + r2 + r3 + r4 + r5 + r6 + r7 + r8 + r9);
fG = (float)(g1 + g2 + g3 + g4 + g5 + g6 + g7 + g8 + g9);
fB = (float)(b1 + b2 + b3 + b4 + b5 + b6 + b7 + b8 + b9);
fR /= 9;
fG /= 9;
fB /= 9;
pTargetBits[0] = (byte)fB;
pTargetBits[1] = (byte)fG;
pTargetBits[2] = (byte)fR;
}
pSrcBits += 3;
pTargetBits += 3;
}
pSrcBits += srcBits.Stride - width * 3;
pTargetBits += srcBits.Stride - width * 3;
}
}
bitmap.UnlockBits(srcBits);
bmpReturn.UnlockBits(targetBits);
return bmpReturn;
}
catch
{
return null;
}
}
相关文章
- 07-11【原生JS和canvans实现图片的高斯模糊(qq音乐图片背景效果)】
- 07-11ffmpeg命令合成-高斯模糊+文字+图片轮换
- 07-11opencv3 图片模糊操作-均值滤波 高斯滤波 中值滤波 双边滤波
- 07-11opencv 简单模糊和高斯模糊 cvSmooth
- 07-1113、在 uwp应用中,给图片添加高斯模糊滤镜效果(一)
- 07-11iOS开发-图片高斯模糊效果
- 07-11Jquery如何序列化form表单数据为JSON对象 C# ADO.NET中设置Like模糊查询的参数 从客户端出现小于等于公式符号引发检测到有潜在危险的Request.Form 值 jquery调用iframe里面的方法 Js根据Ip地址自动判断是哪个城市 【我们一起写框架】MVVM的WPF框架(三)—数据控件 设计模式之简单工厂模式(C#语言描述)
- 07-11python利用PIL库使图片高斯模糊
- 07-11c# 高斯模糊
- 07-11c# 图片简单模糊 非高斯模糊