Finger.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using bbiwarg.Images;
  8. using bbiwarg.Utility;
  9. using bbiwarg.Graphics;
  10. using bbiwarg.Recognition.HandRecognition;
  11. using bbiwarg.Recognition.TouchRecognition;
  12. using bbiwarg.Recognition.Tracking;
  13. using Emgu.CV;
  14. namespace bbiwarg.Recognition.FingerRecognition
  15. {
  16. class Finger : TrackableObject
  17. {
  18. public Vector2D TipPoint { get { return SliceTrail.StartSlice.Mid; } }
  19. public Vector2D HandPoint { get { return SliceTrail.EndSlice.Mid; } }
  20. public Vector2D MidPoint { get { return SliceTrail.MidSlice.Mid; } }
  21. public Vector2D Direction { get { return LineSegment.Direction; } }
  22. public Vector2D TipDirection { get { return SliceTrail.getStartDirection(); } }
  23. public Vector2D HandDirection { get { return SliceTrail.getEndDirection(); } }
  24. public LineSegment2D LineSegment { get { return SliceTrail.LineSegment; } }
  25. public FingerSliceTrail SliceTrail { get; private set; }
  26. public Contour<Point> Contour { get { return SliceTrail.getContour(); } }
  27. public Hand Hand { get; private set; }
  28. public TouchEvent TouchEvent { get; private set; }
  29. public Finger(FingerSliceTrail sliceTrail)
  30. : base()
  31. {
  32. SliceTrail = sliceTrail;
  33. }
  34. public override float getSimilarity(TrackableObject compareObject)
  35. {
  36. Finger compareFinger = (Finger)compareObject;
  37. LineSegment2D compareLineSegment = compareFinger.LineSegment;
  38. Line2D compareLine = compareLineSegment.Line;
  39. //tip position
  40. float tipPointDistance = TipPoint.getDistanceTo(compareFinger.TipPoint);
  41. float tipPointSimilarity = Math.Max(0, 1 - tipPointDistance / Constants.ImageDiagonalLength);
  42. //hand position
  43. float handPointDistance = HandPoint.getDistanceTo(compareFinger.HandPoint);
  44. float handPointSimilarity = Math.Max(0, 1 - handPointDistance / Constants.ImageDiagonalLength);
  45. /*
  46. //check angle
  47. float angle = LineSegment.Line.getAngleBetween(compareLine);
  48. float angleSimilarity = Math.Max(1 - angle / Constants.FingerSimilarityMaxAngle, 0);
  49. //check parallel distance
  50. float parallelDistance = LineSegment.getParallelDistanceTo(compareLineSegment);
  51. float parallelDistanceSimilarity = Math.Max(1 - parallelDistance / Constants.FingerSimilarityMaxParallelDistance, 0);
  52. //check vertical distance
  53. float verticalDistance = LineSegment.getVerticalDistanceTo(compareLineSegment);
  54. float verticalDistanceSimilarity = Math.Max(1 - verticalDistance / Constants.FingerSimilarityMaxVerticalDistance, 0);
  55. */
  56. float similarity = tipPointSimilarity * handPointSimilarity;
  57. //float similarity = tipPointSimilarity * handPointSimilarity * angleSimilarity * parallelDistanceSimilarity * verticalDistanceSimilarity;
  58. //float similarity = (tipPointSimilarity + handPointSimilarity + angleSimilarity + parallelDistanceSimilarity + verticalDistanceSimilarity)/5;
  59. //float similarity = (angleSimilarity + parallelDistanceSimilarity + verticalDistanceSimilarity) / 3;
  60. return similarity;
  61. }
  62. public void setHand(Hand hand) {
  63. Hand = hand;
  64. }
  65. public void setTouchEvent(TouchEvent touchEvent) {
  66. TouchEvent = touchEvent;
  67. }
  68. public void draw(OutputImage outputImage, bool tracked)
  69. {
  70. if (tracked)
  71. {
  72. outputImage.drawLineSegment(LineSegment, Constants.FingerTrackedColor);
  73. }
  74. else
  75. {
  76. for (int i = 0; i < SliceTrail.NumSlices; i++)
  77. {
  78. outputImage.drawLineSegment(SliceTrail[i].LineSegment, Constants.FingerSliceColor);
  79. }
  80. outputImage.drawLineSegment(LineSegment, Constants.FingerDetectedColor);
  81. outputImage.drawContour(Contour, Constants.FingerContourColor, 1);
  82. }
  83. }
  84. }
  85. }