VideoHandle.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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.Finger;
  8. using bbiwarg.Images;
  9. using bbiwarg.InputProviders;
  10. using Emgu.CV;
  11. using Emgu.CV.Structure;
  12. namespace bbiwarg
  13. {
  14. class VideoHandle
  15. {
  16. private IInputProvider inputProvider;
  17. private InputFrame inputFrame;
  18. private int width;
  19. private int height;
  20. private DepthImage depthImage;
  21. private EdgeImage edgeImage;
  22. private FingerDetector fingerDetector;
  23. public VideoHandle(IInputProvider inputProvider) {
  24. this.inputProvider = inputProvider;
  25. }
  26. public void start() {
  27. inputProvider.init();
  28. inputProvider.start();
  29. inputProvider.updateFrame();
  30. processFrameUpdate();
  31. }
  32. public void stop() {
  33. inputProvider.stop();
  34. }
  35. public void nextFrame()
  36. {
  37. if (inputProvider.isActive())
  38. {
  39. inputProvider.releaseFrame();
  40. inputProvider.updateFrame();
  41. processFrameUpdate();
  42. }
  43. else
  44. {
  45. inputProvider.stop();
  46. }
  47. }
  48. public int getWidth()
  49. {
  50. return width;
  51. }
  52. public int getHeight()
  53. {
  54. return height;
  55. }
  56. public Int16 getDepthAt(int x, int y) {
  57. return depthImage.getDepthAt(x, y);
  58. }
  59. public float getRelativeDepth(int x, int y) {
  60. return depthImage.getRelativeDepth(x, y);
  61. }
  62. public bool isEdgeAt(int x, int y) {
  63. return edgeImage.isEdgeAt(x, y);
  64. }
  65. public bool isPossibleFingerPointAt(int x, int y) {
  66. return fingerDetector.isPossibleFingerPointAt(x, y);
  67. }
  68. public bool isFingerPointAt(int x, int y) {
  69. return fingerDetector.isFingerPointAt(x, y);
  70. }
  71. private void processFrameUpdate()
  72. {
  73. //read data from inputProvider
  74. inputFrame = inputProvider.getInputFrame();
  75. width = inputFrame.getWidth();
  76. height = inputFrame.getHeight();
  77. //create depthImage
  78. Image<Gray, Int16> image = new Image<Gray, Int16>(width, height);
  79. for (int x = 0; x < width; x++) {
  80. for (int y = 0; y < height; y++) {
  81. image.Data[y, x, 0] = inputFrame.getDepthAt(x, y);
  82. }
  83. }
  84. depthImage = new DepthImage(width, height, image);
  85. //create edgeImage
  86. edgeImage = new EdgeImage(depthImage);
  87. //detect fingers
  88. fingerDetector = new FingerDetector(depthImage, edgeImage);
  89. }
  90. }
  91. }