2015-06-27 69 views
2

我試圖解決的問題是,我將來自不同項目的類合併到單個項目中。這些類中的函數使用KinectSensor變量,它是它們類的成員並識別不同的手勢。我試圖在並行線程中運行手勢識別功能,但想知道如何將kinect數據傳遞給它們。在並行線程之間共享kinect流數據

有沒有一種方法可以在並行運行的單個程序的線程之間共享kinect傳感器流數據(Color流,深度流等)數據?接收到kinect流數據的所有線程是否會收到一個觸發事件,如「框架就緒」?

我使用SDK 1.8

回答

1

的KinectSensor對象使用的kinect 360,在C#表示單個超高動力學,並且單個的Kinect只能由單個程序中使用。

如果您正在談論的兩個線程是同一個程序的一部分,您可以通過簡單地共享對同一個對象的訪問來「共享流」。

但是,顏色,深度和骨架流是通過向事件註冊回調方法獲得的。因此,您可以執行以下操作:

  1. 將對同一個KinectSensor對象(預初始化)的引用分享給兩個線程;
  2. 在每個線程
  3. ,註冊回調到深度,顏色和骨架流,其更新在變量中的流的內容是本地線程(或僅由一個線程使用):

    // 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 
         } 
        } 
    } 
    
+0

是否知道Kinect是否將SkeletonFrameReady事件處理程序觸發到單獨的線程中(並行化)還是具有此類選項?我正在重構Hotspotizer的代碼(我的版本位於https://github.com/birbilis/Hotspotizer),它有多個這樣的事件處理程序使用調用多個方法的單個事件處理程序,但是想知道是否有意使用它(如果Kinect SDK在多個線程上並行調用事件處理程序[雖然猜不到]) –