zimIdentifyLineLSD.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using UnityEngine;
  6. using o0.Geometry2D.Float;
  7. using ZIM.Unity;
  8. using o0.Num;
  9. using o0InfraredLocate.ZIM;
  10. namespace o0.Project
  11. {
  12. public static partial class Extension
  13. { // (垂直方向,水平方向)
  14. public static (Vector, float) ZIMLineSegGradient(Matrix mat, Matrix dir, float gradient, Vector start, Vector end, (int, int) range, bool reverseXY)
  15. {
  16. int startX, endX;
  17. if (start.x < end.x)
  18. {
  19. startX = Mathf.RoundToInt(start.x) + 1;
  20. endX = Mathf.RoundToInt(end.x) + 1;
  21. }
  22. else
  23. {
  24. startX = Mathf.RoundToInt(start.x) - 1;
  25. endX = Mathf.RoundToInt(end.x) - 1;
  26. }
  27. if (startX == endX)
  28. return (new Vector(start.x, start.y), 0);
  29. var xDiff = end.x - start.x;
  30. var xToYRate = (float)(end.y - start.y) / xDiff;
  31. Func<float, float> xToYFunc = (x) => (x - start.x) * xToYRate + start.y;
  32. var B1 = Mathf.RoundToInt(start.x);
  33. var B1V = mat[B1, Mathf.RoundToInt(start.y)];
  34. var Sum1 = B1V;
  35. foreach (var Bx in startX.Range(endX))
  36. {
  37. int[,] CIArray = new int[range.Item1, range.Item2];
  38. float[] CAArray = new float[range.Item1 * range.Item2];
  39. float[] CMArray = new float[range.Item1 * range.Item2];
  40. for (int j = 0; j < range.Item2; j++)
  41. {
  42. var x = xDiff > 0 ? Bx + j : Bx - j;
  43. var y = xToYFunc(x);
  44. var m1 = range.Item1 / 2 - 1;
  45. var m2 = range.Item1 / 2;
  46. var y1 = Mathf.FloorToInt(y);
  47. var y2 = Mathf.CeilToInt(y);
  48. for (int i = 0; i < range.Item1 / 2; i++)
  49. {
  50. if (reverseXY)
  51. CIArray[i, j] = mat.Index(y1 + i - m1, x);
  52. else
  53. CIArray[i, j] = mat.Index(x, y1 + i - m1);
  54. CAArray[i + j * range.Item1] = dir.Element[CIArray[i, j]];
  55. CMArray[i + j * range.Item1] = mat.Element[CIArray[i, j]];
  56. }
  57. for (int i = range.Item1 / 2; i < range.Item1; i++)
  58. {
  59. if (reverseXY)
  60. CIArray[i, j] = mat.Index(y2 + i - m2, x);
  61. else
  62. CIArray[i, j] = mat.Index(x, y2 + i - m2);
  63. CAArray[i + j * range.Item1] = dir.Element[CIArray[i, j]];
  64. CMArray[i + j * range.Item1] = mat.Element[CIArray[i, j]];
  65. }
  66. }
  67. var CV = o0.Max(CMArray);
  68. var avaAngle = 60;
  69. var halfAvaAngle = avaAngle / 2;
  70. var breakFlag = true;
  71. foreach (var ca in CAArray)
  72. {
  73. if (MathF.Abs(ca - gradient) <= halfAvaAngle || MathF.Abs(ca - 360 - gradient) <= halfAvaAngle || MathF.Abs(ca + 360 - gradient) <= halfAvaAngle)
  74. {
  75. breakFlag = false;
  76. break;
  77. }
  78. }
  79. if (breakFlag)
  80. break;
  81. B1 = Bx;
  82. Sum1 += CV;
  83. }
  84. return (new Vector(B1, xToYFunc(B1)), Sum1);
  85. }
  86. // Old, 弃用的 ------------
  87. public static List<Line> ZIMIdentifyQuadLSD(this Matrix screenLocateMat, Matrix edgeMat, Matrix edgeDirMat,
  88. out Line[] oldLines, out List<Line> possibleLines, out List<(Line, float, float)> allLines, ScreenMap screen,
  89. float gradientLength, float minLength = 100)
  90. {
  91. // 加权平均
  92. Vector[] avgPointsColumn = new Vector[screenLocateMat.Size.x];
  93. float[] valueSumsColumn = new float[screenLocateMat.Size.x];
  94. Parallel.For(0, screenLocateMat.Size.x, i =>
  95. {
  96. for (int j = 0; j < screenLocateMat.Size.y; j++)
  97. {
  98. var value = screenLocateMat[i, j];
  99. valueSumsColumn[i] += value;
  100. avgPointsColumn[i] += new Vector(i, j) * value;
  101. }
  102. });
  103. Vector avgPoint = Vector.Zero;
  104. var valueSum = 0f;
  105. for (int i = 0; i < screenLocateMat.Size.x; i++)
  106. {
  107. avgPoint += avgPointsColumn[i];
  108. valueSum += valueSumsColumn[i];
  109. }
  110. avgPoint /= valueSum;
  111. allLines = edgeMat.IdentifyLineLSD(edgeDirMat, minLength, 20, LineCaptureSize: new Vector(0, 5));
  112. //Debug.Log("[IdentifyLineLSD] lines.Count: " + lines.Count);
  113. // LSD计算得到的矩阵尺寸较小(因为卷积),这里必须进行位移
  114. var offset = new Vector((screenLocateMat.Size.x - edgeMat.Size.x) / 2, (screenLocateMat.Size.y - edgeMat.Size.y) / 2);
  115. for (int i = 0; i < allLines.Count; i++)
  116. allLines[i] = (allLines[i].Item1 + offset, allLines[i].Item2, allLines[i].Item3);
  117. // 沿直线计算综合梯度(梯度乘以长度系数)
  118. float estimateGradient(Line line)
  119. {
  120. var dir = (line.B - line.A).Normalized;
  121. var vertical = new Vector(-dir.y, dir.x) * (gradientLength / 2);
  122. var step = 2;
  123. var ll = line.Length;
  124. var lg = new List<float>();
  125. for (int i = 0; i <= ll; i += step)
  126. {
  127. var point = line.A + dir * i;
  128. var ga = point + vertical;
  129. var gb = point - vertical;
  130. lg.Add(screenLocateMat[(int)ga.x, (int)ga.y] - screenLocateMat[(int)gb.x, (int)gb.y]);
  131. }
  132. float e = (float)Math.Sqrt(Math.Max(1, line.Length / minLength / 3)); // 长度系数,筛选时梯度更大、长度更长的线段更优
  133. return e * Math.Abs(lg.Mean());
  134. }
  135. // 下、右、上、左
  136. var quadLines = new List<(float, Line)>[4] {new List<(float, Line)>(), new List<(float, Line)>(), new List<(float, Line)>(), new List<(float, Line)>() };
  137. possibleLines = new List<Line>();
  138. oldLines = null;
  139. // 如果已有定位数据,根据现有数据筛选线条
  140. if (screen.QuadInCamera != null)
  141. {
  142. Debug.Log("[IdentifyLineLSD] 根据已有定位数据做筛选");
  143. screen.RefreshCameraSize(new Vector2(screenLocateMat.Size.x, screenLocateMat.Size.y));
  144. var calibration = ScreenLocate.Main.ReDoLocateCalibrationRatio * screenLocateMat.Size.y;
  145. oldLines = screen.QuadInCamera.GetLines();
  146. var pedals = oldLines.Select((i) => o0Extension.PointPedal(i, avgPoint, out _)).ToArray(); // 当前定位的垂足,下、右、上、左
  147. foreach (var i in allLines)
  148. {
  149. float minDistance = float.MaxValue;
  150. int index = -1;
  151. foreach (var j in pedals.Index())
  152. {
  153. var d = (o0Extension.PointPedal(i.Item1, avgPoint, out _) - pedals[j]).Length;
  154. if (d < minDistance)
  155. {
  156. minDistance = d;
  157. index = j;
  158. }
  159. }
  160. //Debug.Log(minDistance +", -----------"+ calibration);
  161. if (minDistance < calibration) // 垂足的距离足够近
  162. {
  163. quadLines[index].Add((estimateGradient(i.Item1), i.Item1));
  164. possibleLines.Add(i.Item1);
  165. }
  166. }
  167. }
  168. else
  169. {
  170. var avaAngleHalf = 75f;
  171. foreach (var (line, sum, gradient) in allLines)
  172. {
  173. possibleLines.Add(line);
  174. var a = (avgPoint - (line.A + line.B) / 2).DegreeToXAxis();
  175. //Debug.Log(a + ", " + gradient + ", " + sum);
  176. int index = -1;
  177. if (Math.Abs(a - gradient) < avaAngleHalf || Math.Abs(a - 360 - gradient) < avaAngleHalf || Math.Abs(a + 360 - gradient) < avaAngleHalf)
  178. {
  179. if (gradient > 45 && gradient < 135) // 下
  180. index = 0;
  181. else if (gradient > 135 && gradient < 225) // 右
  182. index = 1;
  183. else if (gradient > 225 && gradient < 315) // 上
  184. index = 2;
  185. else
  186. index = 3;
  187. //var g = Math.Abs(lg.Mean());
  188. //Debug.Log(gradient + ", " + g);
  189. //List<float> lp1 = new List<float>(), lp2 = new List<float>(); // 线两侧的值
  190. //for (float i = 0; i <= ll; i += step)
  191. //{
  192. // var point = line.A + dir * i;
  193. // var ga = point + vertical;
  194. // var gb = point - vertical;
  195. // lp1.Add(screenLocateMat[(int)ga.x, (int)ga.y]);
  196. // lp2.Add(screenLocateMat[(int)gb.x, (int)gb.y]);
  197. //}
  198. //var avg1 = lp1.Mean();
  199. //var avg2 = lp2.Mean();
  200. //var v1 = lp1.Variance();
  201. //var v2 = lp2.Variance();
  202. //var lineGradient = Math.Abs(avg1 - avg2) / (v1 + v2 + 0.2f); // 方差越小,梯度的价值越高
  203. ////var g = Math.Abs(lg.Mean());
  204. ////Debug.Log(gradient + ", " + g);
  205. //Debug.Log(v1 + ", " + v2 + ", " + lineGradient);
  206. //quadLines[index].Add((lineGradient, line));
  207. quadLines[index].Add((estimateGradient(line), line));
  208. }
  209. }
  210. }
  211. var result = new Line[4];
  212. for (int i = 0; i < 4; i++)
  213. {
  214. if (quadLines[i].Count > 0)
  215. result[i] = quadLines[i].Max((a, b) =>a.Item1.CompareTo(b.Item1)).Item2;
  216. }
  217. return result.ToList();
  218. }
  219. }
  220. }