PixelSpotArea.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 (radius > radiusDic[degreeI])
  95. radiusDic[degreeI] = radius;
  96. }
  97. foreach (var p in Pixels1)
  98. {
  99. var dir = p - Center;
  100. var radius = dir.magnitude;
  101. var degreeI = radius < 0.00001f ? 0 : (int)(dir.DegreeToXAxis() / 45);
  102. if (radius > radiusDic[degreeI])
  103. radiusDic[degreeI] = radius;
  104. }
  105. return radius = radiusDic.Mean();
  106. }
  107. }
  108. public List<Vector2> Pixels0;
  109. public List<Vector2> Pixels1;
  110. HashSet<(int, int)> grids0;
  111. HashSet<(int, int)> grids1;
  112. public PixelSpotArea(Vector2 location, (int, int) grid0, (int, int) grid1)
  113. {
  114. Pixels0 = new List<Vector2>(1000); // 预估的初始容量
  115. Pixels1 = new List<Vector2>(5000);
  116. this.grids0 = new HashSet<(int, int)>(); // hashset如果设置了太大的容量会降低效率
  117. this.grids1 = new HashSet<(int, int)>();
  118. Join(location, grid0, grid1);
  119. }
  120. public PixelSpotArea(IList<PixelSpotArea> areas)
  121. {
  122. Pixels0 = new List<Vector2>(1000);
  123. Pixels1 = new List<Vector2>(5000);
  124. grids0 = new HashSet<(int, int)>();
  125. grids1 = new HashSet<(int, int)>();
  126. foreach (var a in areas)
  127. {
  128. Pixels0.AddRange(a.Pixels0);
  129. grids0.AddRange(a.grids0);
  130. grids1.AddRange(a.grids1);
  131. //Center += a.Center * a.SpotPointCount;
  132. //SpotPointCount += a.SpotPointCount;
  133. }
  134. //Center /= SpotPointCount;
  135. }
  136. public bool Include0((int, int) grid) => grids0.Contains(grid);
  137. public bool Include1((int, int) grid) => grids1.Contains(grid);
  138. public void Join(Vector2 point) => Join(point, GetGrid0(point), GetGrid1(point));
  139. public void Join(Vector2 point, (int x, int y) grid0, (int x, int y) grid1)
  140. {
  141. Pixels0.Add(point);
  142. //SpotPointCount++;
  143. //Center += (point - Center) / SpotPointCount;
  144. grids0.AddRange(new (int, int)[]
  145. {
  146. (grid0.x + -1, grid0.y + 0) , (grid0.x + 1 , grid0.y + 0),
  147. (grid0.x + 1, grid0.y + -1) , (grid0.x + -1, grid0.y + 1),
  148. (grid0.x + 0, grid0.y + -1) , (grid0.x + 0 , grid0.y + 1),
  149. (grid0.x + -1, grid0.y + -1) , (grid0.x + 1 , grid0.y + 1),
  150. grid0
  151. });
  152. grids1.AddRange(new (int, int)[]
  153. {
  154. (grid1.x + -1, grid1.y + 0) , (grid1.x + 1 , grid1.y + 0),
  155. (grid1.x + 1, grid1.y + -1) , (grid1.x + -1, grid1.y + 1),
  156. (grid1.x + 0, grid1.y + -1) , (grid1.x + 0 , grid1.y + 1),
  157. (grid1.x + -1, grid1.y + -1) , (grid1.x + 1 , grid1.y + 1),
  158. grid1
  159. });
  160. }
  161. // 不再join之后,才可以用add添加周围泛光
  162. public void Add(Vector2 point)
  163. {
  164. //var radius = (point - Center).LengthManhattan();
  165. //if (radius > MaxRadius)
  166. // MaxRadius = radius;
  167. Pixels1.Add(point);
  168. //var radius = (point - Center).LengthManhattan();
  169. //if (radius > MaxRadius)
  170. // MaxRadius = radius;
  171. }
  172. public float TotalBrightness(Color[] pixels, Func<int, int, int> Vector2ToIndex, int outer_size = 3)
  173. {
  174. (int x, int y) rectMin = ((int)(Center.x - Radius - outer_size), (int)(Center.y - Radius - outer_size));
  175. (int x, int y) rectMax = ((int)(Center.x + Radius + outer_size), (int)(Center.y + Radius + outer_size));
  176. var total = 0f;
  177. Parallel.For(rectMin.x, rectMax.x, (i) =>
  178. {
  179. var t = 0;
  180. for (int j = rectMin.y; j < rectMax.y; j++)
  181. {
  182. var index = Vector2ToIndex(i, j);
  183. var b = pixels[index].Brightness(64);
  184. t += b;
  185. }
  186. lock (InfraredLocate.locker)
  187. {
  188. total += t;
  189. }
  190. });
  191. //total /= (rectMax.y - rectMin.y) * (rectMax.x - rectMin.x);
  192. return total;
  193. }
  194. }
  195. }