InfraredLocate.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  1. using o0;
  2. using o0.Project;
  3. using SixLabors.ImageSharp;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Runtime.Serialization.Formatters;
  8. using System.Threading.Tasks;
  9. using UnityEngine;
  10. using ZIM.Unity;
  11. using Color = UnityEngine.Color;
  12. using Debug = UnityEngine.Debug;
  13. namespace ZIM
  14. {
  15. //class DescendingComparer<T> : IComparer<T>
  16. //{
  17. // public int Compare(T x, T y)
  18. // {
  19. // return Comparer<T>.Default.Compare(y, x);
  20. // }
  21. //}
  22. public enum InfraredMatch
  23. {
  24. Nomatch = 0,
  25. Match0 = 1,
  26. Match1 = 2
  27. }
  28. // 支持2个红外点的识别
  29. public class InfraredLocate
  30. {
  31. public static object locker = new object();
  32. public static List<InfraredMatch> GetMatches(InfraredMatch im)
  33. {
  34. List<InfraredMatch> positions = new List<InfraredMatch>();
  35. InfraredMatch bit = InfraredMatch.Match0;
  36. while (bit <= im)
  37. {
  38. if ((im & bit) != 0)
  39. {
  40. positions.Add(bit);
  41. }
  42. bit = (InfraredMatch)((int)bit << 1);
  43. }
  44. return positions;
  45. }
  46. readonly int samplingScale = 2;
  47. readonly float[] spotBrightness = new float[] { 0.93f, 0.5f }; // 亮点阈值
  48. //const float circleVariance = 30f;
  49. //const int brightAreaRadius = 30;
  50. //const int LeastBrightPoint = 10000;
  51. Serenegiant.UVC.UVCManager.CameraInfo mCameraInfo;
  52. ScreenIdentification screenIdentification;
  53. InfraredSpotSettings infraredSpotSettings;
  54. InfraredSpot[] InfraredSpots;
  55. public InfraredSpot GetSpot(InfraredMatch match)
  56. {
  57. switch (match)
  58. {
  59. case InfraredMatch.Match0:
  60. return InfraredSpots[0];
  61. case InfraredMatch.Match1:
  62. return InfraredSpots[1];
  63. default:
  64. return null;
  65. }
  66. }
  67. public InfraredLocate(Serenegiant.UVC.UVCManager.CameraInfo cameraInfo, ScreenIdentification infraredIdentification, InfraredSpotSettings infraredSpotSettings)
  68. {
  69. this.mCameraInfo = cameraInfo;
  70. this.screenIdentification = infraredIdentification;
  71. this.infraredSpotSettings = infraredSpotSettings;
  72. }
  73. readonly int CheakFrame = 10;
  74. bool Infrared12Overlap = false;
  75. int cheakCounter = 0;
  76. public InfraredSpot[] UpdateSingle(Color[] cameraPixels)
  77. {
  78. var spotArea = LocateToScreen(cameraPixels, screenIdentification.Screen.QuadRect);
  79. if (spotArea.Count > 0)
  80. {
  81. InfraredSpots[0].Update(spotArea.Max((a, b) => a.Radius.CompareTo(b.Radius)));
  82. }
  83. return InfraredSpots;
  84. }
  85. // New, 通过透视映射计算红外点的相对位置, 返回的红外点根据半径 从大到小排序
  86. public InfraredSpot[] Update(Color[] cameraPixels)
  87. {
  88. var spotArea = LocateToScreen(cameraPixels, screenIdentification.Screen.QuadRect);
  89. return MatchInfraredRay(spotArea);
  90. }
  91. // New, 返回由大到小排序的点
  92. public List<PixelSpotArea> LocateToScreen(Color[] pixels, Rect rect)
  93. {
  94. if (InfraredSpots == null)
  95. {
  96. InfraredSpots = new InfraredSpot[2] {
  97. new InfraredSpot(screenIdentification.Screen, InfraredMatch.Match0),
  98. new InfraredSpot(screenIdentification.Screen, InfraredMatch.Match1) };
  99. }
  100. //var watch = new System.Diagnostics.Stopwatch();
  101. //watch.Start();
  102. //var times = new List<double>() { 0.0 };
  103. /* 根据亮点情况调整samplingScale
  104. (int x, int y) rectMin = ((int)rect.min.x / samplingScale, (int)rect.min.y / samplingScale);
  105. (int x, int y) rectMax = ((int)rect.max.x / samplingScale, (int)rect.max.y / samplingScale);
  106. int brightCount = 0;
  107. Parallel.For(rectMin.x, rectMax.x, (i) =>
  108. {
  109. for (int j = rectMin.y; j < rectMax.y; j++)
  110. {
  111. int ip = i * samplingScale;
  112. int jp = j * samplingScale;
  113. var index = camera.Vector2ToIndex(ip, jp);
  114. var b = pixels[index].Brightness(64);
  115. if (b > minBrightness)
  116. {
  117. lock (locker)
  118. {
  119. brightCount++;
  120. }
  121. }
  122. }
  123. });
  124. if (brightCount > 1000)
  125. {
  126. samplingScale = (int)Math.Ceiling(samplingScale * Math.Sqrt(brightCount / 1000.0)); // 如果亮点太多,控制亮点数量在1000左右
  127. }
  128. /**/
  129. (int x, int y) rectMin = ((int)rect.min.x / samplingScale, (int)rect.min.y / samplingScale);
  130. (int x, int y) rectMax = ((int)rect.max.x / samplingScale, (int)rect.max.y / samplingScale);
  131. var spotPoint = new List<Vector2>((int)rect.width * (int)rect.height / 256); // 预估的初始容量
  132. var brightPoint = new List<Vector2>((int)rect.width * (int)rect.height / 64);
  133. Parallel.For(rectMin.x, rectMax.x, (i) =>
  134. {
  135. var points0 = new List<Vector2>(rectMax.y - rectMin.y);
  136. var points1 = new List<Vector2>(rectMax.y - rectMin.y);
  137. for (int j = rectMin.y; j < rectMax.y; j++)
  138. {
  139. int ip = i * samplingScale;
  140. int jp = j * samplingScale;
  141. var index = mCameraInfo.CoordToIndex(ip, jp);
  142. //var b = brightness[index];
  143. //var b = (int)Math.Round(ConvBrightness(brightness, (ip, jp)));
  144. var b = pixels[index].Brightness();
  145. if (b > spotBrightness[0])
  146. {
  147. points0.Add(new Vector2(ip, jp));
  148. }
  149. else if (b > spotBrightness[1])
  150. {
  151. points1.Add(new Vector2(ip, jp));
  152. }
  153. }
  154. lock (spotPoint)
  155. {
  156. spotPoint.AddRange(points0);
  157. brightPoint.AddRange(points1);
  158. }
  159. });
  160. //Debug.Log("spotPoint Count: " + spotPoint.Count + ", brightPoint Count: " + brightPoint.Count);
  161. //times.Add(watch.ElapsedMilliseconds);
  162. //UnityEngine.Debug.Log("time1: " + (times[times.Count - 1] - times[times.Count - 2]));
  163. // 所有点映射到屏幕空间
  164. Parallel.For(0, spotPoint.Count, (i) => spotPoint[i] = screenIdentification.Screen.TransformToScreen(spotPoint[i]));
  165. Parallel.For(0, brightPoint.Count, (i) => brightPoint[i] = screenIdentification.Screen.TransformToScreen(brightPoint[i]));
  166. //{
  167. // var texure = new Texture2D((int)screenIdentification.Screen.UVSize.x + 1, (int)screenIdentification.Screen.UVSize.y + 1);
  168. // var pixels2 = new Color[texure.width * texure.height];
  169. // foreach (var i in pixels2.Index())
  170. // pixels2[i] = Color.white;
  171. // foreach (var i in spotPoint)
  172. // {
  173. // if (!screenIdentification.Screen.UVInScreen(i))
  174. // continue;
  175. // pixels2[(int)i.y * texure.width + (int)i.x] = Color.red;
  176. // }
  177. // foreach (var i in brightPoint)
  178. // {
  179. // if (!screenIdentification.Screen.UVInScreen(i))
  180. // continue;
  181. // pixels2[(int)i.y * texure.width + (int)i.x] = Color.black;
  182. // }
  183. // texure.filterMode = FilterMode.Point;
  184. // texure.SetPixels(pixels2);
  185. // texure.Apply();
  186. // ScreenLocate.DebugTexture(2, texure);
  187. //}
  188. if (spotPoint.Count > 0)
  189. {
  190. //times.Add(watch.ElapsedMilliseconds);
  191. //UnityEngine.Debug.Log("time2: " + (times[times.Count - 1] - times[times.Count - 2]));
  192. var spotArea = PixelSpotArea.Cluster(spotPoint, brightPoint, screenIdentification.Screen.UVInScreen);
  193. //times.Add(watch.ElapsedMilliseconds);
  194. //UnityEngine.Debug.Log("time3: " + (times[times.Count - 1] - times[times.Count - 2]));
  195. if (spotArea.Count == 0)
  196. return spotArea;
  197. // 亮度较低的部分合并到区域中
  198. for (int i = 0; i < brightPoint.Count; i++)
  199. {
  200. var p = brightPoint[i];
  201. var grid = PixelSpotArea.GetGrid1(p);
  202. for (int j = 0; j < spotArea.Count; j++)
  203. {
  204. var area = spotArea[j];
  205. if (area.Include1(grid))
  206. area.Add(p);
  207. }
  208. }
  209. //Debug.Log("Area Count" + spotArea.Count);
  210. //times.Add(watch.ElapsedMilliseconds);
  211. //UnityEngine.Debug.Log("time4: " + (times[times.Count - 1] - times[times.Count - 2]));
  212. return spotArea;
  213. //半径再按透视修正一遍,降低一点常规角度下反射的影响
  214. foreach (var i in spotArea)
  215. {
  216. var r0 = i.Radius / 2 / mCameraInfo.CurrentWidth;
  217. var center = i.Center;
  218. var offset1 = center + new Vector2(i.Radius / 2, 0);
  219. var offset2 = center - new Vector2(i.Radius / 2, 0);
  220. var offset3 = center + new Vector2(0, i.Radius / 2);
  221. var offset4 = center - new Vector2(0, i.Radius / 2);
  222. var transR = ((screenIdentification.Screen.TransformToScreen(offset1) - screenIdentification.Screen.TransformToScreen(offset2)).magnitude +
  223. (screenIdentification.Screen.TransformToScreen(offset3) - screenIdentification.Screen.TransformToScreen(offset4)).magnitude) / 4;
  224. var r1 = transR / screenIdentification.Screen.UVSize.x;
  225. //Debug.Log(r1 / r0);
  226. i.Radius *= (float)Math.Pow(r1 / r0, 2); // 摄像机位置不同参数也可能不同
  227. }
  228. //if (spotArea.Count == 1)
  229. //Debug.Log($"{spotArea[0].MaxRadius}");
  230. return spotArea;
  231. //// 排序亮区
  232. //spotArea.Sort((a, b) => b.MaxRadius.CompareTo(a.MaxRadius));
  233. ////var areas = new SortedList<float, PixelSpotArea>(new DescendingComparer<float>());
  234. ////foreach (var i in spotArea)
  235. //// areas.Add(i.MaxRadius, i);
  236. //var result = new Vector2[spotArea.Count];
  237. //foreach (var i in spotArea.Index())
  238. // result[i] = spotArea[i].Center;
  239. //times.Add(watch.ElapsedMilliseconds);
  240. //UnityEngine.Debug.Log("time6: " + (times[times.Count - 1] - times[times.Count - 2]));
  241. //return result;
  242. }
  243. return new List<PixelSpotArea>();
  244. }
  245. public List<Vector2> GetOld(int[] brightness) // 取整后的亮度图
  246. {
  247. return LocateOld(brightness, new Rect(0, 0, mCameraInfo.CurrentWidth, mCameraInfo.CurrentHeight));
  248. }
  249. public List<Vector2> LocateOld(int[] brightness, Rect rect)
  250. {
  251. var brightPixelDic = new Dictionary<float, List<Vector2>>();
  252. (int x, int y) origin = ((int)rect.min.x + 1, (int)rect.min.y + 1);
  253. Parallel.For(origin.x / samplingScale, (origin.x + (int)rect.width) / samplingScale, (i) =>
  254. {
  255. for (int j = origin.y / samplingScale; j < (origin.y + rect.height) / samplingScale; j++)
  256. {
  257. int ip = i * samplingScale;
  258. int jp = j * samplingScale;
  259. var index = mCameraInfo.CoordToIndex(ip, jp);
  260. //var b = brightness[index];
  261. //var b = (int)Math.Round(ConvBrightness(brightness, (ip, jp)));
  262. var b = ConvBrightness(brightness, (ip, jp), 3);
  263. if (b > 32)
  264. {
  265. lock (brightPixelDic)
  266. {
  267. if (brightPixelDic.TryGetValue(b, out List<Vector2> list))
  268. list.Add(new Vector2(ip, jp));
  269. else
  270. brightPixelDic.Add(b, new List<Vector2> { new Vector2(ip, jp) });
  271. }
  272. }
  273. }
  274. });
  275. //Parallel.For(0, pixels.Length / 7, (i) =>
  276. //{
  277. // i = i * 7;
  278. // var b = getBrightness(pixels[i]);
  279. // if (b >= maxBrightness)
  280. // {
  281. // lock (brightSpots)
  282. // {
  283. // if (b != maxBrightness)
  284. // brightSpots.Clear();
  285. // brightSpots.Add(indexToVector2(i));
  286. // }
  287. // maxBrightness = b;
  288. // }
  289. //});
  290. if (brightPixelDic.Count > 0)
  291. {
  292. // 取亮度最高的像素
  293. var keys = brightPixelDic.Keys.ToList();
  294. var maxIndex = o0.o0.MaxIndex(keys);
  295. //keys.Sort((a, b) => -a.CompareTo(b));
  296. var brightPixels = new List<Vector2>();
  297. for (int i = 0; i < Math.Min(1, keys.Count); i++)
  298. brightPixels.AddRange(brightPixelDic[keys[maxIndex]]);
  299. //Debug.Log("max brightness: " + keys[maxIndex]);
  300. //Debug.Log("brightPixels.Count: " + brightPixels.Count);
  301. //var testTexture = new Texture2D((int)_textureSize.x, (int)_textureSize.y);
  302. //testTexture.wrapMode = TextureWrapMode.Clamp;
  303. //testTexture.filterMode = FilterMode.Point;
  304. //foreach (var i in brightPixels)
  305. // testTexture.SetPixel((int)i.x, (int)i.y, Color.black);
  306. //testTexture.Apply();
  307. //testImage.texture = testTexture;
  308. var brightArea = new List<PixelCircleArea>() { new PixelCircleArea(brightPixels.First()) };
  309. for (int i = 1; i < brightPixels.Count; i++)
  310. {
  311. var p = brightPixels[i];
  312. var grid = PixelArea.GetGrid(p);
  313. var join = new SortedList<int, PixelCircleArea>();
  314. for (int j = 0; j < brightArea.Count; j++)
  315. {
  316. var area = brightArea[j];
  317. if (area.Include(grid))
  318. join.Add(j, area);
  319. }
  320. if (join.Count == 0)
  321. brightArea.Add(new PixelCircleArea(p));
  322. else if (join.Count == 1)
  323. join.First().Value.Add(p, grid);
  324. else
  325. {
  326. var combine = new PixelCircleArea(join.Values);
  327. combine.Add(p, grid);
  328. brightArea.Add(combine);
  329. for (int j = join.Count - 1; j >= 0; j--)
  330. brightArea.RemoveAt(join.ElementAt(j).Key);
  331. }
  332. //foreach (var j in brightArea)
  333. //{
  334. // var offset = (p - j.Center);
  335. // if (offset.magnitude < brightAreaRadius) // 距离近的并入该区域
  336. // {
  337. // j.Pixels.Add(p);
  338. // j.Center += offset / j.Pixels.Count;
  339. // join = true;
  340. // break;
  341. // }
  342. //}
  343. //if (!join)
  344. // brightArea.Add(new PixelArea(p));
  345. }
  346. //var sum = new Vector2(0, 0);
  347. //foreach (var i in brightPixels)
  348. //{
  349. // sum += i;
  350. //}
  351. //var middle = sum/brightPixels.Count;
  352. //Debug.Log("brightArea.Count: " + brightArea.Count);
  353. //if (brightArea.Count <= 1)
  354. // return brightArea.FirstOrDefault().Center;
  355. //PixelArea maxBrightArea = brightArea.First();
  356. //float maxCircleBrightness = 10000f;
  357. var CircleAreas = new List<PixelCircleArea>();
  358. for (int i = 0; i < brightArea.Count; i++)
  359. {
  360. var area = brightArea[i];
  361. var value = area.CircleBrightness(out float variance, brightness, ConvBrightness, 3);
  362. //Debug.Log(counter + "Variance: " + variance);
  363. //Debug.Log(counter + "CircleBrightness: " + value);
  364. //Debug.Log(counter + "CircleRadius: " + area.MaxRadius);
  365. if (variance < 30)
  366. {
  367. CircleAreas.Add(area);
  368. }
  369. //if (maxCircleBrightness == 10000f)
  370. //{
  371. // maxCircleBrightness = value;
  372. // maxBrightArea = area;
  373. //}
  374. //else if (maxCircleBrightness < value || (maxCircleBrightness == value && area.Pixels.Count > maxBrightArea.Pixels.Count))
  375. //{
  376. // maxCircleBrightness = value;
  377. // maxBrightArea = area;
  378. //}
  379. }
  380. //Debug.Log("CricleBrightness: " + maxCircleBrightness);
  381. //Debug.Log(counter + "CircleAreas: " + CircleAreas.Count);
  382. //counter++;
  383. if (CircleAreas.Count == 0)
  384. return null;
  385. if (CircleAreas.Count == 1)
  386. return new List<Vector2> { CircleAreas[0].Center };
  387. CircleAreas.Sort((a, b) => b.MaxRadius.CompareTo(a.MaxRadius));
  388. return new List<Vector2> { CircleAreas[0].Center, CircleAreas[1].Center };
  389. }
  390. return null;
  391. }
  392. InfraredSpot[] MatchInfraredRay(List<PixelSpotArea> spotArea)
  393. {
  394. var matchedArea = new Dictionary<InfraredMatch, PixelSpotArea>() { { InfraredMatch.Match0, null }, { InfraredMatch.Match1, null } };
  395. var v0 = InfraredSpots[0].Verify(spotArea, matchedArea);
  396. var v1 = InfraredSpots[1].Verify(spotArea, matchedArea);
  397. if (!Infrared12Overlap)
  398. {
  399. if (!v0 && !v1)
  400. {
  401. //Application.targetFrameRate = 1;
  402. //Debug.Log($"{Time.time}全失败 spotArea {spotArea.Count}");
  403. if (spotArea.Count == 0)
  404. {
  405. //InfraredSpots[0].UpdateByPredict();
  406. //InfraredSpots[1].UpdateByPredict();
  407. }
  408. else if (spotArea.Count == 1)
  409. {
  410. if (InfraredSpots[0].Predict != null && InfraredSpots[1].Predict == null)
  411. {
  412. matchedArea[InfraredMatch.Match0] = spotArea[0];
  413. //InfraredSpots[0].Update(spotArea[0]);
  414. //InfraredSpots[1].UpdateByPredict();
  415. }
  416. else if (InfraredSpots[1].Predict != null && InfraredSpots[0].Predict == null)
  417. {
  418. matchedArea[InfraredMatch.Match1] = spotArea[0];
  419. }
  420. else
  421. {
  422. if (spotArea[0].Radius > infraredSpotSettings.RadiusThreshold)
  423. {
  424. matchedArea[InfraredMatch.Match0] = spotArea[0];
  425. }
  426. else
  427. {
  428. matchedArea[InfraredMatch.Match1] = spotArea[0];
  429. }
  430. }
  431. }
  432. else
  433. {
  434. spotArea.Sort((a, b) => b.Radius.CompareTo(a.Radius)); // 排序亮区
  435. matchedArea[InfraredMatch.Match0] = spotArea[0];
  436. matchedArea[InfraredMatch.Match1] = spotArea[1];
  437. }
  438. }
  439. else
  440. {
  441. PixelSpotArea select = null;
  442. PixelSpotArea selectOther = null;
  443. InfraredMatch oneFailed = InfraredMatch.Nomatch;
  444. if (!v0)
  445. {
  446. select = matchedArea[InfraredMatch.Match1];
  447. //InfraredSpots[1].Update(select);
  448. oneFailed = InfraredMatch.Match0;
  449. }
  450. else if (!v1)
  451. {
  452. select = matchedArea[InfraredMatch.Match0];
  453. //InfraredSpots[0].Update(select);
  454. oneFailed = InfraredMatch.Match1;
  455. }
  456. if (oneFailed != InfraredMatch.Nomatch)
  457. {
  458. //Debug.LogWarning($"{Time.time}成功1个 {oneFailed}");
  459. spotArea.Sort((a, b) => b.Radius.CompareTo(a.Radius)); // 排序亮区
  460. foreach (var i in spotArea)
  461. {
  462. if (i != select)
  463. {
  464. selectOther = i;
  465. break;
  466. }
  467. }
  468. matchedArea[oneFailed] = selectOther;
  469. //if (selectOther == null)
  470. //{
  471. // GetSpot(oneFailed).UpdateByPredict();
  472. //}
  473. //else
  474. //{
  475. // GetSpot(oneFailed).Update(selectOther);
  476. //}
  477. }
  478. else
  479. {
  480. //Debug.LogWarning($"{Time.time}成功2个");
  481. if (matchedArea[InfraredMatch.Match0] == matchedArea[InfraredMatch.Match1])
  482. {
  483. // 重叠
  484. Infrared12Overlap = true;
  485. //if (spotArea.Count == 1)
  486. //{
  487. // Infrared12Overlap = true;
  488. // InfraredSpots[0].Update(spotArea[0]);
  489. // InfraredSpots[1].UpdateByPredict();
  490. // return InfraredSpots;
  491. //}
  492. //else
  493. //{
  494. // Infrared12Overlap = false;
  495. // spotArea.Sort((a, b) => b.Radius.CompareTo(a.Radius)); // 排序亮区
  496. // InfraredSpots[0].Update(spotArea[0]);
  497. // InfraredSpots[1].Update(spotArea[1]);
  498. //}
  499. }
  500. else //if (matchedArea[InfraredMatch.Match1] != matchedArea[InfraredMatch.Match2])
  501. {
  502. //Infrared12Overlap = false;
  503. //InfraredSpots[0].Update(matchedArea[InfraredMatch.Match0]);
  504. //InfraredSpots[1].Update(matchedArea[InfraredMatch.Match1]);
  505. // 每隔20帧更新阈值
  506. if (++cheakCounter >= CheakFrame)
  507. {
  508. //Debug.Log($"{Time.time}更新阈值2个");
  509. var middle = (matchedArea[InfraredMatch.Match0].Radius + matchedArea[InfraredMatch.Match1].Radius) / 2;
  510. infraredSpotSettings.UpdateThreshold(middle);
  511. cheakCounter = 0;
  512. if (matchedArea[InfraredMatch.Match0].Radius < middle)
  513. {
  514. //Debug.Log($"{Time.time}大小异常");
  515. InfraredSpots[0].Reset(); // 防止异常
  516. InfraredSpots[1].Reset();
  517. }
  518. }
  519. }
  520. }
  521. }
  522. }
  523. if (Infrared12Overlap)
  524. {
  525. if (v0)
  526. {
  527. var overlapAera = matchedArea[InfraredMatch.Match0];
  528. (PixelSpotArea, float) split = (null, float.MaxValue);
  529. foreach (var i in spotArea)
  530. {
  531. if (i != overlapAera)
  532. {
  533. var distance = (i.Center - overlapAera.Center).magnitude;
  534. if (distance < overlapAera.Radius * 2 && distance < split.Item2)
  535. {
  536. split = (i, distance);
  537. }
  538. }
  539. }
  540. if (split.Item1 != null)
  541. {
  542. //InfraredSpots[0].Update(overlapAera);
  543. //InfraredSpots[1].Update(split.Item1);
  544. matchedArea[InfraredMatch.Match1] = split.Item1;
  545. InfraredSpots[1].Disappear = false;
  546. Infrared12Overlap = false;
  547. }
  548. else
  549. {
  550. if ((InfraredSpots[1].Predict.Value - overlapAera.Center).magnitude > overlapAera.Radius / 2)
  551. InfraredSpots[1].Disappear = true;
  552. //InfraredSpots[0].Update(overlapAera);
  553. if (InfraredSpots[1].Predict == null || InfraredSpots[1].Disappear)
  554. {
  555. //InfraredSpots[1].Update(overlapAera);
  556. matchedArea[InfraredMatch.Match1] = overlapAera;
  557. InfraredSpots[1].Predict = null;
  558. }
  559. else
  560. {
  561. matchedArea[InfraredMatch.Match1] = null;
  562. //InfraredSpots[1].UpdateByPredict();
  563. }
  564. //if (++cheakCounter >= 20 * cheakFrame)
  565. //{
  566. // cheakCounter = 0;
  567. // InfraredSpots[1].Reset();
  568. // Infrared12Overlap = false;
  569. //}
  570. }
  571. }
  572. else
  573. {
  574. //InfraredSpots[0].UpdateByPredict();
  575. //InfraredSpots[1].UpdateByPredict();
  576. matchedArea[InfraredMatch.Match1] = null;
  577. }
  578. }
  579. foreach (var i in matchedArea)
  580. GetSpot(i.Key).Update(i.Value);
  581. return InfraredSpots;
  582. }
  583. // 亮度图是64位整形,返回卷积后的均值是float
  584. float ConvBrightness(int[] pixels, (int x, int y) point, int kernel_size = 3)
  585. {
  586. var sum = 0f;
  587. for (int i = point.x - kernel_size / 2; i <= point.x + kernel_size / 2; i++)
  588. {
  589. for (int j = point.y - kernel_size / 2; j <= point.y + kernel_size / 2; j++)
  590. {
  591. var index = mCameraInfo.CoordToIndex(i, j);
  592. if (index > 0 && index < pixels.Length) // 超出边缘不计算
  593. sum += pixels[index];
  594. }
  595. }
  596. return sum / (kernel_size * kernel_size);
  597. }
  598. //(int max, int second) MaxAreaIndex(Color[] pixels, List<PixelArea> list)
  599. //{
  600. // var maxValue = float.MinValue;
  601. // var secondValue = float.MinValue;
  602. // var max = 0;
  603. // var second = 0;
  604. // foreach (var i in list.Index())
  605. // {
  606. // var value = list[i].TotalBrightness(pixels, camera.Vector2ToIndex, samplingScale * 2);
  607. // if (value > maxValue)
  608. // {
  609. // maxValue = value;
  610. // max = i;
  611. // }
  612. // else if (maxValue > secondValue)
  613. // {
  614. // secondValue = value;
  615. // second = i;
  616. // }
  617. // }
  618. // return (max, second);
  619. //}
  620. //float CricleVariance(int[] pixels, PixelArea area, int kernel_size = 3)
  621. //{
  622. // var hash = new HashSet<(int, int)>();
  623. // foreach (var (cos,sin) in angleMathList)
  624. // {
  625. // var x = (int)Math.Round(area.Center.x + (area.MaxRadius + PixelArea.gridLength * 4) * cos);
  626. // var y = (int)Math.Round(area.Center.y + (area.MaxRadius + PixelArea.gridLength * 4) * sin);
  627. // hash.Add((x, y));
  628. // }
  629. // var avg = 0f;
  630. // foreach (var p in hash)
  631. // {
  632. // var value = ConvBrightness(pixels, p, kernel_size);
  633. // avg += value;
  634. // }
  635. // return avg / hash.Count;
  636. //}
  637. //class PixelArea
  638. //{
  639. // public static int gridLength;
  640. // public static (int x, int y) gridSize;
  641. // public static (int x, int y) GetGrid(Vector2 v)
  642. // {
  643. // var m = (int)(v.x / gridLength);
  644. // var n = (int)(v.y / gridLength);
  645. // return (m, n);
  646. // }
  647. // public Vector2 Center;
  648. // public List<Vector2> Pixels;
  649. // public PixelArea(Vector2 location)
  650. // {
  651. // this.Center = location;
  652. // this.Pixels = new List<Vector2> { location };
  653. // }
  654. //}
  655. }
  656. }