none
How to get x, y, z coordinates of joints in KinectExplorer from SDK 2.0? RRS feed

  • Question

  • Hi!

    I´m starting a project where i need x, y, z coordinates of the pre-mapped joints in Kinect SDK 2.0. So far i have only modified the example that came with the SDK, KinectExplorer, trying to get the program simply to print out x, y, z coordinates of e.g. the left knee. Can anyone help me with this problem? 

    Wednesday, March 14, 2012 5:07 PM

Answers

  • OK, here it is, the full monty:

    namespace SkeletonExample
    {
        using System;
        using System.Linq;
        using Microsoft.Kinect;
    
        internal class Program
        {
            private static void Main(string[] args)
            {
                // Find the first connected sensor
                KinectSensor sensor = KinectSensor.KinectSensors.Where(s => s.Status == KinectStatus.Connected).FirstOrDefault();
                if (sensor == null)
                {
                    Console.WriteLine("No Kinect sensor found!");
                    return;
                }
    
                // Create object that will track skeletons using the sensor
                Tracker tracker = new Tracker(sensor);
    
                // Start the sensor
                sensor.Start();
    
                // Run until the user presses 'q' or 'Q' on the keyboard
                while (Char.ToLowerInvariant(Console.ReadKey().KeyChar) != 'q') { }
    
                // Stop the sensor
                sensor.Stop();
            }
        }
    
        internal class Tracker
        {
            private Skeleton[] skeletons = null;
    
            public Tracker(KinectSensor sensor)
            {
                // Connect the skeleton frame handler and enable skeleton tracking
                sensor.SkeletonFrameReady += SensorSkeletonFrameReady;
                sensor.SkeletonStream.Enable();
            }
    
            private void SensorSkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
            {
                // Access the skeleton frame
                using (SkeletonFrame skeletonFrame = e.OpenSkeletonFrame())
                {
                    if (skeletonFrame != null)
                    {
                        if (this.skeletons == null)
                        {
                            // Allocate array of skeletons
                            this.skeletons = new Skeleton[skeletonFrame.SkeletonArrayLength];
                        }
    
                        // Copy skeletons from this frame
                        skeletonFrame.CopySkeletonDataTo(this.skeletons);
    
                        // Find first tracked skeleton, if any
                        Skeleton skeleton = this.skeletons.Where(s => s.TrackingState == SkeletonTrackingState.Tracked).FirstOrDefault();
    
                        if (skeleton != null)
                        {
                            // Obtain the left knee joint; if tracked, print its position
                            Joint j = skeleton.Joints[JointType.KneeLeft];
    
                            if (j.TrackingState == JointTrackingState.Tracked)
                            {
                                Console.WriteLine("Left knee: " + j.Position.X + ", " + j.Position.Y + ", " + j.Position.Z);
                            }
                        }
                    }
                }
            }
        }
    }
    

    This program will track skeletons and print out the position of the left knee of one skeleton, until you hit 'Q' on the keyboard.

    John
    K4W Dev

    Monday, March 19, 2012 7:17 PM

