本文介绍一种旋转模糊滤镜的实现算法。
旋转模糊主要特点是:整张图像围绕一个中心点做旋转变换,同时有个控制旋转程度的变量和一个控制模糊程度的变量,来完成这个效果。图像中距离中心点越近,旋转和模糊的程度都越小,反之,越大。
假设中心点O坐标为(cenX,cenY),当前点位置为P(x,y):
1,PO距离Dis=Math.Sqrt((y - cenY) * (y - cenY) + (x - cenX) * (x - cenX));
2,PO和水平方向夹角angle=Math.Atan2((double)(y - cenY), (double)(x - cenX));
3,当前点P对应的旋转后新的坐标newX,newY计算:
newX = Dis * Math.Cos(angle) + cenX;
newY = Dis * Math.Sin(angle) + cenY;
下面给出完整的C#代码:
~~~
/// <summary>
/// Rotate Blur
/// </summary>
/// <param name="src">Source image.</param>
/// <param name="cenX">The X position of Blur.</param>
/// <param name="cenY">The Y position of Blur.</param>
/// <param name="intensity">The intensity of blur,0-100.</param>
/// <returns>The result image.</returns>
private Bitmap RotateBlurProcess(Bitmap srcBitmap, int cenX, int cenY, int intensity)
{
Bitmap a = new Bitmap(srcBitmap);
int w = a.Width;
int h = a.Height;
cenX = Math.Min(w - 1, Math.Max(0, cenX));
cenY = Math.Min(h - 1, Math.Max(0, cenY));
Bitmap dst = new Bitmap(w, h);
System.Drawing.Imaging.BitmapData srcData = a.LockBits(new Rectangle(0, 0, w, h), System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
System.Drawing.Imaging.BitmapData dstData = dst.LockBits(new Rectangle(0, 0, w, h), System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
unsafe
{
byte* pIn = (byte*)srcData.Scan0.ToPointer();
byte* pOut = (byte*)dstData.Scan0.ToPointer();
byte* p = null;
int stride = srcData.Stride - w * 4;
int newX = 0, newY = 0;
double angle = 0;
double temp = 0, r = 0, g = 0, b = 0;
for (int y = 0; y < h; y++)
{
for (int x = 0; x < w; x++)
{
r = 0;
g = 0;
b = 0;
temp = Math.Sqrt((y - cenY) * (y - cenY) + (x - cenX) * (x - cenX));
angle = Math.Atan2((double)(y - cenY), (double)(x - cenX));
for (int n = 0; n < intensity; n++)
{
angle = angle + 0.005;
newX = (int)(temp * Math.Cos(angle) + (double)cenX);
newY = (int)(temp * Math.Sin(angle) + (double)cenY);
newX = Math.Min(w - 1, Math.Max(0, newX));
newY = Math.Min(h - 1, Math.Max(0, newY));
p = pIn + newY * srcData.Stride + newX * 4;
b = b + p[0];
g = g + p[1];
r = r + p[2];
}
b = Math.Min(255, Math.Max(0, b / intensity));
g = Math.Min(255, Math.Max(0, g / intensity));
r = Math.Min(255, Math.Max(0, r / intensity));
pOut[0] = (byte)b;
pOut[1] = (byte)g;
pOut[2] = (byte)r;
pOut[3] = (byte)255;
pOut += 4;
}
pOut += stride;
}
a.UnlockBits(srcData);
dst.UnlockBits(dstData);
}
return dst;
}
~~~
最后,看下效果图:
[![](https://box.kancloud.cn/2016-01-05_568b3326edd01.jpg)](http://www.zealpixel.com/data/attachment/portal/201509/15/143909xts7rcps466mst27.jpg)
原图
[![](https://box.kancloud.cn/2016-01-05_568b332712f15.png)](http://www.zealpixel.com/data/attachment/portal/201509/15/143912qmvpwqpqvrvj1m1p.png)
效果图
给出一个完整DEMO程序的下载地址:[http://www.zealpixel.com/forum.php?mod=viewthread&tid=148&extra=page%3D1](http://www.zealpixel.com/forum.php?mod=viewthread&tid=148&extra=page%3D1) 跟大家分享一下!
- 前言
- 序言
- Brannan滤镜
- 编码基础(Photoshop基础变换的代码实现)
- Toaster滤镜
- Hudson滤镜(Instagram)
- 暴雨滤镜
- 大雪滤镜
- 图像滤镜实现万能方法研究
- 大雾效果滤镜
- 连环画滤镜
- 暗调滤镜
- 怀旧风格滤镜
- (Nostalgla Filter)老照片滤镜
- (Punch Filter)交叉冲印滤镜
- (Lightleaks Filter)漏光滤镜
- 漫画滤镜
- LOMO Filter
- Glow Filter发光滤镜
- (Instagram)1977滤镜
- (Sketch Filter)素描滤镜
- 水彩画滤镜
- 图像光照效果滤镜
- Oilpaint油画滤镜
- Swirl滤镜
- Wave滤镜
- 球面(Spherize)滤镜
- 挤压(Pinch)滤镜
- 旋转模糊滤镜
- 霓虹、浮雕、木刻滤镜
- 图像滤镜晕影调节算法研究
- PS平均(滤镜-模糊-平均)效果
- Photoshop实现Instagram Amaro滤镜特效
- Photoshop实现Instagram之Nashville滤镜
- Photoshop实现Instagram之Sierra滤镜
- Photoshop实现Instagram之Mayfair滤镜效果
- ZPhotoEngine超级算法库
- 乐高像素拼图特效
- 乐高像素拼图特效滤镜的代码实现
- 保留细节的磨皮滤镜之PS实现
- 保留细节的磨皮之C#程序实现
- 流行艺术风滤镜特效PS实现
- PS图层混合模式之明度模式