图像滤镜艺术---Oilpaint油画滤镜

 Oilpaint油画滤镜
    图像油画效果实际上是将图像边缘产生一种朦胧,雾化的效果,同时,将一定的边缘模糊化,这样图像整体上看去像素与像素之间就像雾一样随机呈现。
 算法过程如下:
 假设当前像素为 P(x,y) ,他的随机位置为 Pd(dx,dy) ,那么算法公式如下:

 其中,K(v)为最大值不大于v的随机数正数,v为雾化阈值,v值越大,雾化程度越明显,反之,雾化程度越小,v=0时,图像无变化效果。

 核心代码如下:

        /// <summary>

        /// Mosaic filter.

        /// </summary>

        /// <param name="src">Source  image.</param>

        /// <param name="blockSize">The size of mosaic effect.</param>

        /// <returns>Resullt image.</returns>

        public Bitmap OilpaintFilter(Bitmap src, int intensity)

        {

            Bitmap srcBitmap = new Bitmap(src);

            int w = srcBitmap.Width;

            int h = srcBitmap.Height;

            System.Drawing.Imaging.BitmapData srcData = srcBitmap.LockBits(new Rectangle(0, 0, w, h), System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

            IntPtr ptr = srcData.Scan0;

            int bytes = h * srcData.Stride;

            byte[] srcValues = new byte[bytes];

            System.Runtime.InteropServices.Marshal.Copy(ptr, srcValues, 0, bytes);

            byte[] tempValues = (byte[])srcValues.Clone();

            int stride = srcData.Stride;

            Random ran = new Random();

            int k = 0;

            int dx = 0;

            int dy = 0;

            for (int j = 0; j < h; j++)

            {

                for (int i = 0; i < w; i++)

                {

                    k = ran.Next(intensity);

                    dx = (i + k) >= w ? w - 1 : (i + k);

                    dy = (j + k) >= h ? h - 1 : (j + k);

                    tempValues[i * 4 + j * w * 4] = (byte)srcValues[dx * 4 + dy * w * 4];

                    tempValues[i * 4 + 1 + j * w * 4] = (byte)srcValues[dx * 4 + 1 + dy * w * 4];

                    tempValues[i * 4 + 2 + j * w * 4] = (byte)srcValues[dx * 4 + 2 + dy * w * 4];

                }

            }

            srcValues = (byte[])tempValues.Clone();

            System.Runtime.InteropServices.Marshal.Copy(srcValues, 0, ptr, bytes);

            srcBitmap.UnlockBits(srcData);

            return srcBitmap;

        }

 图像油画滤镜效果如下:

原图

Oilpaint滤镜效果

最后,放上一个完整的C#版程序Demo下载链接:http://www.zealpixel.com/forum.php?mod=viewthread&tid=52&extra=page%3D1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Trent1985

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值