TouchDetector.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. using bbiwarg.Recognition.FingerRecognition;
  11. using bbiwarg.Recognition.PalmRecognition;
  12. using bbiwarg.Utility;
  13. using bbiwarg.Graphics;
  14. namespace bbiwarg.Recognition.TouchRecognition
  15. {
  16. class TouchDetector
  17. {
  18. private DepthImage depthImage;
  19. private OutputImage outputImage;
  20. private List<Finger> fingers;
  21. public List<TouchEvent> TouchEvents { get; private set; }
  22. public TouchDetector(List<Finger> fingers, Quadrangle palmQuad, DepthImage depthImage, OutputImage outputImage)
  23. {
  24. this.depthImage = depthImage;
  25. this.outputImage = outputImage;
  26. this.fingers = fingers;
  27. this.TouchEvents = new List<TouchEvent>();
  28. foreach (Finger finger in fingers)
  29. {
  30. Vector2D tipPoint = finger.TipPoint;
  31. Vector2D direction = finger.TipDirection;
  32. Vector2D directionInv = direction.getInverse();
  33. Vector2D tipPointInside = (tipPoint + Constants.TouchEventTipInsideFactor * directionInv).moveInBound(Vector2D.Zero, depthImage.BottomRight, direction);
  34. Vector2D tipPointOutside = (tipPoint + Constants.TouchEventTipOutsideFactor * direction).moveInBound(Vector2D.Zero, depthImage.BottomRight, directionInv);
  35. Image<Gray, byte> touchMask = getTouchMask(tipPointInside);
  36. int touchPixels = touchMask.CountNonzero()[0];
  37. int numPixels = touchMask.Width * touchMask.Height;
  38. float touchValue = touchPixels / (float)numPixels;
  39. if (touchValue > Constants.TouchEventMinTouchValue)
  40. {
  41. if (palmQuad != null && palmQuad.isInside(tipPointOutside))
  42. {
  43. outputImage.fillCircle(tipPointOutside.IntX, tipPointOutside.IntY, 5, Constants.TouchEventDetectedColor);
  44. TouchEvent touchEvent = new PalmTouchEvent(tipPointOutside, touchMask, finger, palmQuad);
  45. TouchEvents.Add(touchEvent);
  46. finger.setTouchEvent(touchEvent);
  47. }
  48. }
  49. outputImage.fillCircle(tipPoint.IntX, tipPoint.IntY, 3, Constants.TouchEventTipColor);
  50. outputImage.drawText(tipPoint.IntX, tipPoint.IntY,String.Format("{0:0.00}", touchValue), Color.Red);
  51. }
  52. }
  53. private Image<Gray, byte> getTouchMask(Vector2D touchPoint)
  54. {
  55. int minX = Math.Max(touchPoint.IntX - 2 * Constants.TouchEventAreaSize / 3, 0);
  56. int maxX = Math.Min(touchPoint.IntX + Constants.TouchEventAreaSize / 3, depthImage.Width - 1);
  57. int minY = Math.Max(touchPoint.IntY - 2 * Constants.TouchEventAreaSize / 3, 0);
  58. int maxY = Math.Min(touchPoint.IntY + Constants.TouchEventAreaSize / 3, depthImage.Height - 1);
  59. Vector2D relTouchPoint = new Vector2D(touchPoint.IntX - minX, touchPoint.IntY - minY);
  60. Rectangle rect = new Rectangle(minX, minY, maxX - minX + 1, maxY - minY + 1);
  61. Image<Gray, byte> touchArea = depthImage.Image.Copy(rect);
  62. Image<Gray, byte> touchMask = new Image<Gray, byte>(rect.Width + 2, rect.Height + 2);
  63. MCvConnectedComp comp = new MCvConnectedComp();
  64. CvInvoke.cvFloodFill(touchArea, relTouchPoint, new MCvScalar(255), new MCvScalar(Constants.TouchEventFloodfillLowDiff), new MCvScalar(Constants.TouchEventFloodfillHighDiff), out comp, Emgu.CV.CvEnum.CONNECTIVITY.EIGHT_CONNECTED, Emgu.CV.CvEnum.FLOODFILL_FLAG.DEFAULT, touchMask);
  65. Rectangle cropRect = new Rectangle(1, 1, rect.Width, rect.Height);
  66. return touchMask.Copy(cropRect);
  67. }
  68. /*
  69. private float getTouchValue(Point touchPoint)
  70. {
  71. int floodValue = 255;
  72. int minX = Math.Max(touchPoint.X - 2*Constants.TouchEventAreaSize / 3, 0);
  73. int maxX = Math.Min(touchPoint.X + Constants.TouchEventAreaSize / 3, depthImage.Width-1);
  74. int minY = Math.Max(touchPoint.Y - 2*Constants.TouchEventAreaSize / 3, 0);
  75. int maxY = Math.Min(touchPoint.Y + Constants.TouchEventAreaSize / 3, depthImage.Height-1);
  76. Vector2D relTouch = new Vector2D(touchPoint.X - minX, touchPoint.Y - minY);
  77. Rectangle rect = new Rectangle(minX, minY, maxX - minX + 1, maxY - minY + 1);
  78. Image<Gray, byte> touchArea = depthImage.Image.Copy(rect);
  79. MCvConnectedComp comp = new MCvConnectedComp();
  80. CvInvoke.cvFloodFill(touchArea, relTouch, new MCvScalar(floodValue), new MCvScalar(Constants.TouchEventFloodfillLowDiff), new MCvScalar(Constants.TouchEventFloodfillHighDiff), out comp, Emgu.CV.CvEnum.CONNECTIVITY.EIGHT_CONNECTED, Emgu.CV.CvEnum.FLOODFILL_FLAG.DEFAULT, IntPtr.Zero);
  81. int matchedPixels = 0;
  82. int countedPixels = 0;
  83. for (int x = minX; x <= maxX; x++)
  84. {
  85. for (int y = minY; y <= maxY; y++)
  86. {
  87. Color color = outputImage.getColotAt(x, y);
  88. byte depth = touchArea.Data[y - minY, x - minX, 0];
  89. Color subtractColor;
  90. if(depth == floodValue)
  91. {
  92. matchedPixels++;
  93. subtractColor = Constants.TouchEventAreaMatchedSubtractColor;
  94. }
  95. else
  96. {
  97. subtractColor = Constants.TouchEventAreaNonMatchedSubtractColor;
  98. }
  99. Color newColor = Color.FromArgb(Math.Max(color.R - subtractColor.R, 0), Math.Max(color.G - subtractColor.G, 0), Math.Max(color.B - subtractColor.B, 0));
  100. outputImage.drawPixel(x, y, newColor);
  101. countedPixels++;
  102. }
  103. }
  104. float rel = (float)matchedPixels / (float)countedPixels;
  105. outputImage.drawLineSegment(new Utility.LineSegment2D(new Vector2D(minX, maxY), new Vector2D(minX + rel * Constants.TouchEventAreaSize, maxY)), Constants.TouchEventStatusBarColor, 2);
  106. return rel;
  107. }*/
  108. }
  109. }