PS平均(滤镜-模糊-平均)效果
最后更新于:2022-04-01 06:46:40
本文介绍PS中滤镜-模糊-平均模糊的效果实现:
这个效果很简单,原理如下:
1,统计全图像素的R,G,B值得和sumR,sumG,sumB;
2,计算平均R,G,B(R = sumR/(width*height)...);
3,用平均R,G,B代替全图所有像素即可。
代码实现如下:
~~~
public static Bitmap Mean(Bitmap src)
{
Bitmap dst = new Bitmap(src);
BitmapData srcData = dst.LockBits(new Rectangle(0, 0, dst.Width, dst.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
byte* p = (byte*)srcData.Scan0;
int i, j;
int sumR, sumG, sumB;
int pos;
int width = dst.Width;
int height = dst.Height;
int stride = srcData.Stride;
sumB = 0; sumG = 0; sumR = 0;
for (j = 0; j < height; j++)
{
for (i = 0; i < width; i++)
{
pos = i * 4 + j * stride;
sumB += p[pos];
sumG += p[pos + 1];
sumR += p[pos + 2];
}
}
pos = width * height;
sumB = sumB / pos;
sumG = sumG / pos;
sumR = sumR / pos;
for (j = 0; j < height; j++)
{
for (i = 0; i < width; i++)
{
pos = i * 4 + j * stride;
p[pos] = (byte)sumB;
p[pos + 1] = (byte)sumG;
p[pos + 2] = (byte)sumR;
}
}
dst.UnlockBits(srcData);
return dst;
}
~~~
效果与PS一模一样:
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-01-05_568b332866c91.jpg)
原图
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-01-05_568b33287d2d6.jpg)
本文算法效果图
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-01-05_568b332888185.jpg)
PS效果图
跟大家分享一下!