InputProviderIisu.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. using Iisu;
  2. using System;
  3. using Emgu.CV;
  4. using Emgu.CV.Structure;
  5. namespace BBIWARG.Input.InputProviding
  6. {
  7. /// <summary>
  8. /// InputProvider provides the raw depth and confidence data through an event.
  9. /// </summary>
  10. public class InputProviderIisu : IInputProvider
  11. {
  12. /// <summary>
  13. /// data handle for the raw confidence data
  14. /// </summary>
  15. protected IDataHandle<Iisu.Data.IImageData> confidenceImage;
  16. /// <summary>
  17. /// data handle for the raw depth data
  18. /// </summary>
  19. protected IDataHandle<Iisu.Data.IImageData> depthImage;
  20. /// <summary>
  21. /// iisu device from which the data is read
  22. /// </summary>
  23. protected IDevice device;
  24. /// <summary>
  25. /// parameter handle for the horizontal field of view angle
  26. /// </summary>
  27. protected IParameterHandle<float> fieldOfViewHorizontal;
  28. /// <summary>
  29. /// parameter handle for the vertical field of view angle
  30. /// </summary>
  31. protected IParameterHandle<float> fieldOfViewVertical;
  32. /// <summary>
  33. /// parameter handle for the frame rate
  34. /// </summary>
  35. protected IParameterHandle<float> frameRate;
  36. /// <summary>
  37. /// iisu handle
  38. /// </summary>
  39. protected IHandle handle;
  40. /// <summary>
  41. /// parameter handle for the image height
  42. /// </summary>
  43. protected IParameterHandle<int> height;
  44. /// <summary>
  45. /// parameter handle for the image width
  46. /// </summary>
  47. protected IParameterHandle<int> width;
  48. /// <summary>
  49. /// the id of the current frame
  50. /// </summary>
  51. public virtual int CurrentFrameID { get { return device.FrameId; } }
  52. /// <summary>
  53. /// the horizontal field of view angle
  54. /// </summary>
  55. public float FieldOfViewHorizontal { get { return fieldOfViewHorizontal.Value; } }
  56. /// <summary>
  57. /// the vertical field of view angle
  58. /// </summary>
  59. public float FieldOfViewVertical { get { return fieldOfViewVertical.Value; } }
  60. /// <summary>
  61. /// the height of all images
  62. /// </summary>
  63. public int ImageHeight { get { return height.Value; } }
  64. /// <summary>
  65. /// the width of all images
  66. /// </summary>
  67. public int ImageWidth { get { return width.Value; } }
  68. /// <summary>
  69. /// true iff the input source provides data
  70. /// </summary>
  71. public bool IsActive { get; private set; }
  72. /// <summary>
  73. /// event that the device started
  74. /// </summary>
  75. public event DeviceStartedEventHandler DeviceStartedEvent;
  76. /// <summary>
  77. /// event that a new frame is available
  78. /// </summary>
  79. public event NewFrameEventHandler NewFrameEvent;
  80. /// <summary>
  81. /// Constructs an InputProvider.
  82. /// </summary>
  83. public InputProviderIisu()
  84. {
  85. IsActive = false;
  86. }
  87. /// <summary>
  88. /// Initializes to device and data handles.
  89. /// </summary>
  90. public void initialize()
  91. {
  92. createDevice();
  93. registerHandles();
  94. }
  95. /// <summary>
  96. /// Starts the device.
  97. /// </summary>
  98. public void start()
  99. {
  100. device.Start();
  101. IsActive = true;
  102. if (DeviceStartedEvent != null)
  103. DeviceStartedEvent(this, new EventArgs());
  104. run();
  105. }
  106. /// <summary>
  107. /// Stops the device.
  108. /// </summary>
  109. public void stop()
  110. {
  111. IsActive = false;
  112. device.Stop(true);
  113. }
  114. /// <summary>
  115. /// Creates an iisu device which provides the data.
  116. /// </summary>
  117. protected void createDevice()
  118. {
  119. handle = Iisu.Iisu.Context.CreateHandle();
  120. IDeviceConfiguration conf = createDeviceConfiguration();
  121. device = handle.InitializeDevice(conf);
  122. }
  123. /// <summary>
  124. /// Returns an iisu device configuration.
  125. /// </summary>
  126. /// <returns>iisu device configuration</returns>
  127. protected virtual IDeviceConfiguration createDeviceConfiguration()
  128. {
  129. IDeviceConfiguration conf = handle.CreateDeviceConfiguration();
  130. conf.IsAsynchronous = false;
  131. return conf;
  132. }
  133. /// <summary>
  134. /// Gets the next frame from the device.
  135. /// </summary>
  136. protected virtual void nextFrame()
  137. {
  138. device.UpdateFrame(true);
  139. provideNewFrame();
  140. device.ReleaseFrame();
  141. }
  142. /// <summary>
  143. /// Triggers the new frame event.
  144. /// </summary>
  145. protected void provideNewFrame()
  146. {
  147. if (NewFrameEvent != null)
  148. {
  149. Image<Gray, UInt16> rawDepthImage = new Image<Gray, UInt16>(ImageWidth, ImageHeight, ImageWidth * 2, depthImage.Value.Raw);
  150. Image<Gray, UInt16> confidenceImage2 = new Image<Gray, UInt16>(ImageWidth, ImageHeight, ImageWidth * 2, confidenceImage.Value.Raw);
  151. Image<Gray, byte> confidenceMask = confidenceImage2.ThresholdBinary(new Gray(Parameters.ConfidenceImageMinThreshold), new Gray(1)).Convert<Gray, byte>();
  152. rawDepthImage = rawDepthImage.Or((1 - confidenceMask).Convert<Gray, UInt16>().Mul(UInt16.MaxValue));
  153. NewFrameEvent(this, new NewFrameEventArgs(CurrentFrameID, rawDepthImage));
  154. }
  155. }
  156. /// <summary>
  157. /// Registers all parameter and data handles.
  158. /// </summary>
  159. protected virtual void registerHandles()
  160. {
  161. width = device.RegisterParameterHandle<int>("SOURCE.CAMERA.DEPTH.Width");
  162. height = device.RegisterParameterHandle<int>("SOURCE.CAMERA.DEPTH.Height");
  163. fieldOfViewHorizontal = device.RegisterParameterHandle<float>("SOURCE.CAMERA.DEPTH.HFOV");
  164. fieldOfViewVertical = device.RegisterParameterHandle<float>("SOURCE.CAMERA.DEPTH.VFOV");
  165. frameRate = device.RegisterParameterHandle<float>("SOURCE.FrameRate");
  166. depthImage = device.RegisterDataHandle<Iisu.Data.IImageData>("SOURCE.CAMERA.DEPTH.Image");
  167. confidenceImage = device.RegisterDataHandle<Iisu.Data.IImageData>("SOURCE.CAMERA.CONFIDENCE.Image");
  168. }
  169. /// <summary>
  170. /// Provides the main loop for reading data from the device.
  171. /// </summary>
  172. protected virtual void run()
  173. {
  174. while (IsActive)
  175. nextFrame();
  176. }
  177. }
  178. }