PixelSpotAreaOld.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using o0;
  2. using o0.Num;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Threading.Tasks;
  7. using UnityEngine;
  8. using ZIM.Unity;
  9. using static ScreenLocate;
  10. using Color = UnityEngine.Color;
  11. namespace ZIM
  12. {
  13. // 亮区的点用来定位(计算center),泛光区域的点用来计算radius
  14. public class PixelSpotAreaOld
  15. {
  16. // 中心
  17. public Vector2 Centroid { get; private set; }
  18. // 亮区的半径,计算含泛光点
  19. float radius = 0;
  20. public float Radius
  21. {
  22. set
  23. {
  24. radius = value;
  25. }
  26. get
  27. {
  28. if (radius != 0)
  29. return radius;
  30. //var radiusDic = new Dictionary<int, float>() { { 0, 0 }, { 45, 0 }, { 90, 0 }, { 135, 0 }, { 180, 0 }, { 225, 0 }, { 270, 0 }, { 315, 0 } };
  31. var radiusDic = new float[8];
  32. foreach (var p in Pixels0)
  33. {
  34. var dir = p - Centroid;
  35. var radius = dir.magnitude;
  36. var degreeI = radius < 0.00001f ? 0 : (int)(dir.DegreeToXAxis() / 45);
  37. if (degreeI > 7) degreeI = 0; // 防止正好360度
  38. if (radius > radiusDic[degreeI])
  39. radiusDic[degreeI] = radius;
  40. }
  41. foreach (var p in Pixels1)
  42. {
  43. var dir = p - Centroid;
  44. var radius = dir.magnitude;
  45. var degreeI = radius < 0.00001f ? 0 : (int)(dir.DegreeToXAxis() / 45);
  46. if (degreeI > 7) degreeI = 0;
  47. if (radius > radiusDic[degreeI])
  48. radiusDic[degreeI] = radius;
  49. }
  50. return radius = radiusDic.Mean();
  51. }
  52. }
  53. public List<Vector2> Pixels0 = new List<Vector2>();
  54. public List<Vector2> Pixels1 = new List<Vector2>();
  55. public PixelSpotAreaOld(Vector2 center) // kmeans中用随机点初始化,作为中心
  56. {
  57. Centroid = center;
  58. }
  59. public void Add0(Vector2 point)
  60. {
  61. Pixels0.Add(point);
  62. }
  63. public void Add1(Vector2 point)
  64. {
  65. Pixels1.Add(point);
  66. }
  67. // 计算中心点,用亮区计算, 如果中心改变则输出true,不变输出false
  68. public bool UpdateCentroid()
  69. {
  70. Vector2 sum = Vector2.zero;
  71. foreach (var p in Pixels0)
  72. sum += p;
  73. var newCentroid = sum / Pixels0.Count;
  74. if (Centroid == newCentroid)
  75. return false;
  76. Centroid = newCentroid;
  77. return true;
  78. }
  79. // 不影响center
  80. public void Clear()
  81. {
  82. Pixels0.Clear();
  83. Pixels1.Clear();
  84. }
  85. public float TotalBrightness(Color[] pixels, Func<int, int, int> Vector2ToIndex, int outer_size = 3)
  86. {
  87. (int x, int y) rectMin = ((int)(Centroid.x - Radius - outer_size), (int)(Centroid.y - Radius - outer_size));
  88. (int x, int y) rectMax = ((int)(Centroid.x + Radius + outer_size), (int)(Centroid.y + Radius + outer_size));
  89. var total = 0f;
  90. Parallel.For(rectMin.x, rectMax.x, (i) =>
  91. {
  92. var t = 0;
  93. for (int j = rectMin.y; j < rectMax.y; j++)
  94. {
  95. var index = Vector2ToIndex(i, j);
  96. var b = pixels[index].Brightness(64);
  97. t += b;
  98. }
  99. lock (InfraredLocate.locker)
  100. {
  101. total += t;
  102. }
  103. });
  104. //total /= (rectMax.y - rectMin.y) * (rectMax.x - rectMin.x);
  105. return total;
  106. }
  107. }
  108. }