TouchDetector.cs 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. private List<TouchEvent> touchEvents;
  21. public TouchDetector(List<Finger> fingers, DepthImage depthImage, TouchImage touchImage) {
  22. this.depthImage = depthImage;
  23. this.touchImage = touchImage;
  24. this.fingers = fingers;
  25. this.touchEvents = new List<TouchEvent>();
  26. float floodValueThreshold = 0.5f;
  27. foreach (Finger finger in fingers) {
  28. Vector2D tipPoint = finger.Tip;
  29. float floodValue = getFloodValue((int)tipPoint.X, (int)tipPoint.Y);
  30. if (floodValue > floodValueThreshold)
  31. {
  32. //correct touchEvent position
  33. Vector2D direction = finger.Line.Direction;
  34. float directionFactor = 10;
  35. float x = HelperFunctions.thresholdRange<float>(0, depthImage.getWidth() - 1, tipPoint.X + directionFactor * direction.X);
  36. float y = HelperFunctions.thresholdRange<float>(0, depthImage.getHeight() - 1, tipPoint.Y + directionFactor * direction.Y);
  37. Vector2D tep = new Vector2D(x,y);
  38. touchImage.setTouchAt((int)tep.X, (int)tep.Y, TouchImageState.touchDetected);
  39. TouchEvent touchEvent = new TouchEvent(tep, floodValue, finger);
  40. touchEvents.Add(touchEvent);
  41. }
  42. }
  43. }
  44. public List<TouchEvent> getTouchEvents() {
  45. return touchEvents;
  46. }
  47. private float getFloodValue(int touchX, int touchY) {
  48. int searchSize = 15;
  49. int maxDepthDifference = 20;
  50. Int16 fingerDiameter = 5;
  51. Int16 depthAtTouch = (Int16) (depthImage.getDepthAt(touchX, touchY) + fingerDiameter);
  52. int minX = Math.Max(touchX - searchSize, 0);
  53. int maxX = Math.Min(touchX + searchSize, depthImage.getWidth());
  54. int minY = Math.Max(touchY - searchSize, 0);
  55. int maxY = Math.Min(touchY + searchSize, depthImage.getHeight());
  56. int matchedPixels = 0;
  57. int countedPixels = 0;
  58. for (int x = minX; x < maxX; x++) {
  59. for (int y = minY; y < maxY; y++) {
  60. Int16 depth = depthImage.getDepthAt(x,y);
  61. touchImage.setTouchAt(x, y, TouchImageState.touchArea);
  62. if (Math.Abs(depthAtTouch - depth) < maxDepthDifference) {
  63. matchedPixels++;
  64. touchImage.setTouchAt(x, y, TouchImageState.touchAreaMatched);
  65. }
  66. countedPixels++;
  67. }
  68. }
  69. float rel = (float)matchedPixels / (float)countedPixels;
  70. //status bar (% of matched pixels) -> green
  71. for (int x = minX; x < minX + (maxX-minX)*rel; x++) {
  72. touchImage.setTouchAt(x, maxY - 1, TouchImageState.touchAreaStatusBar);
  73. }
  74. return rel;
  75. }
  76. }
  77. }