VideoHandle.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Diagnostics;
  7. using bbiwarg.Detectors.Fingers;
  8. using bbiwarg.Detectors.Touch;
  9. using bbiwarg.Images;
  10. using bbiwarg.InputProviders;
  11. using Emgu.CV;
  12. using Emgu.CV.Structure;
  13. namespace bbiwarg
  14. {
  15. class VideoHandle
  16. {
  17. private IInputProvider inputProvider;
  18. private InputFrame inputFrame;
  19. private int width;
  20. private int height;
  21. private DepthImage depthImage;
  22. private EdgeImage edgeImage;
  23. private TouchImage touchImage;
  24. private PalmImage palmImage;
  25. private FingerDetector fingerDetector;
  26. private TouchDetector touchDetector;
  27. private TouchTracker touchTracker;
  28. public VideoHandle(IInputProvider inputProvider) {
  29. this.inputProvider = inputProvider;
  30. //initialize trackers
  31. touchTracker = new TouchTracker();
  32. }
  33. public void start() {
  34. inputProvider.init();
  35. inputProvider.start();
  36. inputProvider.updateFrame();
  37. processFrameUpdate();
  38. }
  39. public void stop() {
  40. inputProvider.stop();
  41. }
  42. public void nextFrame()
  43. {
  44. if (inputProvider.isActive())
  45. {
  46. inputProvider.releaseFrame();
  47. inputProvider.updateFrame();
  48. processFrameUpdate();
  49. }
  50. else
  51. {
  52. inputProvider.stop();
  53. }
  54. }
  55. public int getWidth()
  56. {
  57. return width;
  58. }
  59. public int getHeight()
  60. {
  61. return height;
  62. }
  63. public Int16 getDepthAt(int x, int y) {
  64. return depthImage.getDepthAt(x, y);
  65. }
  66. public float getRelativeDepth(int x, int y) {
  67. return depthImage.getRelativeDepth(x, y);
  68. }
  69. public bool isEdgeAt(int x, int y) {
  70. return edgeImage.isEdgeAt(x, y);
  71. }
  72. public bool isPossibleFingerPointAt(int x, int y) {
  73. return fingerDetector.isPossibleFingerPointAt(x, y);
  74. }
  75. public bool isFingerPointAt(int x, int y) {
  76. return fingerDetector.isFingerPointAt(x, y);
  77. }
  78. public bool isPalmPointAt(int x, int y) {
  79. return palmImage.belongsToPalm(x, y);
  80. }
  81. public TouchImageState getTouchImageStateAt(int x, int y) {
  82. return touchImage.getStateAt(x, y);
  83. }
  84. private void processFrameUpdate()
  85. {
  86. //read data from inputProvider
  87. inputFrame = inputProvider.getInputFrame();
  88. width = inputFrame.getWidth();
  89. height = inputFrame.getHeight();
  90. //create depthImage
  91. Image<Gray, Int16> image = new Image<Gray, Int16>(width, height);
  92. for (int x = 0; x < width; x++) {
  93. for (int y = 0; y < height; y++) {
  94. image.Data[y, x, 0] = inputFrame.getDepthAt(x, y);
  95. }
  96. }
  97. depthImage = new DepthImage(width, height, image);
  98. //create edgeImage
  99. edgeImage = new EdgeImage(depthImage);
  100. //detect fingers
  101. fingerDetector = new FingerDetector(depthImage, edgeImage);
  102. //create touchImage
  103. touchImage = new TouchImage(depthImage);
  104. //detect touchEvents
  105. touchDetector = new TouchDetector(fingerDetector.getFingers(), depthImage, touchImage);
  106. //track touchEvents
  107. touchTracker.setDetectedTouchEventsThisFrame(touchDetector.getTouchEvents(), touchImage);
  108. palmImage = new PalmImage(edgeImage);
  109. }
  110. }
  111. }