ConvexityDefect.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. public class ConvexityDefect
  11. {
  12. public Vector2D OuterShort { get; private set; }
  13. public Vector2D OuterLong { get; private set; }
  14. public Vector2D Inner { get; private set; }
  15. public Vector2D VectorShort { get; private set; }
  16. public Vector2D VectorLong { get; private set; }
  17. public LineSegment2D OuterLineSegment { get; private set; }
  18. public float Depth { get; private set; }
  19. public ConvexityDefect(MCvConvexityDefect mcvCOnvexityDefect) {
  20. Inner = new Vector2D(mcvCOnvexityDefect.DepthPoint);
  21. Vector2D p1 = new Vector2D(mcvCOnvexityDefect.StartPoint);
  22. Vector2D p2 = new Vector2D(mcvCOnvexityDefect.EndPoint);
  23. if ((p1 - Inner).Length > (p2 - Inner).Length)
  24. {
  25. OuterLong = p1;
  26. OuterShort = p2;
  27. }
  28. else {
  29. OuterLong = p2;
  30. OuterShort = p1;
  31. }
  32. VectorLong = OuterLong - Inner;
  33. VectorShort = OuterShort - Inner;
  34. OuterLineSegment = new LineSegment2D(OuterLong, OuterShort);
  35. Depth = OuterLineSegment.getDistanceTo(Inner);
  36. }
  37. public bool isPossibleThumbDefect(Finger thumb) {
  38. float tipDistance = thumb.TipPoint.getDistanceTo(OuterShort);
  39. float handDistance = thumb.HandPoint.getDistanceTo(Inner);
  40. float thumbShortLengthRatio = thumb.LineSegment.Length / VectorShort.Length;
  41. float shortLongLengthRatio = VectorShort.Length / VectorLong.Length;
  42. return (tipDistance <= Parameters.HandThumbDefectMaxDistanceToThumb &&
  43. handDistance <= Parameters.HandThumbDefectMaxDistanceToThumb &&
  44. thumbShortLengthRatio <= Parameters.HandThumbDefectMaxThumbShortLengthRatio && thumbShortLengthRatio >= Parameters.HandThumbDefectMinThumbShortLengthRatio &&
  45. shortLongLengthRatio <= Parameters.HandThumbDefectMaxShortLongLengthRatio && shortLongLengthRatio >= Parameters.HandThumbDefectMinShortLongLengthRatio);
  46. }
  47. public bool isCausedByFinger(Finger finger) {
  48. return (OuterLineSegment.intersectsWith(finger.LineSegment));
  49. }
  50. }
  51. }