Extension.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. using o0.Geometry2D.Float;
  2. using System;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using UnityEngine;
  6. namespace ZIM
  7. {
  8. public static partial class Extension
  9. {
  10. public static Vector2 UnityVector(this Vector v)
  11. {
  12. return new Vector2(v.x, v.y);
  13. }
  14. public static Vector o0Vector(this Vector2 v)
  15. {
  16. return new Vector(v.x, v.y);
  17. }
  18. public static float LengthManhattan(this Vector2 v)
  19. {
  20. return Math.Abs(v.x) + Math.Abs(v.y);
  21. }
  22. public static float Brightness(this Color c)
  23. {
  24. return 0.59f * c.r + 0.3f * c.r + 0.11f * c.r; // 红色为主
  25. }
  26. // 转换为整数再计算
  27. public static int Brightness(this Color c, int colorBits)
  28. {
  29. int r = (int)(c.r * colorBits);
  30. int g = (int)(c.g * colorBits);
  31. int b = (int)(c.b * colorBits);
  32. return (int)(0.59f * r + 0.3f * g + 0.11f * b); // 红色为主
  33. }
  34. // 像素坐标映射到Unity局部坐标
  35. public static Vector2 pixelToLocalPosition_AnchorCenter(this Vector2 pixel, Vector2 size, Rect dstRect)
  36. {
  37. var xp = pixel.x / size.x - 0.5f;
  38. var yp = pixel.y / size.y - 0.5f;
  39. //var origin = rawImage.anchoredPosition;
  40. return new Vector2(dstRect.width * xp, dstRect.height * yp);
  41. }
  42. // 像素坐标映射到Unity局部坐标
  43. public static Vector2 pixelToLocalPosition_AnchorCenter(this Vector2 pixel, o0.Geometry2D.Vector<int> size, Rect dstRect)
  44. {
  45. var xp = pixel.x / size.x - 0.5f;
  46. var yp = pixel.y / size.y - 0.5f;
  47. //var origin = rawImage.anchoredPosition;
  48. return new Vector2(dstRect.width * xp, dstRect.height * yp);
  49. }
  50. public static bool IsInScreen(this Quadrilateral Quad, Vector2 size)
  51. {
  52. if (Quad.A.x < Quad.B.x && Quad.A.y < Quad.C.y && Quad.B.y < Quad.D.y && Quad.C.x < Quad.D.x && Quad.A.x > 0 && Quad.A.y > 0 &&
  53. Quad.B.x < size.x && Quad.B.y > 0 && Quad.C.x > 0 && Quad.C.y < size.y && Quad.D.x < size.x && Quad.D.y < size.y)
  54. return true;
  55. return false;
  56. }
  57. public static float DegreeToXAxis(this Vector2 v)
  58. {
  59. var a = v.x > 0 ? Math.Atan(v.y / v.x) * 180 / Math.PI : 180 + Math.Atan(v.y / v.x) * 180 / Math.PI;
  60. a = a < 0 ? 360 + a : a;
  61. return (float)a;
  62. }
  63. public static Texture2D zimAutoLightSimple(this WebCamTexture texture)
  64. {
  65. var pixel = texture.GetPixels();
  66. return zimAutoLightSimple(pixel,texture.width,texture.height);
  67. }
  68. // 效果不太好
  69. public static Texture2D zimAutoLightSimple(this Color[] pixel, int width, int height)
  70. {
  71. Color[] p = new Color[pixel.Length];
  72. var brightness16 = new int[256];
  73. for (int i = 0; i < width / 7; i++)
  74. {
  75. for (int j = 0; j < height / 7; j++)
  76. {
  77. var index = j * 7 * width + i * 7;
  78. var b = pixel[index].Brightness(256);
  79. brightness16[b]++;
  80. }
  81. }
  82. var limit = (int)(width * height / 49 * 0.001f); // 取亮度大约在前0.1%的值(不取最大的因为可能会被个别亮点影响)
  83. int select = 255;
  84. for (; select >= 0; select--)
  85. {
  86. limit -= brightness16[select];
  87. if (limit <= 0)
  88. break;
  89. }
  90. var scale = 1 / (select / 256f);
  91. //var avgBrightness = 0f;
  92. //Parallel.For(0, pixel.Length / 256, (i) => avgBrightness += pixel[i * 256].Brightness());
  93. //avgBrightness /= pixel.Length / 256;
  94. //var scale = 1 / avgBrightness / 2f;
  95. if (float.IsInfinity(scale))
  96. return null;
  97. Parallel.For(0, p.Length, (i) => p[i] = pixel[i] * scale);
  98. var tex = new Texture2D(width, height);
  99. tex.SetPixels(p);
  100. tex.Apply();
  101. return tex;
  102. }
  103. public static Texture2D zimAutoLight(this WebCamTexture texture)
  104. {
  105. var pixel = texture.GetPixels();
  106. return zimAutoLight(pixel, texture.width, texture.height);
  107. }
  108. public static Texture2D zimAutoLight(this Color[] pixel, int width, int height)
  109. {
  110. float[] p = new float[pixel.Length];
  111. Parallel.For(0, pixel.Length, (i) => p[i] = pixel[i].Brightness());
  112. var scale = 1 / Enumerable.Max(p) * 2;
  113. if (float.IsInfinity(scale))
  114. return null;
  115. Parallel.For(0, p.Length, (i) => pixel[i] *= scale); // 归一化
  116. var tex = new Texture2D(width, height);
  117. tex.SetPixels(pixel);
  118. tex.Apply();
  119. return tex;
  120. }
  121. public static Texture2D zimAutoLevelEqualization(this WebCamTexture texture, int bit = 256)
  122. {
  123. var pixel = texture.GetPixels();
  124. float[] p = new float[pixel.Length];
  125. Parallel.For(0, pixel.Length, (i) => p[i] = pixel[i].r); // 红通道
  126. bit -= 1;
  127. var scale = 1 / Enumerable.Max(p);
  128. if (float.IsInfinity(scale))
  129. return null;
  130. Parallel.For(0, p.Length, (i) => p[i] *= scale); // 归一化
  131. int[] pint = new int[p.Length];
  132. Parallel.For(0, p.Length, i => pint[i] = (int)(p[i] * bit)); // 映射到256色彩
  133. //Debug.Log(Enumerable.Max(pint) + ", " + scale);
  134. int[] H = new int[bit + 1];
  135. for (int i = 0; i < p.Length; i++)
  136. H[pint[i]]++;
  137. float[] HF = new float[bit + 1];
  138. for (int i = 0; i <= bit; i++)
  139. HF[i] = H[i] / (float)p.Length; // 概率
  140. float[] D = new float[bit + 1];
  141. float temp = 0;
  142. for (int i = 1; i <= bit; i++) // 不取颜色为0的概率
  143. {
  144. temp += HF[i];
  145. D[i] = temp; // 累计密度
  146. }
  147. Parallel.For(0, p.Length, i => p[i] = D[pint[i]] / D.Last()); // 取密度为颜色,并归一化
  148. UnityEngine.Color[] pixelLighted = new UnityEngine.Color[pixel.Length];
  149. Parallel.For(0, pixel.Length, (i) => pixelLighted[i] = new UnityEngine.Color(p[i], 0, 0));
  150. var tex = new Texture2D(texture.width, texture.height);
  151. tex.SetPixels(pixelLighted);
  152. tex.Apply();
  153. return tex;
  154. }
  155. }
  156. }