Finger.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 FingerPoint nearest;
  16. private FingerPoint farthest;
  17. private PointF direction;
  18. private PointF pointOnLine;
  19. private PointF start;
  20. private PointF end;
  21. private bool startEndUpToDate;
  22. public Finger(FingerPoint fingerPoint)
  23. {
  24. fingerPoints = new List<FingerPoint>();
  25. nearest = fingerPoint;
  26. farthest = fingerPoint;
  27. startEndUpToDate = false;
  28. addFingerPoint(fingerPoint);
  29. }
  30. public FingerPoint getNearest()
  31. {
  32. return nearest;
  33. }
  34. public FingerPoint getFarthest()
  35. {
  36. return farthest;
  37. }
  38. public PointF getStart()
  39. {
  40. if (!startEndUpToDate) updateStartEnd();
  41. return start;
  42. }
  43. public PointF getEnd()
  44. {
  45. if (!startEndUpToDate) updateStartEnd();
  46. return end;
  47. }
  48. public void addFingerPoint(FingerPoint fingerPoint)
  49. {
  50. fingerPoints.Add(fingerPoint);
  51. //update nearest
  52. if (fingerPoint.getDepth() < nearest.getDepth()) nearest = fingerPoint;
  53. //update farthest
  54. if (fingerPoint.getDepth() > farthest.getDepth()) farthest = fingerPoint;
  55. startEndUpToDate = 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. public float getLength()
  71. {
  72. FingerPoint fp1 = getNearest();
  73. FingerPoint fp2 = getFarthest();
  74. float distance = fp1.getDistanceTo(fp2);
  75. return distance;
  76. }
  77. private PointF projectToLine(PointF p)
  78. {
  79. float px = p.X, py = p.Y, dx = direction.X, dy = direction.Y, ox = pointOnLine.X, oy = pointOnLine.Y;
  80. float diffx = px - ox;
  81. float diffy = py - oy;
  82. float diff_d = (diffx * dx + diffy * dy);
  83. float d_d = (dx * dx + dy * dy);
  84. float q = diff_d / d_d;
  85. return new PointF(ox + q * dx, oy + q * dy);
  86. }
  87. private void updateStartEnd() {
  88. //update direction+pointonline
  89. PointF[] pointArray = new PointF[fingerPoints.Count];
  90. int i = 0;
  91. foreach (FingerPoint fp in fingerPoints)
  92. {
  93. pointArray[i] = new PointF(fp.getX(), fp.getY());
  94. ++i;
  95. }
  96. PointCollection.Line2DFitting(pointArray, Emgu.CV.CvEnum.DIST_TYPE.CV_DIST_L2, out direction, out pointOnLine);
  97. //update start+end
  98. start = projectToLine(new PointF(farthest.getX(), farthest.getY()));
  99. end = projectToLine(new PointF(nearest.getX(), nearest.getY()));
  100. startEndUpToDate = true;
  101. }
  102. }
  103. }