FingerSliceTrail.cs 7.0 KB

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