ConvexityDefect.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. Inner = new Vector2D(mcvConvexityDefect.DepthPoint);
  49. Vector2D p1 = new Vector2D(mcvConvexityDefect.StartPoint);
  50. Vector2D p2 = new Vector2D(mcvConvexityDefect.EndPoint);
  51. if ((p1 - Inner).Length > (p2 - Inner).Length)
  52. {
  53. OuterLong = p1;
  54. OuterShort = p2;
  55. }
  56. else {
  57. OuterLong = p2;
  58. OuterShort = p1;
  59. }
  60. VectorLong = OuterLong - Inner;
  61. VectorShort = OuterShort - Inner;
  62. OuterLineSegment = new LineSegment2D(OuterLong, OuterShort);
  63. Depth = OuterLineSegment.getDistanceTo(Inner);
  64. }
  65. /// <summary>
  66. /// Tests if this defect could be the defect near the thumb.
  67. /// </summary>
  68. /// <param name="thumb">finger representing the thumb</param>
  69. /// <returns>true iff this defect is a possible thumb defect</returns>
  70. public bool isPossibleThumbDefect(Finger thumb) {
  71. float tipDistance = thumb.TipPoint.getDistanceTo(OuterShort);
  72. float handDistance = thumb.HandPoint.getDistanceTo(Inner);
  73. float thumbShortLengthRatio = thumb.LineSegment.Length / VectorShort.Length;
  74. float shortLongLengthRatio = VectorShort.Length / VectorLong.Length;
  75. return (tipDistance <= Parameters.HandThumbDefectMaxDistanceToThumb &&
  76. handDistance <= Parameters.HandThumbDefectMaxDistanceToThumb &&
  77. thumbShortLengthRatio <= Parameters.HandThumbDefectMaxThumbShortLengthRatio && thumbShortLengthRatio >= Parameters.HandThumbDefectMinThumbShortLengthRatio &&
  78. shortLongLengthRatio <= Parameters.HandThumbDefectMaxShortLongLengthRatio && shortLongLengthRatio >= Parameters.HandThumbDefectMinShortLongLengthRatio);
  79. }
  80. /// <summary>
  81. /// Tests if this defect is caused by a finger.
  82. /// </summary>
  83. /// <param name="finger">the finger maybe causing the defect</param>
  84. /// <returns>true iff this defect is caused by finger</returns>
  85. public bool isCausedByFinger(Finger finger) {
  86. return (OuterLineSegment.intersectsWith(finger.LineSegment));
  87. }
  88. }
  89. }