unity图片后期处理
Posted lipper_
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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; } }
以上是关于unity图片后期处理的主要内容,如果未能解决你的问题,请参考以下文章
Unity shader学习之屏幕后期效果之调整屏幕亮度,饱和度,对比度