TouchDetector.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. namespace bbiwarg.Detectors.Touch
  12. {
  13. class TouchDetector
  14. {
  15. private DepthImage depthImage;
  16. private List<Finger> fingers;
  17. public TouchDetector(DepthImage depthImage, List<Finger> fingers) {
  18. this.depthImage = depthImage;
  19. this.fingers = fingers;
  20. foreach (Finger finger in fingers) {
  21. //farthest
  22. FingerPoint fp1 = finger.getFarthest();
  23. float touchFP1 = isTouchAt(fp1.getX(), fp1.getY());
  24. //nearest
  25. FingerPoint fp2 = finger.getNearest();
  26. float touchFP2 = isTouchAt(fp2.getX(), fp2.getY());
  27. int depthDifference = fp1.getDepth() - fp2.getDepth();
  28. if(touchFP1 > 0.6f && touchFP2 > 0.6f && depthDifference > 20 && depthDifference < 50)
  29. Console.WriteLine("TouchEvent bei x:" + fp1.getX() + " y:" + fp1.getY() + " [FP1:" + Math.Round(touchFP1, 1) + " FP2:" + Math.Round(touchFP2, 1) + "]");
  30. }
  31. }
  32. private float isTouchAt(int touchX, int touchY) {
  33. int searchSize = 15;
  34. int maxDepthDifference = 20;
  35. Int16 depthAtTouch = depthImage.getDepthAt(touchX, touchY);
  36. int minX = Math.Max(touchX - searchSize, 0);
  37. int maxX = Math.Min(touchX + searchSize, depthImage.getWidth());
  38. int minY = Math.Max(touchY - searchSize, 0);
  39. int maxY = Math.Min(touchY + searchSize, depthImage.getHeight());
  40. int matchedPixels = 0;
  41. int countedPixels = 0;
  42. for (int x = minX; x < maxX; x++) {
  43. for (int y = minY; y < maxY; y++) {
  44. Int16 depth = depthImage.getDepthAt(x,y);
  45. depthImage.setDepthAt(x, y, Int16.MaxValue - 1);//counted pixels -> red
  46. if (Math.Abs(depthAtTouch - depth) < maxDepthDifference) {
  47. matchedPixels++;
  48. depthImage.setDepthAt(x, y, Int16.MaxValue);//matched pixels -> blue
  49. }
  50. countedPixels++;
  51. }
  52. }
  53. float rel = (float)matchedPixels / (float)countedPixels;
  54. //status bar (% of matched pixels) -> green
  55. for (int x = minX; x < minX + (maxX-minX)*rel; x++) {
  56. depthImage.setDepthAt(x, maxY-1, Int16.MaxValue-2);
  57. }
  58. return rel;
  59. }
  60. }
  61. }