Finger.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 Emgu.CV;
  8. using Emgu.CV.Structure;
  9. using bbiwarg.Images;
  10. namespace bbiwarg.Detectors.Fingers
  11. {
  12. class Finger
  13. {
  14. private List<FingerPoint> fingerPoints;
  15. private bool lineUpToDate = false;
  16. private PointF direction;
  17. private PointF pointOnLine;
  18. private PointF lineEndPoint1;
  19. private PointF lineEndPoint2;
  20. private FingerPoint tipPoint;
  21. private FingerPoint handPoint;
  22. private float length;
  23. public Finger(FingerPoint fingerPoint)
  24. {
  25. fingerPoints = new List<FingerPoint>();
  26. addFingerPoint(fingerPoint);
  27. }
  28. public PointF getLineEndPoint1()
  29. {
  30. if (!lineUpToDate) updateLine();
  31. return lineEndPoint1;
  32. }
  33. public PointF getLineEndPoint2()
  34. {
  35. if (!lineUpToDate) updateLine();
  36. return lineEndPoint2;
  37. }
  38. public FingerPoint getTipPoint()
  39. {
  40. if (!lineUpToDate) updateLine();
  41. return tipPoint;
  42. }
  43. public PointF getDirection() {
  44. if (!lineUpToDate) updateLine();
  45. return direction;
  46. }
  47. public float getLength()
  48. {
  49. if (!lineUpToDate) updateLine();
  50. return length;
  51. }
  52. public void addFingerPoint(FingerPoint fingerPoint)
  53. {
  54. fingerPoints.Add(fingerPoint);
  55. lineUpToDate = false;
  56. }
  57. public float getMinDistance(FingerPoint fingerPoint)
  58. {
  59. float minDinstance = float.MaxValue;
  60. foreach (FingerPoint fp in fingerPoints)
  61. {
  62. float distance = fp.getDistanceTo(fingerPoint);
  63. if (distance < minDinstance)
  64. {
  65. minDinstance = distance;
  66. }
  67. }
  68. return minDinstance;
  69. }
  70. private void updateLine() {
  71. //update direction+pointonline
  72. PointF[] pointArray = new PointF[fingerPoints.Count];
  73. int i = 0;
  74. foreach (FingerPoint fp in fingerPoints)
  75. {
  76. pointArray[i] = new PointF(fp.getX(), fp.getY());
  77. ++i;
  78. }
  79. PointCollection.Line2DFitting(pointArray, Emgu.CV.CvEnum.DIST_TYPE.CV_DIST_L2, out direction, out pointOnLine);
  80. FingerPoint fp1 = fingerPoints[0];
  81. FingerPoint fp2 = fingerPoints[0];
  82. length = 0.0f;
  83. foreach (FingerPoint fp in fingerPoints) {
  84. float distanceToFP1 = fp.getDistanceTo(fp1);
  85. float distanceToFP2 = fp.getDistanceTo(fp2);
  86. if (length < distanceToFP1 && distanceToFP1 >= distanceToFP2)
  87. {
  88. fp2 = fp;
  89. length = distanceToFP1;
  90. }
  91. else if (length < distanceToFP2 && distanceToFP2 > distanceToFP1)
  92. {
  93. fp1 = fp;
  94. length = distanceToFP2;
  95. }
  96. }
  97. if (fp1.getY() < fp2.getY())
  98. {
  99. tipPoint = fp1;
  100. handPoint = fp2;
  101. }
  102. else
  103. {
  104. tipPoint = fp2;
  105. handPoint = fp1;
  106. }
  107. //update start+end
  108. lineEndPoint1 = projectToLine(new PointF(tipPoint.getX(), tipPoint.getY()));
  109. lineEndPoint2 = projectToLine(new PointF(handPoint.getX(), handPoint.getY()));
  110. lineUpToDate = true;
  111. }
  112. private PointF projectToLine(PointF p)
  113. {
  114. float px = p.X, py = p.Y, dx = direction.X, dy = direction.Y, ox = pointOnLine.X, oy = pointOnLine.Y;
  115. float diffx = px - ox;
  116. float diffy = py - oy;
  117. float diff_d = (diffx * dx + diffy * dy);
  118. float d_d = (dx * dx + dy * dy);
  119. float q = diff_d / d_d;
  120. return new PointF(ox + q * dx, oy + q * dy);
  121. }
  122. }
  123. }