博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
图像滤镜艺术---Swirl滤镜
阅读量:6901 次
发布时间:2019-06-27

本文共 1865 字,大约阅读时间需要 6 分钟。

原文:

Swirl Filter

Swirl 滤镜是实现图像围绕中心点(cenX,cenY)扭曲旋转的效果,效果图如下:

原图

效果图

代码如下:

        //

        ///

        /// Swirl Filter

        ///

        /// Source image.

        /// The X position of Swirl.

        /// The Y position of Swirl.

        /// The degree of swirl,0-360.

        /// The result image.

        private Bitmap SwirlFilterProcess(Bitmap srcBitmap, int cenX, int cenY, int swilDegree)

        {

            Bitmap a = new Bitmap(srcBitmap);

            int w = a.Width;

            int h = a.Height;

            int radius = 0;

            swilDegree = Math.Max(0, Math.Min(360, swilDegree));

            double k = swilDegree / 3600.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 stride = srcData.Stride - w * 4;

                int offsetX = 0, offsetY = 0;

                int newX = 0, newY = 0;

                double radian = 0;

                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));

                        newX = (int)(radius * Math.Cos(radian + k * radius)) + cenX;

                        newY = (int)(radius * Math.Sin(radian + k * radius)) + 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;

        }

最后放上一个完整的C#程序Demo下载地址:http://www.zealpixel.com/thread-58-1-1.html,另外,推荐一个网址www.zealpixel.com,有很多不错的图像处理开源代码!

转载地址:http://abpdl.baihongyu.com/

你可能感兴趣的文章
python numpy模块使用笔记(更新)
查看>>
vue-cli构建项目 npm run build后应该怎么运行在本地查看效果
查看>>
unix平台下I/O聚集和分离的一种方案
查看>>
1081. Binary Lexicographic Sequence(找规律)
查看>>
Postman笔记 - 入门好文
查看>>
通过游戏来学习编程
查看>>
周记(飞船一号
查看>>
openssl初步使用
查看>>
Vue项目碰到"‘webpack-dev-server’不是内部或外部命令,也不是可运行的程序或批处理文件"报错...
查看>>
模拟任务资源管理器的小程序
查看>>
通过一个例子,总结下检测数组属性的N种方法
查看>>
Samba结合AD实现域帐号认证的文件服务器
查看>>
laravel的Eloquent中的get()和Query/Builder中的get()
查看>>
bzoj 2286(洛谷 2495) [Sdoi2011]消耗战——虚树
查看>>
51nod 1301 集合异或和——异或dp
查看>>
weblogic.jdbc.wrapper.Blob_oracle_sql_BLOB cannot be cast to oracle.sql.BLOB 解决方法
查看>>
表格边框设置
查看>>
问题 K: A/B Problem
查看>>
Django实战(7):改造ProductList界面
查看>>
大专生自学Java到找到工作的心得
查看>>