zimIdentifyLineLSD.cs 11 KB

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