ConvexityDefect.cs 3.9 KB

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