写在Shader前, unity数字图像处理 上

来源:互联网 发布:淘宝上可以卖膏药吗 编辑:程序博客网 时间:2024/06/08 14:12

原文地址:  http://blog.csdn.net/u010019717/article/details/52123429               孙广东    2016.8.4

参考:  http://lolikitty.pix.net/ 

http://blog.csdn.net/u010019717

 

注意:  不是Shader  代码

1、 调整图片的饱和度

不太需要原理, 维基百科给出的公式:

https://zh.wikipedia.org/zh-cn/YUV

其中提到了 主要的抽样(subsample)格式有YCbCr 4:2:0、YCbCr 4:2:2、YCbCr 4:1:1和YCbCr 4:4:4。

 http://blog.csdn.net/u010019717

上面公式使用了  4:2:2,  下面代码使用 4:4:4 完全取样。

 

场景中都有什么,  然后挂上下面的脚本   设置参数值大小:

[csharp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. using UnityEngine;  
  2. using System.Collections;  
  3.   
  4. public class Test : MonoBehaviour  
  5. {  
  6.   
  7.     public Texture2D t;  
  8.   
  9.     [Range(0, 5)]  
  10.     public float saturation = 1; // 饱和度调整,编辑器设置  
  11.   
  12.     Texture2D tt;  
  13.   
  14.     void Start()  
  15.     {  
  16.         if (tt != null)  
  17.         {  
  18.             Destroy(tt);  
  19.         }  
  20.   
  21.         transform.localScale = new Vector3((float)t.width / (float)t.height, 1);  
  22.         tt = new Texture2D(t.width, t.height);  
  23.         for (int y = 0; y < t.height; y++)  
  24.         {  
  25.             for (int x = 0; x < t.width; x++)  
  26.             {  
  27.                 Color c = t.GetPixel(x, y);  
  28.   
  29.                 float r = c.r;  
  30.                 float g = c.g;  
  31.                 float b = c.b;  
  32.   
  33.                 float Y = (r * 0.299f) + (g * 0.587f) + (b * 0.114f);  
  34.                 float U = -(r * 0.147f) - (g * 0.289f) + (b * 0.436f);  
  35.                 float V = (r * 0.615f) - (g * 0.515f) - (b * 0.1f);  
  36.   
  37.                 U *= saturation;  
  38.                 V *= saturation;  
  39.   
  40.                 float R = Y + (V * 1.14f);  
  41.                 float G = Y - (U * 0.39f) - (V * 0.58f);  
  42.                 float B = Y + (U * 2.03f);  
  43.   
  44.                 tt.SetPixel(x, y, new Color(R, G, B));  
  45.             }  
  46.         }  
  47.         tt.Apply();  
  48.         GetComponent<Renderer>().material.mainTexture = tt;  
  49.     }  
  50. }  


 

http://blog.csdn.net/u010019717

 

2、将图片颜色二值化

 

整个 图片只会显示成 两种 颜色。

这个很不好测试,  你随便找一张图, 但是要找到 minValveColor  和 maxValveColor 时很费劲的!

[csharp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. using UnityEngine;  
  2. using System.Collections;  
  3.   
  4. public class Test : MonoBehaviour  
  5. {  
  6.   
  7.     public Texture2D t;  
  8.     //在范围内的我们要显示成的颜色:  
  9.     public Color drawColor = new Color32(240, 190, 170, 255);  
  10.   
  11.     //没有在范围内的我们要显示成的颜色:  
  12.     public Color defaultColor = new Color32(0, 0, 0, 0);  
  13.   
  14.     Color minValveColor = new Color32(220 , 200 ,  198, 255);  
  15.     Color maxValveColor = new Color32(221 , 206 , 211, 255);  
  16.   
  17.     float minR = 0;  
  18.     float minG = 0;  
  19.     float minB = 0;  
  20.   
  21.     float maxR = 0;  
  22.     float maxG = 0;  
  23.     float maxB = 0;  
  24.   
  25.     Texture2D tt;  
  26.   
  27.     void Start()  
  28.     {  
  29.         minR = minValveColor.r;  
  30.         minG = minValveColor.g;  
  31.         minB = minValveColor.b;  
  32.   
  33.         maxR = maxValveColor.r;  
  34.         maxG = maxValveColor.g;  
  35.         maxB = maxValveColor.b;  
  36.   
  37.         transform.localScale = new Vector3((float)t.width / (float)t.height, 1);  
  38.         InvokeRepeating("MyUpdate", 0, 5);  
  39.     }  
  40.   
  41.     string z = "";  
  42.   
  43.     void MyUpdate()  
  44.     {  
  45.         if (tt != null)  
  46.         {  
  47.             Destroy(tt);  
  48.         }  
  49.         tt = new Texture2D(t.width, t.height);  
  50.         for (int y = 0; y < t.height; y++)  
  51.         {  
  52.             for (int x = 0; x < t.width; x++)  
  53.             {  
  54.                 Color c = t.GetPixel(x, y);  
  55.                 float r = c.r;  
  56.                 float g = c.g;  
  57.                 float b = c.b;  
  58.   
  59.                 // 根据颜色范围显示  
  60.                 if (r >= minR && r <= maxR && g >= minG && g <= maxG && b >= minB && b <= maxB)  
  61.                 {  
  62.                     tt.SetPixel(x, y, drawColor);  
  63.                 }  
  64.                 else  
  65.                 {  
  66.                     tt.SetPixel(x, y, defaultColor);  
  67.                 }  
  68.             }  
  69.         }  
  70.         tt.Apply();  
  71.         GetComponent<Renderer>().material.mainTexture = tt;  
  72.     }  
  73. }  


3、  将图片 变成黑白色(黑白       或者     YUV  RGB 的转换)

第一种实现方式简单:

[csharp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. using UnityEngine;  
  2. using System.Collections;  
  3.   
  4. public class Test : MonoBehaviour  
  5. {  
  6.   
  7.     public Texture2D t;  
  8.   
  9.     void Start()  
  10.     {  
  11.         transform.localScale = new Vector3((float)t.width / (float)t.height, 1);  
  12.         Texture2D tt = new Texture2D(t.width, t.height);  
  13.         for (int y = 1; y < t.height - 1; y++)  
  14.         {  
  15.             for (int x = 1; x < t.width - 1; x++)  
  16.             {  
  17.                 Color c = t.GetPixel(x, y);  
  18.                 float c2 = (c.r + c.g + c.b) / 3; // 原理,三原色取平均  
  19.                                                   // float c2 = c.grayscale; // 在 Unity 最棒的做法  
  20.                 tt.SetPixel(x, y, new Color(c2, c2, c2));  
  21.             }  
  22.         }  
  23.         tt.Apply();  
  24.         renderer.material.mainTexture = tt;  
  25.     }  
  26. }  


 

////   第二种实现方式, 使用 YUV 作为grayScale  & RGB 转 YUV & YUV 转 RGB ---------------------------

//  采用YUV色彩空间的重要性是它的亮度信号Y和色度信号U、V是分离的。如果只有Y信号分量而没有U、V分量,那么这样表示的图像就是黑白灰度图像。

[csharp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. using UnityEngine;  
  2. using System.Collections;  
  3.   
  4. public class Test : MonoBehaviour  
  5. {  
  6.   
  7.     public Texture2D t;  
  8.   
  9.     void Start()  
  10.     {  
  11.         transform.localScale = new Vector3((float)t.width / (float)t.height, 1);  
  12.         Texture2D tt = new Texture2D(t.width, t.height);  
  13.         for (int y = 0; y < t.height; y++)  
  14.         {  
  15.             for (int x = 0; x < t.width; x++)  
  16.             {  
  17.                 Color c = t.GetPixel(x, y);  
  18.   
  19.                 float r = c.r;  
  20.                 float g = c.g;  
  21.                 float b = c.b;  
  22.   
  23.                 // RGB 转 YUV    RGB和UV的计算都是没有用的!  
  24.                 float Y = (r * 0.299f) + (g * 0.587f) + (b * 0.114f);  
  25.                 float U = -(r * 0.147f) - (g * 0.289f) + (b * 0.436f);  
  26.                 float V = (r * 0.615f) - (g * 0.515f) - (b * 0.1f);  
  27.   
  28.                 // YUV 转 RGB  
  29.                 float R = Y + (V * 1.14f);  
  30.                 float G = Y - (U * 0.39f) - (V * 0.58f);  
  31.                 float B = Y + (U * 2.03f);  
  32.   
  33.                 tt.SetPixel(x, y, new Color(Y, Y, Y)); // Y 即 灰階  
  34.                                                        // tt.SetPixel(x, y, new Color(R, G, B));  
  35.             }  
  36.         }  
  37.         tt.Apply();  
  38.         GetComponent<Renderer>().material.mainTexture = tt;  
  39.     }  
  40. }  


http://blog.csdn.net/u010019717


0 0
原创粉丝点击