数字图像处理

来源:互联网 发布:淘宝上最正宗的冷吃兔 编辑:程序博客网 时间:2024/06/04 01:28

此处列举了6中方法:

1)调整图片的饱和度

2)将图片颜色二值化

3)将图片变成黑白色

4)模糊

5)二值化的另一种方式

6)边缘检测


*********************************************************************************************************************

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. }  


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, 15)]  
  10.     public int intensity = 2;  
  11.   
  12.     Texture2D tt;  
  13.   
  14.     void Start()  
  15.     {  
  16.         transform.localScale = new Vector3((float)t.width / (float)t.height, 1);  
  17.         //InvokeRepeating("MyUpdate", 0, 3);  
  18.   
  19.         MyUpdate();  
  20.     }  
  21.   
  22.     void MyUpdate()  
  23.     {  
  24.         if (tt != null)  
  25.         {  
  26.             Destroy(tt);  
  27.         }  
  28.         tt = new Texture2D(t.width, t.height);  
  29.         for (int y = 1; y < t.height - 1; y++)  
  30.         {  
  31.             for (int x = 1; x < t.width - 1; x++)  
  32.             {  
  33.                 Color c = t.GetPixel(x, y);  
  34.                 if (intensity > 0)  
  35.                 {  
  36.                     for (int i = 0; i < intensity - 1; i++)    // 这个循环太大会卡住!  
  37.                     {  
  38.                         for (int k = 0; k < intensity - 1; k++)  
  39.                         {  
  40.                             c += t.GetPixel(x + i, y + k);  
  41.                         }  
  42.                     }  
  43.                     c = c / Mathf.Pow(intensity, 2) * 1.2f;  
  44.                 }  
  45.                 tt.SetPixel(x, y, c);  
  46.             }  
  47.         }  
  48.         tt.Apply();  
  49.         GetComponent<Renderer>().material.mainTexture = tt;  
  50.     }  
  51. }  



5、 二值化 另一种实现方式

就是讲图片变成自己想要的两种颜色( 自己查看和 第二个Demo的区别 )比之前的好太多了!

http://blog.csdn.net/u010019717

脚本设置 (0和 1之间的值)

[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, 1)]  
  10.     public float intensity = 0.8f;       // 这个代表   灰度值的   分割线  
  11.   
  12.     public Color drawColor = new Color32(240, 190, 170, 255);  
  13.     public Color defaultColor = new Color32(0, 0, 0, 0);  
  14.   
  15.   
  16.     Texture2D tt;  
  17.   
  18.     void Start()  
  19.     {  
  20.         transform.localScale = new Vector3((float)t.width / (float)t.height, 1);  
  21.         InvokeRepeating("MyUpdate", 0, 1);  
  22.     }  
  23.   
  24.     void MyUpdate()  
  25.     {  
  26.         if (tt != null)  
  27.         {  
  28.             Destroy(tt);  
  29.         }  
  30.         tt = new Texture2D(t.width, t.height);  
  31.         for (int y = 1; y < t.height - 1; y++)  
  32.         {  
  33.             for (int x = 1; x < t.width - 1; x++)  
  34.             {  
  35.                 // 这个语句将图片变成两种颜色  
  36.                 tt.SetPixel(x, y, t.GetPixel(x, y).grayscale < intensity ? defaultColor : drawColor);  
  37.             }  
  38.         }  
  39.         tt.Apply();  
  40.         GetComponent<Renderer>().material.mainTexture = tt;  
  41.     }  
  42. }  




6、边缘检测

 

脚本的设置  :

[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, 0.5f)]  
  10.     public float intensity = 0.15f;  
  11.   
  12.     public Color drawColor = new Color32(124, 89, 54, 255);  
  13.     public Color defaultColor = new Color32(0, 0, 0, 0);  
  14.   
  15.   
  16.     Texture2D tt;  
  17.   
  18.     void Start()  
  19.     {  
  20.         transform.localScale = new Vector3((float)t.width / (float)t.height, 1);  
  21.         InvokeRepeating("MyUpdate", 0, 5);  
  22.     }  
  23.   
  24.     void MyUpdate()  
  25.     {  
  26.         if (tt != null)  
  27.         {  
  28.             Destroy(tt);  
  29.         }  
  30.         tt = new Texture2D(t.width, t.height);  
  31.         for (int y = 1; y < t.height - 1; y++)  
  32.         {  
  33.             for (int x = 1; x < t.width - 1; x++)  
  34.             {  
  35.   
  36.                 // 5个位置的灰度值  
  37.                 float g = t.GetPixel(x, y).grayscale;  
  38.                 float gL = t.GetPixel(x - 1, y).grayscale;  
  39.                 float gR = t.GetPixel(x + 1, y).grayscale;  
  40.                 float gT = t.GetPixel(x, y - 1).grayscale;  
  41.                 float gB = t.GetPixel(x, y + 1).grayscale;  
  42.   
  43.                 // 如果和上下左右 的弧度差值大于一个指定的值, 就认为这个是边缘!  
  44.                 if (Mathf.Abs(g - gL) > intensity)  
  45.                 {  
  46.                     tt.SetPixel(x, y, drawColor);  
  47.                 }  
  48.                 else if (Mathf.Abs(g - gR) > intensity)  
  49.                 {  
  50.                     tt.SetPixel(x, y, drawColor);  
  51.                 }  
  52.                 else if (Mathf.Abs(g - gT) > intensity)  
  53.                 {  
  54.                     tt.SetPixel(x, y, drawColor);  
  55.                 }  
  56.                 else if (Mathf.Abs(g - gB) > intensity)  
  57.                 {  
  58.                     tt.SetPixel(x, y, drawColor);  
  59.                 }  
  60.                 else  
  61.                 {  
  62.                     tt.SetPixel(x, y, defaultColor);  
  63.                 }  
  64.             }  
  65.         }  
  66.         tt.Apply();  
  67.         GetComponent<Renderer>().material.mainTexture = tt;  
  68.     }  
  69. }  


http://blog.csdn.Net/u010019717






0 0
原创粉丝点击