TouchDetector.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. using bbiwarg.Graphics;
  14. namespace bbiwarg.Detectors.Touch
  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. float floodValueThreshold = 0.33f;
  29. foreach (Finger finger in fingers)
  30. {
  31. Vector2D tipPoint = finger.Tip;
  32. float floodValue = getFloodValue(tipPoint);
  33. if (floodValue > floodValueThreshold)
  34. {
  35. //correct touchEvent position
  36. Vector2D direction = finger.LineSegment.Line.Direction;
  37. float directionFactor = 10;
  38. float x = HelperFunctions.thresholdRange<float>(0, depthImage.Width - 1, tipPoint.X + directionFactor * direction.X);
  39. float y = HelperFunctions.thresholdRange<float>(0, depthImage.Height - 1, tipPoint.Y + directionFactor * direction.Y);
  40. Vector2D tep = new Vector2D(x, y);
  41. outputImage.fillCircle(tep.IntX, tep.IntY, 5, Constants.TouchEventDetectedColor);
  42. TouchEvent touchEvent = new TouchEvent(tep, floodValue, finger);
  43. TouchEvents.Add(touchEvent);
  44. }
  45. }
  46. }
  47. private float getFloodValue(Point touchPoint)
  48. {
  49. int searchSize = 15;
  50. int maxDepthDifference = 20;
  51. Int16 fingerDiameter = 5;
  52. Int16 depthAtTouch = (Int16)(depthImage.getDepthAt(touchPoint) + fingerDiameter);
  53. int minX = Math.Max(touchPoint.X - searchSize, 0);
  54. int maxX = Math.Min(touchPoint.X + searchSize, depthImage.Width);
  55. int minY = Math.Max(touchPoint.Y - searchSize, 0);
  56. int maxY = Math.Min(touchPoint.Y + searchSize, depthImage.Height);
  57. int matchedPixels = 0;
  58. int countedPixels = 0;
  59. for (int x = minX; x < maxX; x++)
  60. {
  61. for (int y = minY; y < maxY; y++)
  62. {
  63. Int16 depth = depthImage.getDepthAt(x, y);
  64. Color color = outputImage.getColotAt(x, y);
  65. Color subtractColor;
  66. if (depthAtTouch < depth && Math.Abs(depthAtTouch - depth) < maxDepthDifference)
  67. {
  68. matchedPixels++;
  69. subtractColor = Constants.TouchEventAreaMatchedSubtractColor;
  70. }
  71. else
  72. {
  73. subtractColor = Constants.TouchEventAreaNonMatchedSubtractColor;
  74. }
  75. 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));
  76. outputImage.drawPixel(x, y, newColor);
  77. countedPixels++;
  78. }
  79. }
  80. float rel = (float)matchedPixels / (float)countedPixels;
  81. //status bar (% of matched pixels) -> green
  82. for (int x = minX; x < minX + (maxX - minX) * rel; x++)
  83. {
  84. outputImage.drawPixel(x, maxY - 1, Constants.TouchEventStatusBarColor);
  85. }
  86. return rel;
  87. }
  88. }
  89. }