图像特效---哈哈镜效果滤镜

哈哈镜效果滤镜 
    所谓哈哈镜,是一种表面凸凹不平的镜子,可以反应出人像及物件的扭曲面貌
    哈哈镜效果滤镜实际上是通过图像形变来模拟真实的哈哈镜效果。所谓形变是一系列的坐标变换,变换方法不同,呈现的效果也不同。本文将介绍如何通过三角变换公式来实现这个滤镜特效,具体算法过程如下:

  1,对于哈哈镜效果变换,首先它有两个参数,原点坐标和特效影响因子。

  对于图像中的像素点P(x,y),假设原点坐标为XY,那么,根据三角函数变换可以得到:

  当前像素P的相对坐标cX,cY:

    

  3,由2中得到的新坐标就是像素P(x,y)的映射坐标,图像变换后的像素值就是映射坐标处的像素值。

  4,对于半径影响因子k,这里程序中设置为定值100,具体范围自己控制。

 以上就是哈哈镜的算法过程,同时我们放上核心代码如下:

        //

        ///

        /// Sunset Filter

        ///

        /// Source image.

        /// The X position of sun.

        /// The Y position of sun.

        /// The radius of sun light.

        /// The result image.

        private Bitmap ConvexFilterProcess(Bitmap srcBitmap, int cenX, int cenY, int radius)

        {

            Bitmap a = new Bitmap(srcBitmap);

            int w = a.Width;

            int h = a.Height;

            double distance = 0.0;

            double dis = 0.0;

            if (radius > cenX || radius > cenY)

            {

                radius = Math.Min(cenX, 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.Format24bppRgb);

            System.Drawing.Imaging.BitmapData dstData = dst.LockBits(new Rectangle(0, 0, w, h), System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format24bppRgb);

            unsafe

            {

                byte* pIn = (byte*)srcData.Scan0.ToPointer();

                byte* pOut = (byte*)dstData.Scan0.ToPointer();

                byte* p = (byte*)srcData.Scan0.ToPointer();

                int sWidth = srcData.Stride;

                int stride = sWidth - w * 3;

                int R, G, B;

                int newX = 0, newY = 0;

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

                {

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

                    {

                        B = pIn[0];

                        G = pIn[1];

                        R = pIn[2];

                        distance = (x - cenX) * (x - cenX) + (y - cenY) * (y - cenY);

                        dis = Math.Sqrt(distance);

                        if (distance <= radius * radius && distance > 0)

                        {

                            newX = (int)(Math.Floor(dis * (double)(x - cenX) / (double)radius + (double)cenX)+0.5);

                            newY = (int)(Math.Floor(dis * (double)(y - cenY) / (double)radius + (double)cenY)+0.5);

                            pOut[0] = (byte)(*(p + newX * 3 + newY * srcData.Stride));

                            pOut[1] = (byte)(*(p + newX * 3 + 1 + newY * srcData.Stride));

                            pOut[2] = (byte)(*(p + newX * 3 + 2 + newY * srcData.Stride));

                        }

                        else

                        {

                            pOut[0] = (byte)B;

                            pOut[1] = (byte)G;

                            pOut[2] = (byte)R;

                        }

                        pIn += 3;

                        pOut += 3;

                    }

                    pIn += stride;

                    pOut += stride;

                }

                a.UnlockBits(srcData);

                dst.UnlockBits(dstData);

            }

            return dst;


        }

  哈哈镜效果如下:

原图

哈哈镜效果图


程序demo: http://www.zealfilter.com/forum.php?mod=viewthread&tid=53&extra=page%3D2

可以使用OpenCV和NumPy库来实现哈哈镜滤镜效果。以下是一个简单的示例代码: ```python import cv2 import numpy as np def hahaha_filter(img): # 获取图像的宽度和高度 height, width = img.shape[:2] # 创建一个空白图像,用于存储哈哈镜滤镜效果 hahaha_img = np.zeros((height, width, 3), dtype=np.uint8) # 循环遍历每个像素 for y in range(height): for x in range(width): # 计算当前像素到图像中心的距离 dx = x - width/2 dy = y - height/2 distance = np.sqrt(dx**2 + dy**2) # 根据距离计算出放大倍率 scale = 1.5 * distance / (width/2) # 获取经过放大后的像素坐标 new_x = int(x + dx*scale) new_y = int(y + dy*scale) # 确保新坐标在图像范围内 if new_x >= 0 and new_x < width and new_y >= 0 and new_y < height: # 将当前像素的颜色赋值给新位置像素 hahaha_img[new_y, new_x] = img[y, x] return hahaha_img # 读取图像并显示原始图像哈哈镜滤镜效果 img = cv2.imread('example.jpg') cv2.imshow('Original Image', img) hahaha_img = hahaha_filter(img) cv2.imshow('Hahaha Filter Image', hahaha_img) cv2.waitKey(0) cv2.destroyAllWindows() ``` 在这个示例代码中,我们首先定义了一个名为`hahaha_filter`的函数,该函数接受一个图像作为输入,并返回一个经过哈哈镜滤镜处理后的图像。在函数中,我们首先创建了一个空白图像,然后循环遍历原始图像中的每个像素,并根据像素到图像中心的距离计算出放大倍率,并将当前像素的颜色赋值给新位置像素。最后返回处理后的图像。 在主程序中,我们首先读取原始图像,并显示原始图像和经过哈哈镜滤镜处理后的图像。你可以将`example.jpg`替换为自己的图像文件名。运行程序后,你将看到原始图像哈哈镜滤镜效果图像
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值