o0InfraredIdentification.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. using o0.Geometry;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using Unity.VisualScripting;
  8. using UnityEngine;
  9. using UnityEngine.UI;
  10. namespace o0.Project
  11. {
  12. public partial class InfraredIdentification
  13. {
  14. public Geometry2D.Vector<int> Size;
  15. public InfraredIdentification(Geometry2D.Vector<int> Size)
  16. {
  17. this.Size = Size;
  18. }
  19. int DelayWhite = 0;
  20. int CaptureWhite = 0;
  21. int DelayBlack = 0;
  22. int CaptureBlack = 0;
  23. Geometry.Vector<float>[] ScreenLocateTexture = null;
  24. int ScreenCaptureCount = 0;
  25. public void LocateScreen(int Capture = 30, int Delay = 30)//frame
  26. {
  27. DelayWhite = Delay;
  28. CaptureWhite = Capture;
  29. DelayBlack = Delay;
  30. CaptureBlack = Capture;
  31. ScreenCaptureCount = Capture;
  32. o0WebCamera.SetScreen(UnityEngine.Color.white);
  33. }
  34. public void Update(WebCamTexture texture)
  35. {
  36. if (texture.width != Size.x || texture.height != Size.y)
  37. return;//纹理大小不一致
  38. if (DelayWhite != 0)
  39. {
  40. DelayWhite--;
  41. return;
  42. }
  43. if (CaptureWhite != 0)
  44. {
  45. CaptureWhite--;
  46. if(ScreenLocateTexture == null)
  47. ScreenLocateTexture = new Geometry.Vector<float>[Size.x * Size.y];
  48. var pixel = texture.GetPixels();
  49. Parallel.For(0, Size.x * Size.y, i =>
  50. {
  51. var ip = pixel[i];
  52. ScreenLocateTexture[i] += new Geometry.Vector<float>(ip.r, ip.g, ip.b);
  53. });
  54. if (CaptureWhite == 0)
  55. o0WebCamera.SetScreen(UnityEngine.Color.black);
  56. return;
  57. }
  58. if (DelayBlack != 0)
  59. {
  60. DelayBlack--;
  61. return;
  62. }
  63. if (CaptureBlack != 0)
  64. {
  65. CaptureBlack--;
  66. var pixel = texture.GetPixels();
  67. Parallel.For(0, Size.x * Size.y, i =>
  68. {
  69. var ip = pixel[i];
  70. ScreenLocateTexture[i] -= new Geometry.Vector<float>(ip.r, ip.g, ip.b);
  71. });
  72. if (CaptureBlack == 0)
  73. {
  74. o0WebCamera.SetScreen(null);
  75. UnityEngine.Color[] newPixel = new UnityEngine.Color[Size.x * Size.y];
  76. Parallel.For(0, Size.x * Size.y, i => {
  77. var pi = ScreenLocateTexture[i] /= ScreenCaptureCount;
  78. newPixel[i] = new UnityEngine.Color(pi.x, pi.y, pi.z);
  79. });
  80. //读取数据
  81. {
  82. var fileName = "屏幕定位数据20230505_145229.bin";
  83. ScreenLocateTexture = $"TestData/{fileName}".FileReadByte<Vector<float>[]>();
  84. Debug.Log($"Read {fileName}");
  85. Parallel.For(0, Size.x * Size.y, i =>
  86. {
  87. var pi = ScreenLocateTexture[i];
  88. newPixel[i] = new UnityEngine.Color(pi.x, pi.y, pi.z);
  89. });
  90. }
  91. var ScreenLocateTex = new Texture2D(Size.x, Size.y);
  92. ScreenLocateTex.SetPixels(newPixel);
  93. ScreenLocateTex.Apply();
  94. //o0WebCamera.DebugTexture(2, ScreenLocateTex);
  95. var ScreenLocateTexLighted = ScreenLocateTex.AutoLight(10);
  96. o0WebCamera.DebugTexture(2, ScreenLocateTexLighted);
  97. //var FileSavePath = Application.persistentDataPath + "/ScreenLocateTexture.bin";
  98. bool Save = false;
  99. if (Save)
  100. {
  101. var FileSavePath = "屏幕定位数据.bin";
  102. FileSavePath.FileWriteByte(ScreenLocateTexture);
  103. Debug.Log("ScreenLocateTexture Saved To: " + FileSavePath);
  104. }
  105. var ScreenLocateTexR = ScreenLocateTexLighted.ToRGB(ColorChannel.Red);
  106. var ScreenLocateTexG = ScreenLocateTexLighted.ToRGB(ColorChannel.Green);
  107. var ScreenLocateTexB = ScreenLocateTexLighted.ToRGB(ColorChannel.Blue);
  108. o0WebCamera.DebugTexture(3, ScreenLocateTexR);
  109. //o0WebCamera.DebugTexture(4, ScreenLocateTexG);
  110. //o0WebCamera.DebugTexture(5, ScreenLocateTexB);
  111. var watch = new System.Diagnostics.Stopwatch();
  112. watch.Start();
  113. var times = new List<double>() { 0.0 };
  114. var ScreenLocateTexLightedMat = ScreenLocateTexLighted.Too0Mat();
  115. //var ScreenLocateTexLightedMat = texture.Too0Mat();
  116. //var (edge, edgeDir) = ScreenLocateTexLightedMat.IdentifyEdge();
  117. var (edge, edgeDir) = ScreenLocateTexLightedMat.zimIdentifyEdgeGradientAny(15);
  118. //o0WebCamera.DebugTexture(4, ScreenLocateTexLighted.Too0Mat().IdentifyEdgeGradient().ToTex());
  119. o0WebCamera.DebugTexture(4, edge.ToTex());
  120. var quadLines = ScreenLocateTexLightedMat.ZIMIdentifyQuadLSD(edge, edgeDir, out _, out List<Geometry2D.Float.Line> possibleLines, out _, null, 15, 100);
  121. var drawLineMap = new Matrix(edge.Size, Tiling: true);
  122. //drawLineMap.DrawLine(quadLines[0], (x, y) => 1, new Geometry2D.Float.Vector(0, 10));
  123. foreach (var l in quadLines)
  124. {
  125. if (l != null)
  126. drawLineMap.DrawLine(l, (x, y) => 1, new Geometry2D.Float.Vector(0, 10));
  127. }
  128. //var lines = edge.IdentifyLineLSD(edgeDir, 100);
  129. ////var lines = ScreenLocateTexLightedMat.IdentifyLineLSD();
  130. //var drawLineMap = new MatrixF2D(edge.Size.x, edge.Size.y);
  131. //var returnMaxLines = lines.Sub(0, 10);
  132. //foreach (var (line, sum, gradient) in returnMaxLines)
  133. // drawLineMap.DrawLine(line, (x, y) => 1, new Geometry2D.Float.Vector(0, 10));
  134. o0WebCamera.DebugTexture(5, drawLineMap.ToTex());
  135. drawLineMap = new Matrix(edge.Size, Tiling: true);
  136. for (int i = 0; i < possibleLines.Count; i++)
  137. {
  138. var l = possibleLines[i];
  139. if (i < 4)
  140. drawLineMap.DrawLine(l, (x, y) => 1, new Geometry2D.Float.Vector(0, 10));
  141. else
  142. drawLineMap.DrawLine(l, (x, y) => 1, new Geometry2D.Float.Vector(0, 2));
  143. }
  144. o0WebCamera.DebugTexture(2, drawLineMap.ToTex());
  145. times.Add(watch.ElapsedMilliseconds);
  146. UnityEngine.Debug.Log("time: " + (times[times.Count - 1] - times[times.Count - 2]));
  147. //o0WebCamera.DebugTexture(5, edge.IdentifyLine(edgeDir).ToTex());
  148. //o0WebCamera.DebugTexture(4, ScreenLocateTexLighted.Too0Mat().IdentifyEdgeGradientX().ToTex());
  149. //o0WebCamera.DebugTexture(5, ScreenLocateTexLighted.Too0Mat().IdentifyEdgeGradientY().ToTex());
  150. //var convolutionLighted2 = ScreenLocateTexLighted.Too0Mat().IdentifyEdgeVariance().ToTex();
  151. // opecncv处理
  152. // zim
  153. {
  154. //var cvLines = edge.cvHoughLinesP();
  155. //o0WebCamera.DebugTexture(5, cvLines);
  156. //var myLines = Hough.Transform(edgeMat);
  157. //var cvLines = edge.cvLine(myLines);
  158. //o0WebCamera.DebugTexture(5, cvLines);
  159. }
  160. //o0WebCamera.DebugTexture(4, convolutionLighted2);
  161. }
  162. return;
  163. }
  164. /*
  165. var avg = new Geometry4D.Vector<float>();
  166. var pixel = texture.GetPixels();
  167. foreach(var i in pixel.Index())
  168. {
  169. var iP = pixel[i];
  170. avg += new Geometry4D.Vector<float>(iP.r, iP.g, iP.b, iP.a);
  171. }
  172. avg /= pixel.Count();/**/
  173. var newTex = texture.AutoLight(10);
  174. o0WebCamera.DebugTexture(1, newTex);/**/
  175. var texLightedR = newTex.ToRGB(ColorChannel.Red);
  176. var texLightedG = newTex.ToRGB(ColorChannel.Green);
  177. var texLightedB = newTex.ToRGB(ColorChannel.Blue);
  178. o0WebCamera.DebugTexture(3, texLightedR);
  179. o0WebCamera.DebugTexture(4, texLightedG);
  180. o0WebCamera.DebugTexture(5, texLightedB);/**/
  181. //Debug.Log(avg);
  182. }
  183. }
  184. }