o0InfraredIdentification.cs 8.9 KB

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