图像滤镜艺术---挤压(Pinch)滤镜

原文: 图像滤镜艺术---挤压(Pinch)滤镜

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;

        }
效果图如下:

原图

Pinch滤镜效果图

最后放上一个完整的C#版程序Demo给大家下载使用:http://www.zealpixel.com/forum.php?mod=viewthread&tid=55&extra=page%3D1

优秀的个人博客,低调大师

微信关注我们

原文链接:https://yq.aliyun.com/articles/544402

转载内容版权归作者及来源网站所有!

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

相关文章

发表评论

资源下载

更多资源
优质分享Android(本站安卓app)

优质分享Android(本站安卓app)

近一个月的开发和优化,本站点的第一个app全新上线。该app采用极致压缩,本体才4.36MB。系统里面做了大量数据访问、缓存优化。方便用户在手机上查看文章。后续会推出HarmonyOS的适配版本。

Mario,低调大师唯一一个Java游戏作品

Mario,低调大师唯一一个Java游戏作品

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

Eclipse(集成开发环境)

Eclipse(集成开发环境)

Eclipse 是一个开放源代码的、基于Java的可扩展开发平台。就其本身而言,它只是一个框架和一组服务,用于通过插件组件构建开发环境。幸运的是,Eclipse 附带了一个标准的插件集,包括Java开发工具(Java Development Kit,JDK)。

Java Development Kit(Java开发工具)

Java Development Kit(Java开发工具)

JDK是 Java 语言的软件开发工具包,主要用于移动设备、嵌入式设备上的java应用程序。JDK是整个java开发的核心,它包含了JAVA的运行环境(JVM+Java系统类库)和JAVA工具。