本文介紹一種旋轉模糊濾鏡的實現算法。
旋轉模糊主要特點是:整張圖像圍繞一個中心點做旋轉變換,同時有個控制旋轉程度的變量和一個控制模糊程度的變量,來完成這個效果。圖像中距離中心點越近,旋轉和模糊的程度都越小,反之,越大。
假設中心點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;
}
~~~
最后,看下效果圖:
[](http://www.zealpixel.com/data/attachment/portal/201509/15/143909xts7rcps466mst27.jpg)
原圖
[](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圖層混合模式之明度模式