TouchDetector.cs 3.5 KB

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