我要解决的问题是我将来自不同项目的类组合到一个项目中。这些类中的函数使用KinectSensor变量,该变量是其类的成员,并且可以识别不同的手势。我试图在并行线程中运行手势识别功能,但想知道如何将kinect数据传递给它们。
是否有一种方法可以在并行运行的单个程序的线程之间共享kinect传感器流数据(颜色流,深度流等)数据?接收kinect流数据的所有线程都将接收到诸如“骨架框架就绪”之类的触发事件吗?
我正在使用sdk 1.8在C#中使用kinect 360
最佳答案
KinectSensor对象代表单个Kinect,并且单个Kinect只能由单个程序使用。
如果您所讨论的两个线程是同一程序的一部分,则可以通过简单地共享对同一对象的访问来“共享流”。
但是,颜色,深度和骨架流是通过将回调方法注册到事件而获得的。因此,您可以执行以下操作:
// 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
}
}
}
关于c# - 在并行线程之间共享kinect流数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31093023/