Extension.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. using o0.Geometry2D.Float;
  2. using o0InfraredLocate.ZIM;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Threading.Tasks;
  7. using Unity.VisualScripting;
  8. using UnityEngine;
  9. using UnityEngine.Internal;
  10. namespace ZIM.Unity
  11. {
  12. public static partial class Extension
  13. {
  14. public static UnityEngine.Color UnityColor(this o0.Color c)
  15. {
  16. return new UnityEngine.Color(c.r, c.g, c.b, c.a);
  17. }
  18. public static o0.Color o0Color(this UnityEngine.Color c)
  19. {
  20. return new o0.Color(c.r, c.g, c.b, c.a);
  21. }
  22. public static Texture2D zimAutoLightSimple(this WebCamTexture texture)
  23. {
  24. var pixel = texture.GetPixels();
  25. return pixel.zimAutoLightSimple(texture.width, texture.height);
  26. }
  27. public static Texture2D zimAutoLightSimple(this Texture2D texture)
  28. {
  29. var pixel = texture.GetPixels();
  30. return pixel.zimAutoLightSimple(texture.width, texture.height);
  31. }
  32. public static Vector2 Size(this Texture2D texture)
  33. {
  34. return new Vector2(texture.width, texture.height);
  35. }
  36. // 亮点放大到1
  37. public static Texture2D zimAutoLightSpot(this WebCamTexture texture)
  38. {
  39. var pixel = texture.GetPixels();
  40. float[] brightness = new float[pixel.Length];
  41. Parallel.For(0, pixel.Length, (i) => brightness[i] = pixel[i].Brightness());
  42. var max = Enumerable.Max(brightness);
  43. var scale = max >= 0.5f ? 1 / max : 1.3f; // 设个阈值 0.5
  44. Parallel.For(0, pixel.Length, (i) => pixel[i] = pixel[i].RGBMutiply(scale));
  45. var tex = new Texture2D(texture.width, texture.height);
  46. tex.SetPixels(pixel);
  47. tex.Apply();
  48. return tex;
  49. }
  50. public static Texture2D zimAutoLight(this WebCamTexture texture)
  51. {
  52. var pixel = texture.GetPixels();
  53. return zimAutoLight(pixel, texture.width, texture.height, 0);
  54. }
  55. public static Texture2D zimAutoLight(this Texture2D texture, int brightness)
  56. {
  57. var pixel = texture.GetPixels();
  58. return zimAutoLight(pixel, texture.width, texture.height, brightness);
  59. }
  60. public static Texture2D zimAutoLight(this Color[] pixel, int width, int height, [DefaultValue("0")] int brightness)
  61. {
  62. float[] p = new float[pixel.Length];
  63. Parallel.For(0, pixel.Length, (i) => p[i] = pixel[i].Brightness());
  64. var scale = 1 / Enumerable.Max(p) * (2 + brightness);
  65. if (float.IsInfinity(scale))
  66. return null;
  67. Parallel.For(0, p.Length, (i) => pixel[i] = pixel[i].RGBMutiply(scale)); // 归一化
  68. var tex = new Texture2D(width, height);
  69. tex.SetPixels(pixel);
  70. tex.Apply();
  71. return tex;
  72. }
  73. public static Texture2D zimAutoLevelEqualization(this WebCamTexture texture, int bit = 256)
  74. {
  75. var pixel = texture.GetPixels();
  76. float[] p = new float[pixel.Length];
  77. Parallel.For(0, pixel.Length, (i) => p[i] = pixel[i].r); // 红通道
  78. bit -= 1;
  79. var scale = 1 / Enumerable.Max(p);
  80. if (float.IsInfinity(scale))
  81. return null;
  82. Parallel.For(0, p.Length, (i) => p[i] *= scale); // 归一化
  83. int[] pint = new int[p.Length];
  84. Parallel.For(0, p.Length, i => pint[i] = (int)(p[i] * bit)); // 映射到256色彩
  85. //Debug.Log(Enumerable.Max(pint) + ", " + scale);
  86. int[] H = new int[bit + 1];
  87. for (int i = 0; i < p.Length; i++)
  88. H[pint[i]]++;
  89. float[] HF = new float[bit + 1];
  90. for (int i = 0; i <= bit; i++)
  91. HF[i] = H[i] / (float)p.Length; // 概率
  92. float[] D = new float[bit + 1];
  93. float temp = 0;
  94. for (int i = 1; i <= bit; i++) // 不取颜色为0的概率
  95. {
  96. temp += HF[i];
  97. D[i] = temp; // 累计密度
  98. }
  99. Parallel.For(0, p.Length, i => p[i] = D[pint[i]] / D.Last()); // 取密度为颜色,并归一化
  100. UnityEngine.Color[] pixelLighted = new UnityEngine.Color[pixel.Length];
  101. Parallel.For(0, pixel.Length, (i) => pixelLighted[i] = new UnityEngine.Color(p[i], 0, 0));
  102. var tex = new Texture2D(texture.width, texture.height);
  103. tex.SetPixels(pixelLighted);
  104. tex.Apply();
  105. return tex;
  106. }
  107. public static void CopyTo(this RenderTexture src, Texture2D dst)
  108. {
  109. var pre = RenderTexture.active;
  110. RenderTexture.active = src;
  111. dst.ReadPixels(new Rect(0, 0, src.width, src.height), 0, 0);
  112. dst.Apply();
  113. RenderTexture.active = pre;
  114. }
  115. }
  116. }