FingerSliceTrail.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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.Recognition.FingerRecognition
  10. {
  11. /// <summary>
  12. /// Each Finger consists of multiple FingerSlices, a FingerSliceTrail is a collection of these slices.
  13. /// </summary>
  14. public class FingerSliceTrail
  15. {
  16. /// <summary>
  17. /// the line segment connection the first slice's mid to the last slice's mid
  18. /// </summary>
  19. private LineSegment2D lineSegment;
  20. /// <summary>
  21. /// the fitted direction through all slice's mid points [end to start]
  22. /// </summary>
  23. private Vector2D fittedDirection;
  24. /// <summary>
  25. /// indicates wether the variable lineSegment is up to date (outdates if new start or end slices are added)
  26. /// </summary>
  27. private bool lineSegmentUpToDate;
  28. /// <summary>
  29. /// indicates weter the variable fittedDirection is up to date (outdates if new slices are added)
  30. /// </summary>
  31. private bool fittedDirectionUpToDate;
  32. /// <summary>
  33. /// the finger slices
  34. /// </summary>
  35. public List<FingerSlice> Slices { get; private set; }
  36. /// <summary>
  37. /// the first slice
  38. /// </summary>
  39. public FingerSlice StartSlice { get { return Slices[0]; } }
  40. /// <summary>
  41. /// the middle slice
  42. /// </summary>
  43. public FingerSlice MidSlice { get { return Slices[NumSlices / 2]; } }
  44. /// <summary>
  45. /// the last slice
  46. /// </summary>
  47. public FingerSlice EndSlice { get { return Slices[Slices.Count - 1]; } }
  48. /// <summary>
  49. /// The slice at the given index.
  50. /// </summary>
  51. /// <param name="index">the index</param>
  52. /// <returns>the slice at the given index</returns>
  53. public FingerSlice this[int index] { get { return Slices[index]; } }
  54. /// <summary>
  55. /// the number of slices
  56. /// </summary>
  57. public int NumSlices { get { return Slices.Count; } }
  58. /// <summary>
  59. /// the line segment connecting the start slice's mid to the end slice's mid
  60. /// </summary>
  61. public LineSegment2D LineSegment { get { if (!lineSegmentUpToDate) updateLineSegment(); return lineSegment; } }
  62. /// <summary>
  63. /// the fitted direction through all slice's mid points [end to start]
  64. /// </summary>
  65. public Vector2D FittedDirection { get { if (!fittedDirectionUpToDate) updateFittedDirection(); return fittedDirection; } }
  66. /// <summary>
  67. /// Initializes a new instance of the FingerSliceTrail class.
  68. /// </summary>
  69. /// <param name="slice">The initial slice.</param>
  70. public FingerSliceTrail(FingerSlice slice)
  71. {
  72. Slices = new List<FingerSlice>();
  73. addSlice(slice);
  74. lineSegmentUpToDate = false;
  75. fittedDirectionUpToDate = false;
  76. }
  77. /// <summary>
  78. /// Adds a slice to the end of the slice trail and outdates the lineSegment and fittedDirection.
  79. /// </summary>
  80. /// <param name="slice">the slice that should be added</param>
  81. public void addSlice(FingerSlice slice)
  82. {
  83. Slices.Add(slice);
  84. lineSegmentUpToDate = false;
  85. fittedDirectionUpToDate = false;
  86. }
  87. /// <summary>
  88. /// Calculates the direction of the first few slices
  89. /// </summary>
  90. /// <returns>the start direction (pointing direction)</returns>
  91. public Vector2D getStartDirection()
  92. {
  93. int innerStartIndex = Math.Min(NumSlices - 1, Parameters.FingerNumSlicesForRelativeDirection);
  94. return (StartSlice.Mid - Slices[innerStartIndex].Mid).normalize();
  95. }
  96. /// <summary>
  97. /// Calculates the direction of the last few slices.
  98. /// </summary>
  99. /// <returns>the end direction (direction towards hand)</returns>
  100. public Vector2D getEndDirection()
  101. {
  102. int innerEndIndex = Math.Max(0, NumSlices - Parameters.FingerNumSlicesForRelativeDirection);
  103. return (EndSlice.Mid - Slices[innerEndIndex].Mid).normalize();
  104. }
  105. /// <summary>
  106. /// Removes the first few slices.
  107. /// </summary>
  108. /// <param name="numSlices">the number of slices that should be removed</param>
  109. public void removeFirstSlices(int numSlices)
  110. {
  111. Slices.RemoveRange(0, numSlices);
  112. lineSegmentUpToDate = false;
  113. fittedDirectionUpToDate = false;
  114. }
  115. /// <summary>
  116. /// Reverses the trail and updates the line segment and the fitted direction
  117. /// </summary>
  118. public void reverse()
  119. {
  120. Slices.Reverse();
  121. if (lineSegmentUpToDate)
  122. lineSegment = new LineSegment2D(lineSegment.P2, lineSegment.P1);
  123. if (fittedDirectionUpToDate)
  124. fittedDirection = fittedDirection.getInverse();
  125. }
  126. /// <summary>
  127. /// Gets the contour of the finger with a given margin
  128. /// </summary>
  129. /// <param name="margin">the margin around the finger (in pixels)</param>
  130. /// <returns>the contour of the finger</returns>
  131. public Contour<Point> getContour(float margin)
  132. {
  133. List<Point> pointsA = new List<Point>();
  134. List<Point> pointsB = new List<Point>();
  135. foreach (FingerSlice slice in Slices)
  136. {
  137. Vector2D direction = slice.Direction;
  138. pointsA.Add(slice.Start + margin * direction.getInverse());
  139. pointsB.Add(slice.End + margin * direction);
  140. }
  141. pointsA.Reverse();
  142. pointsA.AddRange(pointsB);
  143. Contour<Point> contour = new Contour<Point>(new MemStorage());
  144. contour.PushMulti(pointsA.ToArray(), Emgu.CV.CvEnum.BACK_OR_FRONT.FRONT);
  145. return contour;
  146. }
  147. /// <summary>
  148. /// updtes the line segment (new line segment from end to start)
  149. /// </summary>
  150. private void updateLineSegment()
  151. {
  152. lineSegment = new LineSegment2D(EndSlice.Mid, StartSlice.Mid);
  153. lineSegmentUpToDate = true;
  154. }
  155. /// <summary>
  156. /// updates the fitted direction (line fitting through all slices mid points)
  157. /// </summary>
  158. private void updateFittedDirection()
  159. {
  160. List<PointF> midPoints = new List<PointF>();
  161. foreach (FingerSlice slice in Slices)
  162. midPoints.Add(slice.Mid);
  163. PointF pointOnLine, direction;
  164. PointCollection.Line2DFitting(midPoints.ToArray(), Emgu.CV.CvEnum.DIST_TYPE.CV_DIST_FAIR, out direction, out pointOnLine);
  165. fittedDirection = new Vector2D(direction).normalize();
  166. if (fittedDirection.isInOppositeDirection(LineSegment.Direction))
  167. fittedDirection = fittedDirection.getInverse();
  168. fittedDirectionUpToDate = true;
  169. }
  170. }
  171. }