TouchDetector.cs 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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.Detectors.FingerDetection;
  11. using bbiwarg.Detectors.PalmDetection;
  12. using bbiwarg.Utility;
  13. using bbiwarg.Graphics;
  14. namespace bbiwarg.Detectors.TouchDetection
  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.Tip;
  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.LineSegment.Line.Direction;
  37. Vector2D tep = (tipPoint + Constants.TouchEventTipCorrectionFactor * direction).moveInBound(0, 0, depthImage.Width - 1, depthImage.Height - 1);
  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. Int16 depthAtTouch = (Int16)(depthImage.getDepthAt(touchPoint) + Constants.TouchEventFingerDiameter);
  47. int minX = Math.Max(touchPoint.X - Constants.TouchEventAreaSize / 2, 0);
  48. int maxX = Math.Min(touchPoint.X + Constants.TouchEventAreaSize / 2, depthImage.Width-1);
  49. int minY = Math.Max(touchPoint.Y - Constants.TouchEventAreaSize / 2, 0);
  50. int maxY = Math.Min(touchPoint.Y + Constants.TouchEventAreaSize / 2, depthImage.Height-1);
  51. int matchedPixels = 0;
  52. int countedPixels = 0;
  53. for (int x = minX; x <= maxX; x++)
  54. {
  55. for (int y = minY; y <= maxY; y++)
  56. {
  57. Int16 depth = depthImage.getDepthAt(x, y);
  58. Color color = outputImage.getColotAt(x, y);
  59. Color subtractColor;
  60. if (depthAtTouch < depth && Math.Abs(depthAtTouch - depth) < Constants.TouchEventMaxDepthDifference)
  61. {
  62. matchedPixels++;
  63. subtractColor = Constants.TouchEventAreaMatchedSubtractColor;
  64. }
  65. else
  66. {
  67. subtractColor = Constants.TouchEventAreaNonMatchedSubtractColor;
  68. }
  69. 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));
  70. outputImage.drawPixel(x, y, newColor);
  71. countedPixels++;
  72. }
  73. }
  74. float rel = (float)matchedPixels / (float)countedPixels;
  75. outputImage.drawLineSegment(new Utility.LineSegment2D(new Vector2D(minX, maxY), new Vector2D(minX + rel * Constants.TouchEventAreaSize, maxY)), Constants.TouchEventStatusBarColor);
  76. outputImage.drawLineSegment(new Utility.LineSegment2D(new Vector2D(minX, maxY-1), new Vector2D(minX + rel * Constants.TouchEventAreaSize, maxY-1)), Constants.TouchEventStatusBarColor);
  77. return rel;
  78. }
  79. }
  80. }