unity图片后期处理

处理算法如下,在Start方法中分别调用想要的效果就行了。其中,将你需要处理的 图片 拖拽到 image参数上。注意,如果想要图片保持原来的尺寸不被压缩,需要更改图片的导入设置如下图,主要的Texture Type 和 Non Power of 2 这两个参数。

using UnityEngine;
using System.Collections;
using System.IO;

public class ImageDeal : MonoBehaviour
{
    public Texture2D image;
    private Texture2D outPut;
    int Width;
    int Height;
    // Use this for initialization
    void Start ()
    {
        Width = image.width;
        Height = image.height;
        outPut = new Texture2D (Width, Height);
        Flur ();

        writeImage ();
    }

    /// <summary>
    /// 底片效果.
    /// </summary>
    private void Dipian ()
    {
        Color pixel;
        for (int x = 1; x < Width; x++) {
            for (int y = 1; y < Height; y++) {
                float r, g, b;
                pixel = image.GetPixel (x, y);
                r = 1f - pixel.r;
                g = 1f - pixel.g;
                b = 1f - pixel.b;
                outPut.SetPixel (x, y, new Color (r, g, b));
            }
        }
    }

    /// <summary>
    /// 浮雕效果
    /// </summary>
    private void Fudiao ()
    {
        //以浮雕效果显示图像
        Color pixel1, pixel2;
        for (int x = 0; x < Width - 1; x++) {
            for (int y = 0; y < Height - 1; y++) {
                float r = 0, g = 0, b = 0;
                pixel1 = image.GetPixel (x, y);
                pixel2 = image.GetPixel (x + 1, y + 1);
                r = Mathf.Abs (pixel1.r - pixel2.r + 128 / 255);
                g = Mathf.Abs (pixel1.g - pixel2.g + 128 / 255);
                b = Mathf.Abs (pixel1.b - pixel2.b + 128 / 255);

                outPut.SetPixel (x, y, new Color (r, g, b));
            }
        }

    }
    /// <summary>
    /// 黑白效果
    /// </summary>
    private void Heibai(){
        Color pixel;
        for (int x = 0; x < Width; x++)
            for (int y = 0; y < Height; y++) {
                pixel = image.GetPixel (x, y);
                float r, g, b, Result = 0;
                r = pixel.r;
                g = pixel.g;
                b = pixel.b;
                //实例程序以加权平均值法产生黑白图像
                int iType = 2;
                switch (iType) {
                case 0://平均值法
                    Result = ((r + g + b) / 3);
                    break;
                case 1://最大值法
                    Result = r > g ? r : g;
                    Result = Result > b ? Result : b;
                    break;
                case 2://加权平均值法
                    Result = ((0.7f * r) + (0.2f * g) + (0.1f * b));
                    break;
                }
                outPut.SetPixel (x, y, new Color (Result, Result, Result));
            }
    }
    /// <summary>
    /// 柔化3x3高斯模型
    /// </summary>
    private void Rouhua3(){
        Color pixel;
        int[] Gauss ={ 1, 2, 1, 2, 4, 2, 1, 2, 1 };
        for (int x = 1; x < Width - 1; x++)
            for (int y = 1; y < Height - 1; y++)
            {
                float r = 0, g = 0, b = 0;
                int Index = 0;
                for (int col = -1; col <= 1; col++)
                    for (int row = -1; row <= 1; row++)
                    {
                        pixel = image.GetPixel(x + row, y + col);
                        r += pixel.r * Gauss[Index];
                        g += pixel.g * Gauss[Index];
                        b += pixel.b * Gauss[Index];
                        Index++;
                    }
                r /= 16;
                g /= 16;
                b /= 16;
                outPut.SetPixel(x - 1, y - 1, new Color(r, g, b));
            }
    }
    /// <summary>
    /// 柔化5x5高斯模型
    /// </summary>
    private void Rouhua5(){
        Color pixel;
        int[] Gauss = { 1, 4, 7, 4, 1, 4, 16, 26, 16, 4, 7, 26, 41, 26, 7, 4, 16, 26, 16, 4, 1, 4, 7, 4, 1 };
        for (int x = 1; x < Width - 1; x++)
            for (int y = 1; y < Height - 1; y++)
            {
                float r = 0, g = 0, b = 0;
                int Index = 0;
                for (int col = -2; col <= 2; col++)
                    for (int row = -2; row <= 2; row++)
                    {
                        pixel = image.GetPixel(x + row, y + col);
                        r += pixel.r * Gauss[Index];
                        g += pixel.g * Gauss[Index];
                        b += pixel.b * Gauss[Index];
                        Index++;
                    }
                r /= 273;
                g /= 273;
                b /= 273;
                outPut.SetPixel(x - 1, y - 1, new Color(r, g, b));
            }
    }
    /// <summary>
    /// 柔化,高斯模糊通用方法
    /// </summary>
    private void Flur(){
        Color pixel;
        float[] Gauss = GaussianSmooth (10.0f);
        for (int x = 1; x < Width - 1; x++)
            for (int y = 1; y < Height - 1; y++)
            {
                float r = 0, g = 0, b = 0;
                int Index = 0;
                int length = (int)Mathf.Sqrt (Gauss.Length) / 2;
                for (int col = -length; col <= length; col++)
                    for (int row = -length; row <= length; row++)
                    {
                        pixel = image.GetPixel(x + row, y + col);
                        r += pixel.r * Gauss[Index];
                        g += pixel.g * Gauss[Index];
                        b += pixel.b * Gauss[Index];
                        Index++;
                    }
                outPut.SetPixel(x - 1, y - 1, new Color(r, g, b));
            }
    }
    /// <summary>
    /// 锐化效果
    /// </summary>
    private void Ruihua(){
        Color pixel;
        //拉普拉斯模板
        int[] Laplacian ={ -1, -1, -1, -1, 9, -1, -1, -1, -1 };
        for (int x = 1; x < Width - 1; x++)
            for (int y = 1; y < Height - 1; y++)
            {
                float r = 0, g = 0, b = 0;
                int Index = 0;
                for (int col = -1; col <= 1; col++)
                    for (int row = -1; row <= 1; row++)
                    {
                        pixel = image.GetPixel(x + row, y + col);
                        r += pixel.r * Laplacian[Index];
                        g += pixel.g * Laplacian[Index];
                        b += pixel.b * Laplacian[Index];
                        Index++;
                    }
                outPut.SetPixel(x - 1, y - 1, new Color(r, g, b));
            }
    }
    /// <summary>
    /// 写文件
    /// </summary>
    private void writeImage ()
    {
        byte[] bytes = outPut.EncodeToJPG ();
        File.WriteAllBytes (Application.dataPath + "/test.jpg", bytes);
    }

