TouchDetector.cs 3.3 KB

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