VideoHandle.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 System.Diagnostics;
  8. using bbiwarg.Utility;
  9. using bbiwarg.Detectors.Fingers;
  10. using bbiwarg.Detectors.Palm;
  11. using bbiwarg.Detectors.Touch;
  12. using bbiwarg.Images;
  13. using bbiwarg.InputProviders;
  14. using Emgu.CV;
  15. using Emgu.CV.Structure;
  16. using bbiwarg.Graphics;
  17. namespace bbiwarg
  18. {
  19. class VideoHandle
  20. {
  21. private IInputProvider inputProvider;
  22. private InputFrame inputFrame;
  23. public int Width { get; private set; }
  24. public int Height { get; private set; }
  25. public int CurrentFrame
  26. {
  27. get
  28. {
  29. if (sourceIsMovie())
  30. return inputProvider.getCurrentMovieFrame();
  31. else
  32. return videoFrame;
  33. }
  34. }
  35. public OutputImage[] OutputImages { get; private set; }
  36. private DepthImage depthImage;
  37. private EdgeImage edgeImage;
  38. private FingerDetector fingerDetector;
  39. private PalmDetector palmDetector;
  40. private TouchDetector touchDetector;
  41. private PalmTouchDetector palmTouchDetector;
  42. private FingerTracker fingerTracker;
  43. private TouchTracker touchTracker;
  44. private TouchEventVisualizer touchEventVisualizer;
  45. private int videoFrame = 0;
  46. public VideoHandle(IInputProvider inputProvider)
  47. {
  48. this.inputProvider = inputProvider;
  49. //initialize trackers
  50. touchTracker = new TouchTracker();
  51. fingerTracker = new FingerTracker();
  52. }
  53. public void start()
  54. {
  55. inputProvider.init();
  56. inputProvider.start();
  57. inputProvider.updateFrame();
  58. processFrameUpdate();
  59. }
  60. public void stop()
  61. {
  62. inputProvider.stop();
  63. }
  64. public bool sourceIsMovie()
  65. {
  66. return inputProvider.sourceIsMovie();
  67. }
  68. public void reversePlay()
  69. {
  70. inputProvider.reversePlay();
  71. }
  72. public void pauseMovie()
  73. {
  74. inputProvider.pauseMovie();
  75. }
  76. public void unpauseMovie()
  77. {
  78. inputProvider.unpauseMovie();
  79. }
  80. public void nextFrame()
  81. {
  82. if (inputProvider.isActive())
  83. {
  84. inputProvider.releaseFrame();
  85. inputProvider.updateFrame();
  86. processFrameUpdate();
  87. }
  88. else
  89. {
  90. inputProvider.stop();
  91. }
  92. ++videoFrame;
  93. }
  94. public List<PalmTouchEvent> getPalmTouchEvents()
  95. {
  96. return palmTouchDetector.PalmTouchEvents;
  97. }
  98. private void processFrameUpdate()
  99. {
  100. Timer.start("processFrameUpdate");
  101. //read data from inputProvider
  102. Timer.start("readInputData");
  103. inputFrame = inputProvider.getInputFrame();
  104. Width = inputFrame.Width;
  105. Height = inputFrame.Height;
  106. Timer.stop("readInputData");
  107. //create output images
  108. Timer.start("createOtherImages");
  109. int numImages = 4;
  110. OutputImages = new OutputImage[numImages];
  111. for (int i = 0; i < numImages; i++) {
  112. OutputImages[i] = new OutputImage(Width, Height);
  113. }
  114. Timer.stop("createOtherImages");
  115. //create depthImage
  116. Timer.start("createDepthImage");
  117. Image<Gray, Int16> image = new Image<Gray, Int16>(Width, Height, Width * 2, inputFrame.RawDepthData);
  118. depthImage = new DepthImage(image);
  119. Image<Gray, byte> tmpDepth = (depthImage.MaxDepth - depthImage.MinDepth) - depthImage.Image;
  120. OutputImages[0].Image[0] = OutputImages[0].Image[1] = OutputImages[0].Image[2] = tmpDepth;
  121. Timer.stop("createDepthImage");
  122. // create edge image
  123. Timer.start("createEdgeImage");
  124. edgeImage = new EdgeImage(depthImage);
  125. OutputImages[1].Image[2] = edgeImage.Image.ThresholdBinary(new Gray(0), new Gray(1)).Mul(255);
  126. Timer.stop("createEdgeImage");
  127. //detect fingers
  128. Timer.start("fingerDetection");
  129. fingerDetector = new FingerDetector(depthImage, edgeImage, OutputImages[1]);
  130. Timer.stop("fingerDetection");
  131. //track fingers
  132. Timer.start("fingerTracking");
  133. fingerTracker.setDetectedTouchEventsThisFrame(fingerDetector.Fingers, OutputImages[1]);
  134. Timer.stop("fingerTracking");
  135. //remove background noise
  136. Timer.start("removeBackground");
  137. depthImage.removeBackground(fingerTracker.TrackedFingers);
  138. edgeImage = new EdgeImage(depthImage);
  139. OutputImages[2].Image[0] = OutputImages[2].Image[1] = OutputImages[2].Image[2] = (depthImage.MaxDepth - depthImage.MinDepth) - depthImage.Image;
  140. Timer.stop("removeBackground");
  141. //detect palm
  142. Timer.start("palmDetection");
  143. palmDetector = new PalmDetector(depthImage, edgeImage, fingerDetector.Fingers, OutputImages[2]);
  144. if (sourceIsMovie() && CurrentFrame == 0)
  145. PalmDetector.resetFilter();
  146. Timer.stop("palmDetection");
  147. //detect touchEvents
  148. Timer.start("touchDetection");
  149. touchDetector = new TouchDetector(fingerTracker.TrackedFingers, depthImage, OutputImages[2]);
  150. if (palmDetector.PalmQuad != null)
  151. palmTouchDetector = new PalmTouchDetector(touchDetector.TouchEvents, palmDetector.PalmQuad);
  152. Timer.stop("touchDetection");
  153. //track touchEvents
  154. Timer.start("touchTracking");
  155. touchTracker.setDetectedTouchEventsThisFrame(touchDetector.TouchEvents, OutputImages[2]);
  156. Timer.stop("touchTracking");
  157. // touch event visualizer
  158. if (touchEventVisualizer == null)
  159. touchEventVisualizer = new TouchEventVisualizer(Width, Height);
  160. if (CurrentFrame == 0)
  161. touchEventVisualizer.Reset();
  162. if (palmTouchDetector != null)
  163. {
  164. foreach (PalmTouchEvent e in palmTouchDetector.PalmTouchEvents)
  165. touchEventVisualizer.addPalmTouchEvent(e, CurrentFrame);
  166. touchEventVisualizer.updateImage();
  167. OutputImages[3] = touchEventVisualizer.OutputImage;
  168. }
  169. // add borders
  170. for (int i = 0; i < numImages; i++)
  171. {
  172. OutputImages[i].drawRectangle(0, 0, Width - 1, Height - 1, Color.White);
  173. }
  174. Timer.stop("processFrameUpdate");
  175. }
  176. }
  177. }