在并行线程之间共享kinect流数据

时间:2015-06-27 20:08:43

标签: c# multithreading kinect kinect-sdk

我试图解决的问题是,我将来自不同项目的课程组合到一个项目中。这些类中的函数使用KinectSensor变量,该变量是其类的成员并识别不同的手势。我试图在并行线程中运行手势识别功能,但想知道如何将kinect数据传递给它们。

是否有一种方法可以在并行运行的单个程序的线程之间共享kinect传感器流数据(彩色流,深度流等)数据?一个事件是否会触发像#34;骨架准备好"被接收到kinect流数据的所有线程接收?

我使用kinect 360,在c#中使用sdk 1.8

1 个答案:

答案 0 :(得分:1)

KinectSensor对象代表单个Kinect,单个Kinect只能由单个程序使用。

如果您正在谈论的两个主题是同一个程序的一部分,您可以"共享流"只需共享对同一对象的访问权限。

但是,通过将回调方法注册到事件来获取颜色,深度和骨架流。所以你可以做的是:

  1. 将对同一个KinectSensor对象(预初始化)的引用分享给两个线程;
  2. 在每个线程中,注册深度,颜色和骨架流的回调,它在线程本地的变量中更新流的内容(或仅由一个线程使用):

    // Reference to the single KinectSensor object
    private KinectSensor kinectSensor;
    
    // Local variables with depth, color and skeletal information
    private Skeleton[] skeleton_thread1;
    private Skeleton[] skeleton_thread2;
    private short[] depth_thread1;
    private short[] depth_thread2;
    private byte[] color_thread1;
    private byte[] color_thread2;
    
    // ...
    
    // Register callbacks (you must so this both in thread1 and thread2)
    // Assume that here we are refererring to thread1
    kinectSensor.ColorFrameReady += new EventHandler<ColorFrameReadyEventArgs>(kinectSensor_ColorFrameReady1);
    kinectSensor.DepthFrameReady += new EventHandler<DepthFrameReadyEventArgs>(kinectSensor_DepthFrameReady1);
    kinectSensor.SkeletonFrameReady += new EventHandler<SkeletonFrameReadyEventArgs>(kinectSensor_SkeletonFrameReady1);
    
    // ...
    
    private void kinectSensor_SkeletonFrameReady1(object sender, SkeletonFrameReadyEventArgs e)
    {
        this.skeletonFrame_thread1 = 
        using (SkeletonFrame skeletonFrame = e.OpenSkeletonFrame())
        {
            if (skeletonFrame != null)
            {
                this.skeleton_thread1 = new Skeleton[skeletonFrame.SkeletonArrayLength];
    
                skeletonFrame.CopySkeletonDataTo(this.skeleton_thread1);
    
                // Do stuff
            }
            else
            {
                // Do stuff
            }
        }
    }
    
    private void kinectSensor_ColorFrameReady1(object sender, ColorImageFrameReadyEventArgs e)
    {
        using (ColorImageFrame colorImageFrame = e.OpenColorImageFrame())
        {
            if (colorImageFrame != null)
            {
                this.color_thread1 = new byte[colorImageFrame.PixelDataLength];
                colorImageFrame.CopyPixelDataTo(this.color_thread1);
    
                // Do Stuff
            }
            else
            {
                // Do stuff
            }
        }
    }
    
    private void kinectSensor_DepthFrameReady1(object sender, DepthImageFrameReadyEventArgs e)
    {
        using (DepthImageFrame depthImageFrame = e.OpenDepthImageFrame())
        {
            if (depthImageFrame != null)
            {
                this.depth_thread1 = new short[depthImageFrame.PixelDataLength];
                depthImageFrame.CopyPixelDataTo(this.depth_thread1);
    
                // Do Stuff
            }
            else
            {
                // Do stuff
            }
        }
    }