TouchDetector.cs 4.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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, 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. outputImage.fillCircle(tipPoint.IntX, tipPoint.IntY, 3, Constants.TouchEventTipColor);
  32. float floodValue = getFloodValue(tipPoint);
  33. if (floodValue > Constants.TouchEventMinFloodValue)
  34. {
  35. //correct touchEvent position
  36. Vector2D direction = finger.Direction;
  37. Vector2D tep = (tipPoint + Constants.TouchEventTipCorrectionFactor * direction.getInverse()).moveInBound(Vector2D.Zero, depthImage.BottomRight, direction);
  38. outputImage.fillCircle(tep.IntX, tep.IntY, 5, Constants.TouchEventDetectedColor);
  39. TouchEvent touchEvent = new TouchEvent(tep, floodValue, finger);
  40. TouchEvents.Add(touchEvent);
  41. }
  42. }
  43. }
  44. private float getFloodValue(Point touchPoint)
  45. {
  46. int minX = Math.Max(touchPoint.X - 2*Constants.TouchEventAreaSize / 3, 0);
  47. int maxX = Math.Min(touchPoint.X + Constants.TouchEventAreaSize / 3, depthImage.Width-1);
  48. int minY = Math.Max(touchPoint.Y - 2*Constants.TouchEventAreaSize / 3, 0);
  49. int maxY = Math.Min(touchPoint.Y + Constants.TouchEventAreaSize / 3, depthImage.Height-1);
  50. Vector2D relTouch = new Vector2D(touchPoint.X - minX, touchPoint.Y - minY);
  51. Rectangle rect = new Rectangle(minX, minY, maxX - minX + 1, maxY - minY + 1);
  52. Image<Gray, byte> touchArea = depthImage.Image.Copy(rect);
  53. MCvConnectedComp comp = new MCvConnectedComp();
  54. CvInvoke.cvFloodFill(touchArea, relTouch, new MCvScalar(255), new MCvScalar(0), new MCvScalar(2), out comp, Emgu.CV.CvEnum.CONNECTIVITY.EIGHT_CONNECTED, Emgu.CV.CvEnum.FLOODFILL_FLAG.DEFAULT, IntPtr.Zero);
  55. int matchedPixels = 0;
  56. int countedPixels = 0;
  57. for (int x = minX; x <= maxX; x++)
  58. {
  59. for (int y = minY; y <= maxY; y++)
  60. {
  61. Color color = outputImage.getColotAt(x, y);
  62. byte depth = touchArea.Data[y - minY, x - minX, 0];
  63. Color subtractColor;
  64. if(depth == 255)
  65. {
  66. matchedPixels++;
  67. subtractColor = Constants.TouchEventAreaMatchedSubtractColor;
  68. }
  69. else
  70. {
  71. subtractColor = Constants.TouchEventAreaNonMatchedSubtractColor;
  72. }
  73. 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));
  74. outputImage.drawPixel(x, y, newColor);
  75. countedPixels++;
  76. }
  77. }
  78. float rel = (float)matchedPixels / (float)countedPixels;
  79. outputImage.drawLineSegment(new Utility.LineSegment2D(new Vector2D(minX, maxY), new Vector2D(minX + rel * Constants.TouchEventAreaSize, maxY)), Constants.TouchEventStatusBarColor);
  80. outputImage.drawLineSegment(new Utility.LineSegment2D(new Vector2D(minX, maxY-1), new Vector2D(minX + rel * Constants.TouchEventAreaSize, maxY-1)), Constants.TouchEventStatusBarColor);
  81. return rel;
  82. }
  83. }
  84. }