FingerSliceTrail.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. Slices.Capacity = 200;
  81. addSlice(slice);
  82. lineSegmentUpToDate = false;
  83. fittedDirectionUpToDate = false;
  84. }
  85. /// <summary>
  86. /// The slice at the given index.
  87. /// </summary>
  88. /// <param name="index">the index</param>
  89. /// <returns>the slice at the given index</returns>
  90. public FingerSlice this[int index] { get { return Slices[index]; } }
  91. /// <summary>
  92. /// Adds a slice to the end of the slice trail and outdates the lineSegment and fittedDirection.
  93. /// </summary>
  94. /// <param name="slice">the slice that should be added</param>
  95. public void addSlice(FingerSlice slice)
  96. {
  97. try
  98. {
  99. Slices.Add(slice);
  100. lineSegmentUpToDate = false;
  101. fittedDirectionUpToDate = false;
  102. }
  103. catch (OutOfMemoryException e) {
  104. // do nothing...
  105. }
  106. }
  107. /// <summary>
  108. /// Gets the contour of the finger with a given margin
  109. /// </summary>
  110. /// <param name="margin">the margin around the finger (in pixels)</param>
  111. /// <returns>the contour of the finger</returns>
  112. public Contour<Point> getContour(float margin)
  113. {
  114. List<Point> pointsA = new List<Point>();
  115. List<Point> pointsB = new List<Point>();
  116. foreach (FingerSlice slice in Slices)
  117. {
  118. Vector2D direction = slice.Direction;
  119. pointsA.Add(slice.Start + margin * direction.getInverse());
  120. pointsB.Add(slice.End + margin * direction);
  121. }
  122. pointsA.Reverse();
  123. pointsA.AddRange(pointsB);
  124. Contour<Point> contour = new Contour<Point>(new MemStorage());
  125. contour.PushMulti(pointsA.ToArray(), Emgu.CV.CvEnum.BACK_OR_FRONT.FRONT);
  126. return contour;
  127. }
  128. /// <summary>
  129. /// Calculates the direction of the last few slices.
  130. /// </summary>
  131. /// <returns>the end direction (direction towards hand)</returns>
  132. public Vector2D getEndDirection()
  133. {
  134. int innerEndIndex = Math.Max(0, NumSlices - Parameters.FingerNumSlicesForRelativeDirection);
  135. return (EndSlice.Mid - Slices[innerEndIndex].Mid).normalize();
  136. }
  137. /// <summary>
  138. /// Calculates the direction of the first few slices
  139. /// </summary>
  140. /// <returns>the start direction (pointing direction)</returns>
  141. public Vector2D getStartDirection()
  142. {
  143. int innerStartIndex = Math.Min(NumSlices - 1, Parameters.FingerNumSlicesForRelativeDirection);
  144. return (StartSlice.Mid - Slices[innerStartIndex].Mid).normalize();
  145. }
  146. /// <summary>
  147. /// Removes the first few slices.
  148. /// </summary>
  149. /// <param name="numSlices">the number of slices that should be removed</param>
  150. public void removeFirstSlices(int numSlices)
  151. {
  152. Slices.RemoveRange(0, numSlices);
  153. lineSegmentUpToDate = false;
  154. fittedDirectionUpToDate = false;
  155. }
  156. /// <summary>
  157. /// Reverses the trail and updates the line segment and the fitted direction
  158. /// </summary>
  159. public void reverse()
  160. {
  161. Slices.Reverse();
  162. if (lineSegmentUpToDate)
  163. lineSegment = new LineSegment2D(lineSegment.P2, lineSegment.P1);
  164. if (fittedDirectionUpToDate)
  165. fittedDirection = fittedDirection.getInverse();
  166. }
  167. /// <summary>
  168. /// updates the fitted direction (line fitting through all slices mid points)
  169. /// </summary>
  170. private void updateFittedDirection()
  171. {
  172. List<PointF> midPoints = new List<PointF>();
  173. foreach (FingerSlice slice in Slices)
  174. midPoints.Add(slice.Mid);
  175. PointF pointOnLine, direction;
  176. PointCollection.Line2DFitting(midPoints.ToArray(), Emgu.CV.CvEnum.DIST_TYPE.CV_DIST_FAIR, out direction, out pointOnLine);
  177. fittedDirection = new Vector2D(direction).normalize();
  178. if (fittedDirection.isInOppositeDirection(LineSegment.Direction))
  179. fittedDirection = fittedDirection.getInverse();
  180. fittedDirectionUpToDate = true;
  181. }
  182. /// <summary>
  183. /// updates the line segment (new line segment from end to start)
  184. /// </summary>
  185. private void updateLineSegment()
  186. {
  187. lineSegment = new LineSegment2D(EndSlice.Mid, StartSlice.Mid);
  188. lineSegmentUpToDate = true;
  189. }
  190. }
  191. }