TouchDetector.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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.5f;
  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, 0, 127, 0);
  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. outputImage.drawPixel(x, y, 50, 50, 50);
  65. if (Math.Abs(depthAtTouch - depth) < maxDepthDifference)
  66. {
  67. matchedPixels++;
  68. outputImage.drawPixel(x, y, 50, 50, 255);
  69. }
  70. countedPixels++;
  71. }
  72. }
  73. float rel = (float)matchedPixels / (float)countedPixels;
  74. //status bar (% of matched pixels) -> green
  75. for (int x = minX; x < minX + (maxX - minX) * rel; x++)
  76. {
  77. outputImage.drawPixel(x, maxY - 1, 0, 255, 0);
  78. }
  79. return rel;
  80. }
  81. }
  82. }