Pinch濾鏡
Pinch濾鏡是通過坐標變換來實現以某個點(cenX,cenY)為中心,某個半徑R內圖像向其擠壓變形的效果。實現這個濾鏡的算法很多,主要是數學公式的不同,大家可以自行設計,這里給個代碼示例,大家可以直接使用。
代碼如下:
~~~
/// <summary>
/// Pinch Filter
/// </summary>
/// <param name="src">Source image.</param>
/// <param name="cenX">The X position of sun.</param>
/// <param name="cenY">The Y position of sun.</param>
/// <returns>The result image.</returns>
private Bitmap PinchFilterProcess(Bitmap srcBitmap, int cenX, int cenY)
{
Bitmap a = new Bitmap(srcBitmap);
int w = a.Width;
int h = a.Height;
int radius = 0;
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 sWidth = srcData.Stride;
int stride = sWidth - w * 4;
int offsetX = 0, offsetY = 0;
int newX = 0, newY = 0;
double radian = 0,degree = 10;
for (int y = 0; y < h; y++)
{
for (int x = 0; x < w; x++)
{
offsetX = x - cenX;
offsetY = y - cenY;
radian = Math.Atan2(offsetY, offsetX);
radius = (int)(Math.Sqrt(offsetX * offsetX + offsetY * offsetY));
radius = (int)(Math.Sqrt(radius) * degree);
newX = (int)(radius * Math.Cos(radian)) + cenX;
newY = (int)(radius * Math.Sin(radian)) + 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;
pOut[0] = (byte)p[0];
pOut[1] = (byte)p[1];
pOut[2] = (byte)p[2];
pOut[3] = (byte)255;
pOut += 4;
}
pOut += stride;
}
a.UnlockBits(srcData);
dst.UnlockBits(dstData);
}
return dst;
}
~~~
效果圖如下:
[](http://www.zealpixel.com/data/attachment/portal/201507/21/221354xqq5q0fkq5fqn0fj.jpg)
原圖
[](http://www.zealpixel.com/data/attachment/portal/201507/21/221353y6czmsej4ces55jk.png)
Pinch濾鏡效果圖
最后放上一個完整的C#版程序Demo給大家下載使用:[http://www.zealpixel.com/forum.php?mod=viewthread&tid=55&extra=page%3D1](http://www.zealpixel.com/forum.php?mod=viewthread&tid=55&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圖層混合模式之明度模式