PixelSpotArea.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 Color = UnityEngine.Color;
  9. namespace ZIM
  10. {
  11. // 亮区的点用来定位(计算center),泛光区域的点用来计算radius
  12. public class PixelSpotArea
  13. {
  14. //public static float gridRatio0 = 6f / 1280;
  15. //public static float gridRatio1 = 20f / 720;
  16. public static int gridLength0 = 2; // 亮区
  17. public static int gridLength1 = 10; // 边缘泛光
  18. public static (int x, int y) GetGrid0(Vector2 v)
  19. {
  20. var m = (int)(v.x / gridLength0);
  21. var n = (int)(v.y / gridLength0);
  22. return (m, n);
  23. }
  24. public static (int x, int y) GetGrid1(Vector2 v)
  25. {
  26. var m = (int)(v.x / gridLength1);
  27. var n = (int)(v.y / gridLength1);
  28. return (m, n);
  29. }
  30. // 亮点聚类到区域
  31. public static List<PixelSpotArea> Cluster(List<Vector2> spotPoint, List<Vector2> brightPoint, Func<Vector2, bool> PointFilter = null)
  32. {
  33. var spotArea = new List<PixelSpotArea>();
  34. for (int i = 0; i < spotPoint.Count; i++)
  35. {
  36. var p = spotPoint[i];
  37. if (PointFilter != null && !PointFilter(p)) // 筛选是否在屏幕内
  38. continue;
  39. var grid = PixelSpotArea.GetGrid0(p);
  40. var join = new SortedList<int, PixelSpotArea>();
  41. for (int j = 0; j < spotArea.Count; j++)
  42. {
  43. var area = spotArea[j];
  44. if (area.Include0(grid))
  45. join.Add(j, area);
  46. }
  47. if (join.Count == 0)
  48. spotArea.Add(new PixelSpotArea(p, grid, PixelSpotArea.GetGrid1(p)));
  49. else if (join.Count == 1)
  50. join.First().Value.Join(p, grid, PixelSpotArea.GetGrid1(p));
  51. else
  52. {
  53. var combine = new PixelSpotArea(join.Values);
  54. combine.Join(p, grid, PixelSpotArea.GetGrid1(p));
  55. spotArea.Add(combine);
  56. for (int j = join.Count - 1; j >= 0; j--)
  57. spotArea.RemoveAt(join.ElementAt(j).Key);
  58. }
  59. }
  60. return spotArea;
  61. }
  62. Vector2 center = default; // 该项目里center不可能等于0,0
  63. public Vector2 Center
  64. {
  65. get
  66. {
  67. if (center != default)
  68. return center;
  69. foreach (var p in Pixels0)
  70. {
  71. center += p;
  72. }
  73. return center /= Pixels0.Count;
  74. }
  75. }
  76. float radius = 0;
  77. public float Radius
  78. {
  79. set
  80. {
  81. radius = value;
  82. }
  83. get
  84. {
  85. if (radius != 0)
  86. return radius;
  87. //var radiusDic = new Dictionary<int, float>() { { 0, 0 }, { 45, 0 }, { 90, 0 }, { 135, 0 }, { 180, 0 }, { 225, 0 }, { 270, 0 }, { 315, 0 } };
  88. var radiusDic = new float[8];
  89. foreach (var p in Pixels0)
  90. {
  91. var dir = p - Center;
  92. var radius = dir.magnitude;
  93. var degreeI = radius < 0.00001f ? 0 : (int)(dir.DegreeToXAxis() / 45);
  94. if (degreeI > 7) degreeI = 0; // 防止正好360度
  95. if (radius > radiusDic[degreeI])
  96. radiusDic[degreeI] = radius;
  97. }
  98. foreach (var p in Pixels1)
  99. {
  100. var dir = p - Center;
  101. var radius = dir.magnitude;
  102. var degreeI = radius < 0.00001f ? 0 : (int)(dir.DegreeToXAxis() / 45);
  103. if (degreeI > 7) degreeI = 0;
  104. if (radius > radiusDic[degreeI])
  105. radiusDic[degreeI] = radius;
  106. }
  107. return radius = radiusDic.Mean();
  108. }
  109. }
  110. public List<Vector2> Pixels0;
  111. public List<Vector2> Pixels1;
  112. HashSet<(int, int)> grids0;
  113. HashSet<(int, int)> grids1;
  114. public PixelSpotArea(Vector2 location, (int, int) grid0, (int, int) grid1)
  115. {
  116. Pixels0 = new List<Vector2>(1000); // 预估的初始容量
  117. Pixels1 = new List<Vector2>(5000);
  118. this.grids0 = new HashSet<(int, int)>(); // hashset如果设置了太大的容量会降低效率
  119. this.grids1 = new HashSet<(int, int)>();
  120. Join(location, grid0, grid1);
  121. }
  122. public PixelSpotArea(IList<PixelSpotArea> areas)
  123. {
  124. Pixels0 = new List<Vector2>(1000);
  125. Pixels1 = new List<Vector2>(5000);
  126. grids0 = new HashSet<(int, int)>();
  127. grids1 = new HashSet<(int, int)>();
  128. foreach (var a in areas)
  129. {
  130. Pixels0.AddRange(a.Pixels0);
  131. grids0.AddRange(a.grids0);
  132. grids1.AddRange(a.grids1);
  133. //Center += a.Center * a.SpotPointCount;
  134. //SpotPointCount += a.SpotPointCount;
  135. }
  136. //Center /= SpotPointCount;
  137. }
  138. public bool Include0((int, int) grid) => grids0.Contains(grid);
  139. public bool Include1((int, int) grid) => grids1.Contains(grid);
  140. public void Join(Vector2 point) => Join(point, GetGrid0(point), GetGrid1(point));
  141. public void Join(Vector2 point, (int x, int y) grid0, (int x, int y) grid1)
  142. {
  143. Pixels0.Add(point);
  144. //SpotPointCount++;
  145. //Center += (point - Center) / SpotPointCount;
  146. grids0.AddRange(new (int, int)[]
  147. {
  148. (grid0.x + -1, grid0.y + 0) , (grid0.x + 1 , grid0.y + 0),
  149. (grid0.x + 1, grid0.y + -1) , (grid0.x + -1, grid0.y + 1),
  150. (grid0.x + 0, grid0.y + -1) , (grid0.x + 0 , grid0.y + 1),
  151. (grid0.x + -1, grid0.y + -1) , (grid0.x + 1 , grid0.y + 1),
  152. grid0
  153. });
  154. grids1.AddRange(new (int, int)[]
  155. {
  156. (grid1.x + -1, grid1.y + 0) , (grid1.x + 1 , grid1.y + 0),
  157. (grid1.x + 1, grid1.y + -1) , (grid1.x + -1, grid1.y + 1),
  158. (grid1.x + 0, grid1.y + -1) , (grid1.x + 0 , grid1.y + 1),
  159. (grid1.x + -1, grid1.y + -1) , (grid1.x + 1 , grid1.y + 1),
  160. grid1
  161. });
  162. }
  163. // 不再join之后,才可以用add添加周围泛光
  164. public void Add(Vector2 point)
  165. {
  166. //var radius = (point - Center).LengthManhattan();
  167. //if (radius > MaxRadius)
  168. // MaxRadius = radius;
  169. Pixels1.Add(point);
  170. //var radius = (point - Center).LengthManhattan();
  171. //if (radius > MaxRadius)
  172. // MaxRadius = radius;
  173. }
  174. public float TotalBrightness(Color[] pixels, Func<int, int, int> Vector2ToIndex, int outer_size = 3)
  175. {
  176. (int x, int y) rectMin = ((int)(Center.x - Radius - outer_size), (int)(Center.y - Radius - outer_size));
  177. (int x, int y) rectMax = ((int)(Center.x + Radius + outer_size), (int)(Center.y + Radius + outer_size));
  178. var total = 0f;
  179. Parallel.For(rectMin.x, rectMax.x, (i) =>
  180. {
  181. var t = 0;
  182. for (int j = rectMin.y; j < rectMax.y; j++)
  183. {
  184. var index = Vector2ToIndex(i, j);
  185. var b = pixels[index].Brightness(64);
  186. t += b;
  187. }
  188. lock (InfraredLocate.locker)
  189. {
  190. total += t;
  191. }
  192. });
  193. //total /= (rectMax.y - rectMin.y) * (rectMax.x - rectMin.x);
  194. return total;
  195. }
  196. }
  197. }