    /// <summary>
    /// 动态生成高斯模型
    /// </summary>
    /// <returns>The smooth.</returns>
    /// <param name="sigma">Sigma.</param>
    private float[] GaussianSmooth( float sigma)
    {
        sigma = sigma > 0 ? sigma : -sigma;
        //高斯核矩阵的大小为(6*sigma+1)*(6*sigma+1)
        //ksize为奇数
        int ksize = (int)Mathf.Ceil(sigma * 3) * 2 + 1;   

        //计算一维高斯核
        float[] kernel = new float[ksize];  

        float scale = -0.5f / (sigma * sigma);
        const float PI = 3.141592653f;
        float cons = 1/ Mathf.Sqrt(-scale / PI);  

        float sum = 0;
        int kcenter = ksize/2;
        int i = 0, j = 0;
        for(i = 0; i < ksize; i++)
        {
            int x = i - kcenter;
            kernel[i] = cons * Mathf.Exp(x * x * scale);//一维高斯函数
            sum +=kernel[i];  

        }
        //归一化,确保高斯权值在[0,1]之间
        for(i = 0; i < ksize; i++)
        {
            kernel[i] =  kernel[i] / sum;
        }
        return kernel;
    }
}
时间: 2024-07-30 12:47:02

unity图片后期处理的相关文章

Unity 图片分割将spirte保存在本地

如果你拿到的是一张整图,你想分割之后使用NGUI sprite来使用!  下面就能解决的需求. 步骤: 1. 使用Unity自带的spirte进行分割图片 2. 使用代码把分割出来的2DSpirte转换成本地PNG图片,再导入Unity使用atlas纹理O(∩_∩)O~ 注意事项: 1.  图片切换成Advanced类型 Read/Write Enabled勾上,不然会抛出异常 直接上代码: [MenuItem("Tools/导出精灵")] static void SaveSprite

Unity图片处理类,包括压缩、截屏和滤镜

