旋转模糊滤镜

最后更新于:2022-04-01 06:46:34

本文介绍一种旋转模糊滤镜的实现算法。 旋转模糊主要特点是:整张图像围绕一个中心点做旋转变换,同时有个控制旋转程度的变量和一个控制模糊程度的变量,来完成这个效果。图像中距离中心点越近,旋转和模糊的程度都越小,反之,越大。 假设中心点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://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-01-05_568b3326edd01.jpg)](http://www.zealpixel.com/data/attachment/portal/201509/15/143909xts7rcps466mst27.jpg) 原图 [![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/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) 跟大家分享一下!
';