All replies

  • Give this a try:

    private Skeleton[] skeletons = null;
    
    private void SensorSkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
    {
        using (SkeletonFrame skeletonFrame = e.OpenSkeletonFrame())
        {
            if (skeletonFrame != null)
            {
                if (this.skeletons == null)
                {
                    this.skeletons = new Skeleton[skeletonFrame.SkeletonArrayLength];
                }
                skeletonFrame.CopySkeletonDataTo(this.skeletons);
                Skeleton firstTrackedSkeleton = this.skeletons.Where(s => s.TrackingState == SkeletonTrackingState.Tracked).FirstOrDefault();
                Joint j = skeleton.Joints[JointType.LeftKnee];
    
                if (j.TrackingState == JointTrackingState.Tracked)
                {
                    Console.WriteLine("Left knee: " + j.Position.X + ", " + j.Position.Y + ", " + j.Position.Z);
                }
            }
        }
    }
    

    John
    K4W Dev

    Wednesday, March 14, 2012 7:19 PM
  • By the way, the current Kinect SDK version is 1.0, not 2.0.
    • Proposed as answer by Siraj Suleman Friday, March 16, 2012 12:27 PM
    • Unproposed as answer by Siraj Suleman Saturday, September 22, 2012 10:38 PM
    Wednesday, March 14, 2012 7:27 PM
  • Hi,

    I cannot get VS to recognize "Where" in this.skeletons.Where, but otherwise it looks really good. Do you know why i´m having trouble with the Where? I´m new to c# and could use the help. And yes, it´s v1.0 not 2.0 :D. Thanks!

    Thursday, March 15, 2012 2:25 PM
  • another question... it's possible obtain real distancias between 2 joints? how?
    Thursday, March 15, 2012 3:15 PM
  • John,
    How would one call this method from the main body of code?

    Thursday, March 15, 2012 7:48 PM
  • You need to use namespace as displayed below

    using System.Linq;

    Friday, March 16, 2012 9:04 AM
  • I am using Visual Studio 210 and Kinect SDK 1.0. I tried out your above code but I  have an error  on the following line. Can you please tell me about it. Thanks in advance.

    Joint j = skeleton.Joints[JointType.LeftKnee];

    Friday, March 16, 2012 12:27 PM
  • It´s as simple as changing LeftKnee to KneeLeft. And thanks! I´m currently working on getting the coordinates printed in a window. I can post the code when done.
    Friday, March 16, 2012 12:37 PM
  • Thanks for your quick reply. But I have changed it to LeftKnee to KneeLeft. But actual problem it is not getting "skeleton" and giving me an error. Actually in the program "skeletons" is defined. So I changed it to skeletons but it is not working. As I am new to C# programming I am finding difficulties while solving the problem.

    Thanks in advance.

    Friday, March 16, 2012 1:46 PM
  • I added private void SensorSkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e, Skeleton skeleton, Dictionary<JointType, JointMapping> mappings) and then a reference using System.Linq; . But i have to say, even if i´m not getting any errors i still can´t get it to work, it doesn´t print out coorinates to the console..
    Friday, March 16, 2012 1:54 PM
  • Sorry, yeah, it's KneeLeft.
    Friday, March 16, 2012 8:01 PM
  • My original code was missing a check for when no skeletons are tracked yet. Try this:

                Skeleton firstTrackedSkeleton = this.skeletons.Where(s => s.TrackingState == SkeletonTrackingState.Tracked).FirstOrDefault();
                if (skeleton != null)
                {
                    Joint j = skeleton.Joints[JointType.KneeLeft];
    
                    if (j.TrackingState == JointTrackingState.Tracked)
                    {
                        Console.WriteLine("Left knee: " + j.Position.X + ", " + j.Position.Y + ", " + j.Position.Z);
                    }
                }
    

    John
    K4W Dev

    Friday, March 16, 2012 8:04 PM
  • It's not clear to me why you've changed the signature of the event handler. The handler should have only two parameters, as in the original code. Otherwise, you won't be able to add it as a handler of the sensor's SkeletonFrameReady event.

    John
    K4W Dev

    Friday, March 16, 2012 8:08 PM
  • If this is a console application, and we need main to get this thing to run, how do you call this function/method from main? When I try to compile it, I get a request for another namespace or E-O-F at the end of the code. What should be done? I guess I'm asking for the full monty in an example. 

    Friday, March 16, 2012 9:38 PM
  • OK, here it is, the full monty:

    namespace SkeletonExample
    {
        using System;
        using System.Linq;
        using Microsoft.Kinect;
    
        internal class Program
        {
            private static void Main(string[] args)
            {
                // Find the first connected sensor
                KinectSensor sensor = KinectSensor.KinectSensors.Where(s => s.Status == KinectStatus.Connected).FirstOrDefault();
                if (sensor == null)
                {
                    Console.WriteLine("No Kinect sensor found!");
                    return;
                }
    
                // Create object that will track skeletons using the sensor
                Tracker tracker = new Tracker(sensor);
    
                // Start the sensor
                sensor.Start();
    
                // Run until the user presses 'q' or 'Q' on the keyboard
                while (Char.ToLowerInvariant(Console.ReadKey().KeyChar) != 'q') { }
    
                // Stop the sensor
                sensor.Stop();
            }
        }
    
        internal class Tracker
        {
            private Skeleton[] skeletons = null;
    
            public Tracker(KinectSensor sensor)
            {
                // Connect the skeleton frame handler and enable skeleton tracking
                sensor.SkeletonFrameReady += SensorSkeletonFrameReady;
                sensor.SkeletonStream.Enable();
            }
    
            private void SensorSkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
            {
                // Access the skeleton frame
                using (SkeletonFrame skeletonFrame = e.OpenSkeletonFrame())
                {
                    if (skeletonFrame != null)
                    {
                        if (this.skeletons == null)
                        {
                            // Allocate array of skeletons
                            this.skeletons = new Skeleton[skeletonFrame.SkeletonArrayLength];
                        }
    
                        // Copy skeletons from this frame
                        skeletonFrame.CopySkeletonDataTo(this.skeletons);
    
                        // Find first tracked skeleton, if any
                        Skeleton skeleton = this.skeletons.Where(s => s.TrackingState == SkeletonTrackingState.Tracked).FirstOrDefault();
    
                        if (skeleton != null)
                        {
                            // Obtain the left knee joint; if tracked, print its position
                            Joint j = skeleton.Joints[JointType.KneeLeft];
    
                            if (j.TrackingState == JointTrackingState.Tracked)
                            {
                                Console.WriteLine("Left knee: " + j.Position.X + ", " + j.Position.Y + ", " + j.Position.Z);
                            }
                        }
                    }
                }
            }
        }
    }
    

    This program will track skeletons and print out the position of the left knee of one skeleton, until you hit 'Q' on the keyboard.

    John
    K4W Dev

    Monday, March 19, 2012 7:17 PM
  • Hi John,

    Which coordinate are these points based (i.e. where is the (0,0,0) and how to interpret the (x,y,z) in feet?

    Thanks!

    Friday, May 11, 2012 7:10 PM
  • I am tring to do the sme but with sdk v1.5 and the skeletonbasic. Is it possible?
    Thursday, June 28, 2012 12:40 PM
  • will you send me the project ? i cannot run it rsz_93@hotmail.com
    Wednesday, July 4, 2012 3:57 AM
  • Hi 

    I need code  Floor Determination by SDK 2.0

    For Get distance from Joint to Floor. 

    Help Me, 

    Sunday, July 30, 2017 2:00 AM