2015-04-02 53 views
1

我目前正在研究需要跟蹤關節角度(左膝蓋)並將其顯示在屏幕上的項目。我已經繪製了骨架,並且顯示窗口設置爲將骨架覆蓋在人的上方。我使用C#編程並使用Visual Studio 2013 Ultimate。計算和顯示Kinect SDK 2.0中生活的關節角度

我在這裏看到這個鏈接kinect sdk 2.0 joint angles and tracking 但代碼不斷返回一個錯誤,因爲在底部的結果函數不存在,我不知道爲什麼。此外,我無法確定如何創建一個可以在屏幕上不斷顯示的變量(如果不存在,它可以顯示N/A或0)。

我也跟着回答這個頁面的人的鏈接Display the angles of each joint of the skeleton in kinect c# 但代碼是爲SDK 1.X。

有沒有人有關於如何解決我在更新變量/定義結果時遇到的問題的示例代碼或建議,以便我可以使用第一個示例中的代碼?

+0

如果有人知道如何讓第一個鏈接底部的代碼不斷更新一個非常棒的變量,那麼Skeleton類型不存在於SDK 2.0中,我無法調用Body.Joints – Jicnon 2015-04-02 17:17:18

回答

2

儘管我沒有看到自己的代碼,或者您是如何實現覆蓋骨骼的人物追蹤,我建議您閱讀Mike Taulty的文章系列:http://mtaulty.com/CommunityServer/blogs/mike_taultys_blog/archive/2014/11/04/kinect-for-windows-v2-sdk-hello-custom-gesture-world-part-3.aspx(此鏈接是本系列的第3篇文章,您可以找到鏈接到最高處的#1和#2)。

雖然他使用GestureBuilder Beta來跟蹤骨骼運動,但對於我來說,這是一個很好的起點,讓你能夠運行 - 特別是KINECT設置,事件監聽器等,以便了解KINECT 2獲取它的框架並獲取c#代碼。在第2部分中,他使用一種顏色在WPF應用程序中填充了一個控件,具體取決於檢測到記錄的手勢進展的程度,我認爲這可以部分地回答有關屏幕上「活動變量」的問題。

一旦你按照教程,爲你確定了相關的代碼部分,你意識到你可以(嚴格地說)拋棄OnGestureFrameArrived功能,因爲你需要修改OnBodyFrameArrived方法。在這裏你檢查trackedBody,如果找到了,你可以訪問它的Joints並計算它們的角度。爲了進行調試,我將結果寫入控制檯窗口,所以最終你會將這個結果寫入一個變量,該變量與顯示它的GUI元素相連接。

public void OnBodyFrameArrived(object sender, BodyFrameArrivedEventArgs e) 
    { 
     using (var frame = e.FrameReference.AcquireFrame()) 
     { 
      if (frame != null) 
      { 
       frame.GetAndRefreshBodyData(this.bodies); 

       var trackedBody = this.bodies.Where(b => b.IsTracked).FirstOrDefault(); 

       if (trackedBody != null) 
       { 
         List<Joint> myJoints = new List<Joint>(); 
         myJoints.Add(trackedBody.Joints[JointType.FootLeft]); 
         myJoints.Add(trackedBody.Joints[JointType.AnkleLeft]); 
         myJoints.Add(trackedBody.Joints[JointType.KneeLeft]); 


         if (myJoints.TrueForAll(x => x.TrackingState == TrackingState.Tracked)) 
         { 
          Vector3D KneeLeft = new Vector3D(trackedBody.Joints[JointType.KneeLeft].Position.X, trackedBody.Joints[JointType.KneeLeft].Position.Y, trackedBody.Joints[JointType.KneeLeft].Position.Z); 
          Vector3D AnkleLeft = new Vector3D(trackedBody.Joints[JointType.AnkleLeft].Position.X, trackedBody.Joints[JointType.AnkleLeft].Position.Y, trackedBody.Joints[JointType.AnkleLeft].Position.Z); 
          Vector3D FootLeft = new Vector3D(trackedBody.Joints[JointType.FootLeft].Position.X, trackedBody.Joints[JointType.FootLeft].Position.Y, trackedBody.Joints[JointType.FootLeft].Position.Z); 

          Console.WriteLine("#1: " + AngleBetweenTwoVectors(AnkleLeft - KneeLeft, AnkleLeft - FootLeft)); 
         } 
       } 
       else 
       { 
        this.OnTrackingIdLost(null, null); 
       } 
      } 
     } 
    } 

    public double AngleBetweenTwoVectors(Vector3D vectorA, Vector3D vectorB) 
    { 
     double dotProduct = 0.0; 
     vectorA.Normalize(); 
     vectorB.Normalize(); 
     dotProduct = Vector3D.DotProduct(vectorA, vectorB); 

     return (double)Math.Acos(dotProduct)/Math.PI * 180; 
    } 

TLDR版本:這取決於你如何處理你獲得的Kinect的框架,你需要在你的代碼,其中一個跟蹤身體在幀中發現,以確定正確的事件,在那裏你可以看看針對不同身體關節的狀態並計算角度。另外,如果你想要的話,你可以發給我你的代碼,我可以試着爲你提供這個代碼,因爲你的代碼對我來說也是有意義的。