处理算法如下,在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 = ; x < Width; x++) {
for (int y = ; 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 = ; x < Width - ; x++) {
for (int y = ; y < Height - ; y++) {
float r = , g = , b = ;
pixel1 = image.GetPixel (x, y);
pixel2 = image.GetPixel (x + , y + );
r = Mathf.Abs (pixel1.r - pixel2.r + / );
g = Mathf.Abs (pixel1.g - pixel2.g + / );
b = Mathf.Abs (pixel1.b - pixel2.b + / ); outPut.SetPixel (x, y, new Color (r, g, b));
}
} }
/// <summary>
/// 黑白效果
/// </summary>
private void Heibai(){
Color pixel;
for (int x = ; x < Width; x++)
for (int y = ; y < Height; y++) {
pixel = image.GetPixel (x, y);
float r, g, b, Result = ;
r = pixel.r;
g = pixel.g;
b = pixel.b;
//实例程序以加权平均值法产生黑白图像
int iType = ;
switch (iType) {
case ://平均值法
Result = ((r + g + b) / );
break;
case ://最大值法
Result = r > g ? r : g;
Result = Result > b ? Result : b;
break;
case ://加权平均值法
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 ={ , , , , , , , , };
for (int x = ; x < Width - ; x++)
for (int y = ; y < Height - ; y++)
{
float r = , g = , b = ;
int Index = ;
for (int col = -; col <= ; col++)
for (int row = -; row <= ; 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 /= ;
g /= ;
b /= ;
outPut.SetPixel(x - , y - , new Color(r, g, b));
}
}
/// <summary>
/// 柔化5x5高斯模型
/// </summary>
private void Rouhua5(){
Color pixel;
int[] Gauss = { , , , , , , , , , , , , , , , , , , , , , , , , };
for (int x = ; x < Width - ; x++)
for (int y = ; y < Height - ; y++)
{
float r = , g = , b = ;
int Index = ;
for (int col = -; col <= ; col++)
for (int row = -; row <= ; 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 /= ;
g /= ;
b /= ;
outPut.SetPixel(x - , y - , new Color(r, g, b));
}
}
/// <summary>
/// 柔化,高斯模糊通用方法
/// </summary>
private void Flur(){
Color pixel;
float[] Gauss = GaussianSmooth (10.0f);
for (int x = ; x < Width - ; x++)
for (int y = ; y < Height - ; y++)
{
float r = , g = , b = ;
int Index = ;
int length = (int)Mathf.Sqrt (Gauss.Length) / ;
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 - , y - , new Color(r, g, b));
}
}
/// <summary>
/// 锐化效果
/// </summary>
private void Ruihua(){
Color pixel;
//拉普拉斯模板
int[] Laplacian ={ -, -, -, -, , -, -, -, - };
for (int x = ; x < Width - ; x++)
for (int y = ; y < Height - ; y++)
{
float r = , g = , b = ;
int Index = ;
for (int col = -; col <= ; col++)
for (int row = -; row <= ; 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 - , y - , 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 > ? sigma : -sigma;
//高斯核矩阵的大小为(6*sigma+1)*(6*sigma+1)
//ksize为奇数
int ksize = (int)Mathf.Ceil(sigma * ) * + ; //计算一维高斯核
float[] kernel = new float[ksize]; float scale = -0.5f / (sigma * sigma);
const float PI = 3.141592653f;
float cons = / Mathf.Sqrt(-scale / PI); float sum = ;
int kcenter = ksize/;
int i = , j = ;
for(i = ; i < ksize; i++)
{
int x = i - kcenter;
kernel[i] = cons * Mathf.Exp(x * x * scale);//一维高斯函数
sum +=kernel[i]; }
//归一化,确保高斯权值在[0,1]之间
for(i = ; i < ksize; i++)
{
kernel[i] = kernel[i] / sum;
}
return kernel;
}
}

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

  1. Unity 图片的灰度处理

    我们平时在做项目时,经常遇到按钮的点击而且还要区分悬浮,点击,禁用的状态,美术要针对一张图片做多个状态图片,资源图片的数量也就增大了,那么打出的包的大小也就跟着上去了,所以我们可以针对原始图片进行Sh ...

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

    如果你拿到的是一张整图,你想分割之后使用NGUI sprite来使用!  下面就能解决的需求. 步骤: 1. 使用Unity自带的spirte进行分割图片 2. 使用代码把分割出来的2DSpirte转 ...

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

    先上代码: 1 using System.Threading; using UnityEngine; using System.IO; using System.Collections; public ...

  4. unity 图片 粉碎效果 破碎效果

    效果: 点击按钮后: 这些碎片具有物理碰撞效果,下面会有隐形的支柱垫着碎片,n秒后支柱消失,碎片落下 当然你也可以控制生成的碎片,让他们从下而上一块一块地落下 插件源码: https://github ...

  5. unity 图片变纯色填充

    unity自带shader 即可

  6. unity图片保留周边,中间延伸

    1.先把图片切割,类似下面这样的 2.然后在使用的时候(选择图片类型的时候选择sliced)

  7. Unity图片变灰的方式

    http://www.tuicool.com/articles/Vruuqme NGUI中的Button差点儿是最经常使用到的控件之中的一个,而且能够组合各种组件(比方UIButtonColor,UI ...

  8. Unity Texture 2D Compress

    测试了一下 unity 图片 对 apk 的影响. 上两种测试环境    1024 * 1024     带 alpha的话 默认压缩就是RBA 16bit就是2M     不带的话就是 etc 的话 ...

  9. 【优秀的图片后期编辑工具】Luminar 3.1 for Mac

     [简介] 今天和大家分享最新的 Luminar for Mac 3.1 版本,支持中文界面,Luminar是一款Mac上优秀的图片后期处理工具,功能类似 Photoshop Lightroom 等软 ...

随机推荐

  1. mysql安装后服务启动不了(总结)

    mysql安装后服务启动不了 1.1 前言 最近真的是倒霉到家,装个mysql都能把所有的问题给问候了一遍······不过这也是一个宝贵的经验,得好好总结下,毕竟也不知道以后会不会再次遇到.如果有网友 ...

  2. 教我徒弟Android开发入门(一)

    前言: 这个系列的教程是为我徒弟准备的,也适合还不懂java但是想学android开发的小白们~ 本系列是在Android Studio的环境下运行,默认大家的开发环境都是配置好了的 没有配置好的同学 ...

  3. php RAS加密类代码

    通过openssl实现的签名.验签.非对称加解密,需要配合x.509证书(如crt和pem)文件使用. <?php /** * RSA算法类 * 签名及密文编码:base64字符串/十六进制字符 ...

  4. oracle中 merge into 的用法

    很多时候我们需要通过筛选条件同时对表进行 更新,插入,删除 等操作.这样如果我们单一的去操作表会显得很麻烦,下面会说到这个merge  into 的用法会极大的优化我们操作表的时间和代码量. 举例,先 ...

  5. Codeforces 438D The Child and Sequence

    题意:给定一个n个数的序列,完成以下3个操作: 1.给定区间求和 2.给定区间对x取模 3.单点修改 对一个数取模,这个数至少折半.于是我们记一个最大值max,如果x>max则不做处理. #in ...

  6. 【Win 10 应用开发】MIDI 音乐合成——音符消息篇

    在上一篇中,老周介绍了一些乐理知识,有了那些常识后,进行 MIDI 编程就简单得多了.尽管微软已经把 API 封装好,用起来也很简单,但是,如果你没有相应的音乐知识基础,你是无法进行 MIDI 编程的 ...

  7. Spark Shuffle模块——Suffle Read过程分析

    在阅读本文之前.请先阅读Spark Sort Based Shuffle内存分析 Spark Shuffle Read调用栈例如以下: 1. org.apache.spark.rdd.Shuffled ...

  8. Asp.net mvc 知多少(二)

    本系列主要翻译自<ASP.NET MVC Interview Questions and Answers >- By Shailendra Chauhan,想看英文原版的可访问http:/ ...

  9. ERR Unsupported CONFIG parameter: notify-keyspace-events; nested exception is redis.clients.jedis.exceptions.JedisDataException

    异常信息 时间:2017-04-05 15:53:57,361 - 级别:[ WARN] - 消息: [other] The web application [ROOT] appears to hav ...

  10. Sphinx学习笔记(一)

    最近负责一个项目,需要用到全文检索,我的环境大体如下:       1.数据保存在MySQL中     2.需要支持中文检索     3.尽可能的简单       选择了Sphinx,至于solr和E ...