using Iisu;
using System;
using Emgu.CV;
using Emgu.CV.Structure;
namespace BBIWARG.Input.InputProviding
{
///
/// InputProvider provides the raw depth and confidence data through an event.
///
public class InputProviderIisu : IInputProvider
{
///
/// data handle for the raw confidence data
///
protected IDataHandle confidenceImage;
///
/// data handle for the raw depth data
///
protected IDataHandle depthImage;
///
/// iisu device from which the data is read
///
protected IDevice device;
///
/// parameter handle for the horizontal field of view angle
///
protected IParameterHandle fieldOfViewHorizontal;
///
/// parameter handle for the vertical field of view angle
///
protected IParameterHandle fieldOfViewVertical;
///
/// parameter handle for the frame rate
///
protected IParameterHandle frameRate;
///
/// iisu handle
///
protected IHandle handle;
///
/// parameter handle for the image height
///
protected IParameterHandle height;
///
/// parameter handle for the image width
///
protected IParameterHandle width;
///
/// the id of the current frame
///
public virtual int CurrentFrameID { get { return device.FrameId; } }
///
/// the horizontal field of view angle
///
public float FieldOfViewHorizontal { get { return fieldOfViewHorizontal.Value; } }
///
/// the vertical field of view angle
///
public float FieldOfViewVertical { get { return fieldOfViewVertical.Value; } }
///
/// the height of all images
///
public int ImageHeight { get { return height.Value; } }
///
/// the width of all images
///
public int ImageWidth { get { return width.Value; } }
///
/// true iff the input source provides data
///
public bool IsActive { get; private set; }
///
/// event that the device started
///
public event DeviceStartedEventHandler DeviceStartedEvent;
///
/// event that a new frame is available
///
public event NewFrameEventHandler NewFrameEvent;
///
/// Constructs an InputProvider.
///
public InputProviderIisu()
{
IsActive = false;
}
///
/// Initializes to device and data handles.
///
public void initialize()
{
createDevice();
registerHandles();
}
///
/// Starts the device.
///
public void start()
{
device.Start();
IsActive = true;
if (DeviceStartedEvent != null)
DeviceStartedEvent(this, new EventArgs());
run();
}
///
/// Stops the device.
///
public void stop()
{
IsActive = false;
device.Stop(true);
}
///
/// Creates an iisu device which provides the data.
///
protected void createDevice()
{
handle = Iisu.Iisu.Context.CreateHandle();
IDeviceConfiguration conf = createDeviceConfiguration();
device = handle.InitializeDevice(conf);
}
///
/// Returns an iisu device configuration.
///
/// iisu device configuration
protected virtual IDeviceConfiguration createDeviceConfiguration()
{
IDeviceConfiguration conf = handle.CreateDeviceConfiguration();
conf.IsAsynchronous = false;
return conf;
}
///
/// Gets the next frame from the device.
///
protected virtual void nextFrame()
{
device.UpdateFrame(true);
provideNewFrame();
device.ReleaseFrame();
}
///
/// Triggers the new frame event.
///
protected void provideNewFrame()
{
if (NewFrameEvent != null)
{
Image rawDepthImage = new Image(ImageWidth, ImageHeight, ImageWidth * 2, depthImage.Value.Raw);
Image confidenceImage2 = new Image(ImageWidth, ImageHeight, ImageWidth * 2, confidenceImage.Value.Raw);
Image confidenceMask = confidenceImage2.ThresholdBinary(new Gray(Parameters.ConfidenceImageMinThreshold), new Gray(1)).Convert();
rawDepthImage = rawDepthImage.Or((1 - confidenceMask).Convert().Mul(UInt16.MaxValue));
NewFrameEvent(this, new NewFrameEventArgs(CurrentFrameID, rawDepthImage));
}
}
///
/// Registers all parameter and data handles.
///
protected virtual void registerHandles()
{
width = device.RegisterParameterHandle("SOURCE.CAMERA.DEPTH.Width");
height = device.RegisterParameterHandle("SOURCE.CAMERA.DEPTH.Height");
fieldOfViewHorizontal = device.RegisterParameterHandle("SOURCE.CAMERA.DEPTH.HFOV");
fieldOfViewVertical = device.RegisterParameterHandle("SOURCE.CAMERA.DEPTH.VFOV");
frameRate = device.RegisterParameterHandle("SOURCE.FrameRate");
depthImage = device.RegisterDataHandle("SOURCE.CAMERA.DEPTH.Image");
confidenceImage = device.RegisterDataHandle("SOURCE.CAMERA.CONFIDENCE.Image");
}
///
/// Provides the main loop for reading data from the device.
///
protected virtual void run()
{
while (IsActive)
nextFrame();
}
}
}