的KinectSensor對象使用的kinect 360,在C#表示單個超高動力學,並且單個的Kinect只能由單個程序中使用。
如果您正在談論的兩個線程是同一個程序的一部分,您可以通過簡單地共享對同一個對象的訪問來「共享流」。
但是,顏色,深度和骨架流是通過向事件註冊回調方法獲得的。因此,您可以執行以下操作:
- 將對同一個KinectSensor對象(預初始化)的引用分享給兩個線程;
在每個線程
,註冊回調到深度,顏色和骨架流,其更新在變量中的流的內容是本地線程(或僅由一個線程使用):
// 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
}
}
}
是否知道Kinect是否將SkeletonFrameReady事件處理程序觸發到單獨的線程中(並行化)還是具有此類選項?我正在重構Hotspotizer的代碼(我的版本位於https://github.com/birbilis/Hotspotizer),它有多個這樣的事件處理程序使用調用多個方法的單個事件處理程序,但是想知道是否有意使用它(如果Kinect SDK在多個線程上並行調用事件處理程序[雖然猜不到]) –