FingerSliceTrail.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using bbiwarg.Utility;
  8. using Emgu.CV;
  9. namespace bbiwarg.Detectors.FingerDetection
  10. {
  11. class FingerSliceTrail
  12. {
  13. private List<FingerSlice> slices;
  14. private List<Point> pointsA;
  15. private List<Point> pointsB;
  16. public FingerSlice StartSlice { get { return slices[0]; } }
  17. public FingerSlice MidSlice { get { return slices[NumSlices / 2]; } }
  18. public FingerSlice EndSlice { get { return slices[slices.Count - 1]; } }
  19. public FingerSlice this[int index] { get { return slices[index]; } }
  20. public int NumSlices { get { return slices.Count; } }
  21. public LineSegment2D LineSegment { get; private set; }
  22. public FingerSliceTrail(FingerSlice slice)
  23. {
  24. slices = new List<FingerSlice>();
  25. pointsA = new List<Point>();
  26. pointsB = new List<Point>();
  27. addSlice(slice);
  28. }
  29. public void addSlice(FingerSlice slice)
  30. {
  31. slices.Add(slice);
  32. pointsA.Add(slice.Start - Constants.FingerContourMargin * slice.Direction);
  33. pointsB.Add(slice.End + Constants.FingerContourMargin * slice.Direction);
  34. pointsA.Add(slice.Start);
  35. LineSegment = new LineSegment2D(StartSlice.Mid, slice.Mid);
  36. }
  37. public Vector2D getStartDirection()
  38. {
  39. int innerStartIndex = Math.Min(NumSlices, Constants.FingerNumSlicesForRelativeDirection);
  40. return (EndSlice.Mid - slices[innerStartIndex].Mid).normalize();
  41. }
  42. public Vector2D getEndDirection()
  43. {
  44. int innerEndIndex = Math.Max(0, NumSlices - Constants.FingerNumSlicesForRelativeDirection);
  45. return (EndSlice.Mid - slices[innerEndIndex].Mid).normalize();
  46. }
  47. public void removeFirstSlices(int numSlices) {
  48. slices.RemoveRange(0, numSlices);
  49. LineSegment = new LineSegment2D(StartSlice.Mid, EndSlice.Mid);
  50. }
  51. public void reverse() {
  52. slices.Reverse();
  53. pointsA.Reverse();
  54. pointsB.Reverse();
  55. LineSegment = new LineSegment2D(StartSlice.Mid, EndSlice.Mid);
  56. }
  57. public Contour<Point> getContour()
  58. {
  59. pointsA.Reverse();
  60. List<Point> points = new List<Point>();
  61. points.AddRange(pointsA);
  62. points.AddRange(pointsB);
  63. pointsA.Reverse();
  64. Contour<Point> contour = new Contour<Point>(new MemStorage());
  65. foreach (Point p in points)
  66. {
  67. contour.Push(p);
  68. }
  69. return contour;
  70. }
  71. }
  72. }