先上代码: 1 using System.Threading; 2 using UnityEngine; 3 using System.IO; 4 using System.Collections; 5 6 public class TextureUtility 7 { 8 public class ThreadData 9 { 10 public int start; 11 public int end; 12 public ThreadData (int s, int e) { 13 sta

关于Unity 图片的Texture Type

当我们导入一张图片后,点击图片,有一个Texture Type的贴图类型, Texture:普通贴图 Normal map:法线贴图 Editor GUI and Legacy GUI:UI贴图 Sprite(2D and UI):精灵 Cursor:鼠标指针 Reflection:反射贴图 Cookie:遮罩贴图 Lightmap:烘焙贴图 Advanced:高级(可自定义一些贴图属性)

图片后期处理:将复杂的图片背景美化去除技巧

下面给大家的教程是介绍背景较为复杂的人物图片处理方法.方法是蒙版加模糊滤镜来模糊背景部分.大致过程:先把背景部分抠出来,然后用模糊滤镜模糊处理,后期再用蒙版等控制需要模糊的区域即可.原图 ? 想学更详细的 蒙版和滤镜教程,欢迎来到敬伟Photoshop经典教程! 最终效果 1.打开原图素材,用磁性套索工具勾勒出人物. 2.对未勾选到的位置用多边形套索工具0像素再选.之后对选区反选按组合键Ctrl+Shift+I. 3.添加快速蒙版,按快捷键Q键,未选取部位为红色,表示被保护区域. 4.点击渐变工

Unity图片变灰的方式

http://www.tuicool.com/articles/Vruuqme NGUI中的Button差点儿是最经常使用到的控件之中的一个,而且能够组合各种组件(比方UIButtonColor,UIButtonOffset,UITweenxx),方便设置Button的各种状态下的属性.差点儿能够满足我们的全部需求. 可是对于当Button的isEnabled属性设置为false时,依据设置的disableColor属性设置不可点击时的颜色时,尽管我们设置的灰色,但并非我们想象中的样子! 设置的

Unity Texture 2D Compress

测试了一下 unity 图片 对 apk 的影响. 上两种测试环境 1024 * 1024 带 alpha的话 默认压缩就是RBA 16bit就是2M 不带的话就是 etc 的话 就是0.5m 但是有一个奇怪的结果,打出来的 apk 包却是差不多的.只有解压之后才发现包里面的资源的确符合猜想. 看来带A的图片 apk压缩率 高一点?纯属个人猜测.

2017手游买量时代的到来

--记2017年GMGC大会 一年一度的GMGC (全球游戏大赛)在17年的3月15-17日举行,从本届(2017年度)大会透射出来的很多行业信息,或许可以主导未来几年行业整体的发展格局.首先是手游市场从2012年快速启动,经历5年的爆发式增长后,最近两年进入了白热化的"存量时代".在大量同质化手游产品的情况下,"买量"成为业内中大型游戏企业存活与发展的不二法门.[注: "买量"就是通过营销手段,获取大量玩家的下载量] (大会签到处) 一: 手游

Lightroom Classic CC 8.3.1中文直装版

lightroom classic cc 2019中文直装版是Adobe公司开发的一款非常好用的图片后期处理软件,也是史上首个专为专业摄影师和摄影爱好者提供了全套照片服务的应用程序.可以为用户提供跨桌面.移动设备和 Web 编辑.整理.存储和分享照片所需的一切. lightroom cc 2019 mac 版安装教程 注意!!安装lightroom classic cc 2019 直装版需要联网安装,而且需要登陆Adobe账号!否则激活会无效!! 1.Lightroom Classic CC 2

UNITY VR 视频/图片 开发心得(一)

现在的VR似乎没有之前那么火热了,于是乎我居然开始了VR征程... 说起VR,对于没有接受过相关知识的人来说可能看起来比较高大上,但是VR的原理却没有想象中那么复杂.总的来说,VR之所以能够产生立体感,是因为人有两只眼睛.其实现在有很多自称VR的视频或者图片严格来讲并不能算是VR,因为它只是将一平面图变成了360°的图,其实和平面图是一样的,并没有深度信息,只是你可以转转小脑袋来全方位观察而已.如下图: 可以看到图片是扭曲的,至于为什么人眼在VR眼镜中看到的图像是正常的,是因为这张图片并不是直接