FingerSliceTrail.cs 7.2 KB

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