TouchDetector.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 fingerDiameter = 5;
  36. Int16 depthAtTouch = (Int16) (depthImage.getDepthAt(touchX, touchY) + fingerDiameter);
  37. int minX = Math.Max(touchX - searchSize, 0);
  38. int maxX = Math.Min(touchX + searchSize, depthImage.getWidth());
  39. int minY = Math.Max(touchY - searchSize, 0);
  40. int maxY = Math.Min(touchY + searchSize, depthImage.getHeight());
  41. int matchedPixels = 0;
  42. int countedPixels = 0;
  43. for (int x = minX; x < maxX; x++) {
  44. for (int y = minY; y < maxY; y++) {
  45. Int16 depth = depthImage.getDepthAt(x,y);
  46. depthImage.setDepthAt(x, y, Int16.MaxValue - 1);//counted pixels -> red
  47. if (Math.Abs(depthAtTouch - depth) < maxDepthDifference) {
  48. matchedPixels++;
  49. depthImage.setDepthAt(x, y, Int16.MaxValue);//matched pixels -> blue
  50. }
  51. countedPixels++;
  52. }
  53. }
  54. float rel = (float)matchedPixels / (float)countedPixels;
  55. //status bar (% of matched pixels) -> green
  56. for (int x = minX; x < minX + (maxX-minX)*rel; x++) {
  57. depthImage.setDepthAt(x, maxY-1, Int16.MaxValue-2);
  58. }
  59. return rel;
  60. }
  61. }
  62. }