PixelSpotArea_DbScan.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. using DbscanImplementation;
  2. using o0;
  3. using o0.Num;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Threading.Tasks;
  8. using Unity.VisualScripting;
  9. using UnityEngine;
  10. using ZIM.Unity;
  11. using static ScreenLocate;
  12. using Color = UnityEngine.Color;
  13. namespace ZIM
  14. {
  15. // 亮区的点用来定位(计算center),泛光区域的点用来计算radius
  16. public class PixelSpotArea_DbScan : ISpotArea
  17. {
  18. // 中心
  19. Vector2 centroid = default; // 该项目里center不可能等于0,0
  20. public Vector2 Centroid
  21. {
  22. get
  23. {
  24. if (centroid != default)
  25. return centroid;
  26. foreach (var p in Pixels0)
  27. centroid += p;
  28. return centroid /= Pixels0.Count;
  29. }
  30. }
  31. // 亮区的半径,计算含泛光点
  32. float radius = 0;
  33. public float Radius
  34. {
  35. set
  36. {
  37. radius = value;
  38. }
  39. get
  40. {
  41. if (radius != 0)
  42. return radius;
  43. return CalculateRadius(); // 函数里会赋值 radius
  44. }
  45. }
  46. public float CalculateRadius()
  47. {
  48. //var radiusDic = new Dictionary<int, float>() { { 0, 0 }, { 45, 0 }, { 90, 0 }, { 135, 0 }, { 180, 0 }, { 225, 0 }, { 270, 0 }, { 315, 0 } };
  49. var radiusDic = new float[8];
  50. foreach (var p in Pixels0)
  51. {
  52. var dir = p - Centroid;
  53. var radius = dir.magnitude;
  54. var degreeI = radius < 0.00001f ? 0 : (int)(dir.DegreeToXAxis() / 45);
  55. if (degreeI > 7) degreeI = 0; // 防止正好360度
  56. if (radius > radiusDic[degreeI])
  57. radiusDic[degreeI] = radius;
  58. }
  59. foreach (var p in Pixels1)
  60. {
  61. var dir = p - Centroid;
  62. var radius = dir.magnitude;
  63. var degreeI = radius < 0.00001f ? 0 : (int)(dir.DegreeToXAxis() / 45);
  64. if (degreeI > 7) degreeI = 0;
  65. if (radius > radiusDic[degreeI])
  66. radiusDic[degreeI] = radius;
  67. }
  68. return radius = radiusDic.Mean();
  69. }
  70. // 中心亮点
  71. public List<Vector2> Pixels0 { get; set; }
  72. // 泛光
  73. public List<Vector2> Pixels1 { get; set; }
  74. public PixelSpotArea_DbScan(IEnumerable<Vector2> points) // kmeans中用随机点初始化,作为中心
  75. {
  76. Pixels0 = new List<Vector2>();
  77. Pixels1 = new List<Vector2>();
  78. Pixels0.AddRange(points);
  79. }
  80. public PixelSpotArea_DbScan(List<DbscanPoint<Vector2>> points)
  81. {
  82. Pixels0 = new List<Vector2>();
  83. Pixels1 = new List<Vector2>();
  84. Pixels0.AddRange(points.Select((i) => i.Point));
  85. }
  86. // 中心亮点
  87. public void Add0(Vector2 p)
  88. {
  89. Pixels0.Add(p);
  90. }
  91. // 泛光
  92. public void Add1(Vector2 point)
  93. {
  94. Pixels1.Add(point);
  95. }
  96. public void Clear()
  97. {
  98. Pixels0.Clear();
  99. Pixels1.Clear();
  100. }
  101. public float TotalBrightness(Color[] pixels, Func<int, int, int> Vector2ToIndex, int outer_size = 3)
  102. {
  103. (int x, int y) rectMin = ((int)(Centroid.x - Radius - outer_size), (int)(Centroid.y - Radius - outer_size));
  104. (int x, int y) rectMax = ((int)(Centroid.x + Radius + outer_size), (int)(Centroid.y + Radius + outer_size));
  105. var total = 0f;
  106. Parallel.For(rectMin.x, rectMax.x, (i) =>
  107. {
  108. var t = 0;
  109. for (int j = rectMin.y; j < rectMax.y; j++)
  110. {
  111. var index = Vector2ToIndex(i, j);
  112. var b = pixels[index].Brightness(64);
  113. t += b;
  114. }
  115. lock (InfraredLocate.locker)
  116. {
  117. total += t;
  118. }
  119. });
  120. //total /= (rectMax.y - rectMin.y) * (rectMax.x - rectMin.x);
  121. return total;
  122. }
  123. }
  124. }