ConvexityDefect.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using BBIWARG.Recognition.FingerRecognition;
  2. using Emgu.CV.Structure;
  3. namespace BBIWARG.Utility
  4. {
  5. /// <summary>
  6. /// Stores data about and provides functions to work with convexity defects.
  7. /// </summary>
  8. public class ConvexityDefect
  9. {
  10. /// <summary>
  11. /// distance from OuterLineSegment to Inner
  12. /// </summary>
  13. public float Depth { get; private set; }
  14. /// <summary>
  15. /// the depth point
  16. /// </summary>
  17. public Vector2D Inner { get; private set; }
  18. /// <summary>
  19. /// line segment from start to end point
  20. /// </summary>
  21. public LineSegment2D OuterLineSegment { get; private set; }
  22. /// <summary>
  23. /// the point of start and end point which is farther away from the depth point
  24. /// </summary>
  25. public Vector2D OuterLong { get; private set; }
  26. /// <summary>
  27. /// the point of start and end point which is nearer to the depth point
  28. /// </summary>
  29. public Vector2D OuterShort { get; private set; }
  30. /// <summary>
  31. /// vector from Inner to OuterLong
  32. /// </summary>
  33. public Vector2D VectorLong { get; private set; }
  34. /// <summary>
  35. /// vector from Inner to OuterShort
  36. /// </summary>
  37. public Vector2D VectorShort { get; private set; }
  38. /// <summary>
  39. /// Constructs a ConvexityDefect.
  40. /// </summary>
  41. /// <param name="mcvConvexityDefect">the emgu convexity defect which has start, end and depth point</param>
  42. public ConvexityDefect(MCvConvexityDefect mcvConvexityDefect)
  43. {
  44. Inner = new Vector2D(mcvConvexityDefect.DepthPoint);
  45. Vector2D p1 = new Vector2D(mcvConvexityDefect.StartPoint);
  46. Vector2D p2 = new Vector2D(mcvConvexityDefect.EndPoint);
  47. if ((p1 - Inner).Length > (p2 - Inner).Length)
  48. {
  49. OuterLong = p1;
  50. OuterShort = p2;
  51. }
  52. else
  53. {
  54. OuterLong = p2;
  55. OuterShort = p1;
  56. }
  57. VectorLong = OuterLong - Inner;
  58. VectorShort = OuterShort - Inner;
  59. OuterLineSegment = new LineSegment2D(OuterLong, OuterShort);
  60. Depth = OuterLineSegment.getDistanceTo(Inner);
  61. }
  62. /// <summary>
  63. /// Tests if this defect is caused by a finger.
  64. /// </summary>
  65. /// <param name="finger">the finger maybe causing the defect</param>
  66. /// <returns>true iff this defect is caused by finger</returns>
  67. public bool isCausedByFinger(Finger finger)
  68. {
  69. return OuterLineSegment.intersectsWith(finger.LineSegment);
  70. }
  71. /// <summary>
  72. /// Tests if this defect could be the defect near the thumb.
  73. /// </summary>
  74. /// <param name="thumb">finger representing the thumb</param>
  75. /// <returns>true iff this defect is a possible thumb defect</returns>
  76. public bool isPossibleThumbDefect(Finger thumb)
  77. {
  78. float tipDistance = thumb.TipPoint.getDistanceTo(OuterShort);
  79. float handDistance = thumb.HandPoint.getDistanceTo(Inner);
  80. float thumbShortLengthRatio = thumb.LineSegment.Length / VectorShort.Length;
  81. float shortLongLengthRatio = VectorShort.Length / VectorLong.Length;
  82. return tipDistance <= Parameters.HandThumbDefectMaxDistanceToThumb &&
  83. handDistance <= Parameters.HandThumbDefectMaxDistanceToThumb &&
  84. thumbShortLengthRatio <= Parameters.HandThumbDefectMaxThumbShortLengthRatio && thumbShortLengthRatio >= Parameters.HandThumbDefectMinThumbShortLengthRatio &&
  85. shortLongLengthRatio <= Parameters.HandThumbDefectMaxShortLongLengthRatio && shortLongLengthRatio >= Parameters.HandThumbDefectMinShortLongLengthRatio;
  86. }
  87. }
  88. }