Search code examples
c#windowskinectkinect-sdkkinect-interaction

Both Skeleton tracking methods are correct in kinect?


I have using kinect SDK to develop a application in C# . Now i have a doubt for tracking skeleton .

I have 2 code , but there is 2 different approaches . I want to know both methods leads to same concepts ? or different ?

code 1 :

void Kinect_SkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
{
    using (SkeletonFrame frame = e.OpenSkeletonFrame())
    {
       m_skeletons = new Skeleton[frame.SkeletonArrayLength];
       frame.CopySkeletonDataTo(m_skeletons);
    } 

     if(m_skeletons != null && m_skeletons.Length != 0)
     { 
         foreach (Skeleton skeleton in m_skeletons)
         {
            if (skeleton != null && skeleton.TrackingState == SkeletonTrackingState.Tracked)
            {
                //doing some operations 
            }


          }

      }

}

now another tracking methods like : code 2 :

void Kinect_SkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
{
    using (SkeletonFrame frame = e.OpenSkeletonFrame())
    {
       m_skeletons = new Skeleton[frame.SkeletonArrayLength];
       frame.CopySkeletonDataTo(m_skeletons);
    } 

     if(m_skeletons != null && m_skeletons.Length != 0)
     { 
         foreach (Skeleton skeleton in m_skeletons.Where(s => s.TrackingState == SkeletonTrackingState.Tracked))
          {

               //doing some operations 
          }

      }

}

is both foreach (Skeleton skeleton in m_skeletons) and the

foreach (Skeleton skeleton in m_skeletons.Where(s => s.TrackingState == SkeletonTrackingState.Tracked))

are correct ? Is any difference ?


Solution

  • Both approaches are functionally equivalent.

    The .Where(s => s.TrackingState == SkeletonTrackingState.Tracked) creates an enumerator that already incorporates the check. So your foreach will only iterate the elements for which the condition holds. In the other example, you do this explicitly with an if statement.