TouchDetector.cs 3.4 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.Fingers;
  11. using bbiwarg.Detectors.Palm;
  12. using bbiwarg.Utility;
  13. namespace bbiwarg.Detectors.Touch
  14. {
  15. class TouchDetector
  16. {
  17. private DepthImage depthImage;
  18. private TouchImage touchImage;
  19. private List<Finger> fingers;
  20. public List<TouchEvent> TouchEvents { get; private set; }
  21. public TouchDetector(List<Finger> fingers, DepthImage depthImage, TouchImage touchImage)
  22. {
  23. this.depthImage = depthImage;
  24. this.touchImage = touchImage;
  25. this.fingers = fingers;
  26. this.TouchEvents = new List<TouchEvent>();
  27. float floodValueThreshold = 0.5f;
  28. foreach (Finger finger in fingers)
  29. {
  30. Vector2D tipPoint = finger.Tip;
  31. float floodValue = getFloodValue(tipPoint);
  32. if (floodValue > floodValueThreshold)
  33. {
  34. //correct touchEvent position
  35. Vector2D direction = finger.LineSegment.Line.Direction;
  36. float directionFactor = 10;
  37. float x = HelperFunctions.thresholdRange<float>(0, depthImage.Width - 1, tipPoint.X + directionFactor * direction.X);
  38. float y = HelperFunctions.thresholdRange<float>(0, depthImage.Height - 1, tipPoint.Y + directionFactor * direction.Y);
  39. Vector2D tep = new Vector2D(x, y);
  40. touchImage.setStateAt(tep, TouchImageState.touchDetected);
  41. TouchEvent touchEvent = new TouchEvent(tep, floodValue, finger);
  42. TouchEvents.Add(touchEvent);
  43. }
  44. }
  45. }
  46. private float getFloodValue(Point touchPoint)
  47. {
  48. int searchSize = 15;
  49. int maxDepthDifference = 20;
  50. Int16 fingerDiameter = 5;
  51. Int16 depthAtTouch = (Int16)(depthImage.getDepthAt(touchPoint) + fingerDiameter);
  52. int minX = Math.Max(touchPoint.X - searchSize, 0);
  53. int maxX = Math.Min(touchPoint.X + searchSize, depthImage.Width);
  54. int minY = Math.Max(touchPoint.Y - searchSize, 0);
  55. int maxY = Math.Min(touchPoint.Y + searchSize, depthImage.Height);
  56. int matchedPixels = 0;
  57. int countedPixels = 0;
  58. for (int x = minX; x < maxX; x++)
  59. {
  60. for (int y = minY; y < maxY; y++)
  61. {
  62. Int16 depth = depthImage.getDepthAt(x, y);
  63. touchImage.setStateAt(x, y, TouchImageState.touchArea);
  64. if (Math.Abs(depthAtTouch - depth) < maxDepthDifference)
  65. {
  66. matchedPixels++;
  67. touchImage.setStateAt(x, y, TouchImageState.touchAreaMatched);
  68. }
  69. countedPixels++;
  70. }
  71. }
  72. float rel = (float)matchedPixels / (float)countedPixels;
  73. //status bar (% of matched pixels) -> green
  74. for (int x = minX; x < minX + (maxX - minX) * rel; x++)
  75. {
  76. touchImage.setStateAt(x, maxY - 1, TouchImageState.touchAreaStatusBar);
  77. }
  78. return rel;
  79. }
  80. }
  81. }