none
how to include SkeletonBasics example in my application RRS feed

  • Question

  • Hello,

    i want to draw the skeleton frame in a tab item like i did with the depth and color images.

    So i added the SkeletonBasics project to my solution and added a reference to it. 

    However, i can not call the SkeletonBasics functions directly.

    i tried :

    using namespace Microsoft.Samples.Kinect.SkeletonBasics 

     it does not work either ! 

    if i have to copy all methods of SkeletonBasics so what's the interest to add many projects in the same solution ?

    thanks for any help !

    regards 


    DKF

    Friday, March 13, 2015 1:51 PM

Answers

  • well.. after many attemps i solved the problem ! 

    i will drop the code below for those who need it. 

    i unfortunately copied the skeleton basics code on my application but there were many changes to do with the instanced objects, sometimes the sdk is confused between the references so i had to fix all the bugs ! ( i work with kinect sdk 1.7) 

    the skeleton frame was displayed in a tab item control as an image source named image3

    here is the code 

    public partial class MainWindow : Window
        {
    /// <summary>
            /// Drawing group for skeleton rendering output
            /// </summary>
            private DrawingGroup drawingGroup;
            /// <summary>
            /// Width of output drawing
            /// </summary>
            private const float RenderWidth = 640.0f;
            /// <summary>
            /// Height of our output drawing
            /// </summary>
            private const float RenderHeight = 480.0f;
            /// <summary>
            /// Brush used to draw skeleton center point
            /// </summary>
            private readonly System.Windows.Media.Brush centerPointBrush = System.Windows.Media.Brushes.Blue;
            /// <summary>
            /// Thickness of body center ellipse
            /// </summary>
            private const double BodyCenterThickness = 10;
            /// <summary>
            /// Thickness of clip edge rectangles
            /// </summary>
            private const double ClipBoundsThickness = 10;
            /// <summary>
            /// Brush used for drawing joints that are currently tracked
            /// </summary>
            private readonly System.Windows.Media.Brush trackedJointBrush = new SolidColorBrush(System.Windows.Media.Color.FromArgb(255, 68, 192, 68));
            /// <summary>
            /// Brush used for drawing joints that are currently inferred
            /// </summary>        
            private readonly System.Windows.Media.Brush inferredJointBrush = System.Windows.Media.Brushes.Yellow;
            /// <summary>
            /// Thickness of drawn joint lines
            /// </summary>
            private const double JointThickness = 3;
    
            /// <summary>
            /// Pen used for drawing bones that are currently tracked
            /// </summary>
            private readonly System.Windows.Media.Pen trackedBonePen = new System.Windows.Media.Pen(System.Windows.Media.Brushes.Green, 6);
            /// <summary>
            /// Pen used for drawing bones that are currently inferred
            /// </summary>        
            private readonly System.Windows.Media.Pen inferredBonePen = new System.Windows.Media.Pen(System.Windows.Media.Brushes.Gray, 1);
    
            /// <summary>
            /// Drawing image that we will display
            /// </summary>
            private DrawingImage imageSource;
    // other code for my application
    
    
     private void SensorChooserOnKinectChanged(object sender, KinectChangedEventArgs kinectChangedEventArgs)
            {
    KinectSensor oldSensor = kinectChangedEventArgs.OldSensor;
                KinectSensor newSensor = kinectChangedEventArgs.NewSensor;
                // param de transformation
                // Create the drawing group we'll use for drawing
                this.drawingGroup = new DrawingGroup();
    
                // Create an image source that we can use in our image control
                this.imageSource = new DrawingImage(this.drawingGroup);
    
                // Display the drawing using our image control
                image3.Source = this.imageSource;
    // other code to control kinect switch status case
    
    }
    
    private void KinectSensorOnAllFramesReady(object sender, AllFramesReadyEventArgs allFramesReadyEventArgs)
            {
    // other code for rgb region and depth region
    
      #region skeleton
    
    
                   // Skeleton[] skeletons = new Skeleton[0];
    
                    using (SkeletonFrame skeletonFrame = allFramesReadyEventArgs.OpenSkeletonFrame())
                {
                    if (skeletonFrame != null)
                    {
                        skeletons = new Skeleton[skeletonFrame.SkeletonArrayLength];
                        skeletonFrame.CopySkeletonDataTo(skeletons);
                    }
    
    
    
                    using (DrawingContext dc = this.drawingGroup.Open())
                    {
                        // Draw a transparent background to set the render size
                        dc.DrawRectangle(System.Windows.Media.Brushes.Black, null, new Rect(0.0, 0.0, RenderWidth, RenderHeight));
    
                        if (skeletons.Length != 0)
                        {
                            foreach (Skeleton skel in skeletons)
                            {
                                RenderClippedEdges(skel, dc);
    
                                if (skel.TrackingState == SkeletonTrackingState.Tracked)
                                {
                                    this.DrawBonesAndJoints(skel, dc);
                                }
                                else if (skel.TrackingState == SkeletonTrackingState.PositionOnly)
                                {
                                    dc.DrawEllipse(
                                    this.centerPointBrush,
                                    null,
                                    this.SkeletonPointToScreen(skel.Position),
                                    BodyCenterThickness,
                                    BodyCenterThickness);
    
                                }
                            }
                        }
    
                        // prevent drawing outside of our render area
                        this.drawingGroup.ClipGeometry = new RectangleGeometry(new Rect(0.0, 0.0, RenderWidth, RenderHeight));
                    }
    
                }
    
          #endregion
    
    
     //************************************************************************************************************
    
            //          skeleton basics methods
    
            //************************************************************************************************************
            /// <summary>
            /// Draws indicators to show which edges are clipping skeleton data
            /// </summary>
            /// <param name="skeleton">skeleton to draw clipping information for</param>
            /// <param name="drawingContext">drawing context to draw to</param>
            private static void RenderClippedEdges(Skeleton skeleton, DrawingContext drawingContext)
            {
                if (skeleton.ClippedEdges.HasFlag(FrameEdges.Bottom))
                {
                    drawingContext.DrawRectangle(
                        System.Windows.Media.Brushes.Red,
                        null,
                        new Rect(0, RenderHeight - ClipBoundsThickness, RenderWidth, ClipBoundsThickness));
                }
    
                if (skeleton.ClippedEdges.HasFlag(FrameEdges.Top))
                {
                    drawingContext.DrawRectangle(
                        System.Windows.Media.Brushes.Red,
                        null,
                        new Rect(0, 0, RenderWidth, ClipBoundsThickness));
                }
    
                if (skeleton.ClippedEdges.HasFlag(FrameEdges.Left))
                {
                    drawingContext.DrawRectangle(
                        System.Windows.Media.Brushes.Red,
                        null,
                        new Rect(0, 0, ClipBoundsThickness, RenderHeight));
                }
    
                if (skeleton.ClippedEdges.HasFlag(FrameEdges.Right))
                {
                    drawingContext.DrawRectangle(
                        System.Windows.Media.Brushes.Red,
                        null,
                        new Rect(RenderWidth - ClipBoundsThickness, 0, ClipBoundsThickness, RenderHeight));
                }
            }
    
            /// <summary>
            /// Draws a skeleton's bones and joints
            /// </summary>
            /// <param name="skeleton">skeleton to draw</param>
            /// <param name="drawingContext">drawing context to draw to</param>
            private void DrawBonesAndJoints(Skeleton skeleton, DrawingContext drawingContext)
            {
                // Render Torso
                this.DrawBone(skeleton, drawingContext, JointType.Head, JointType.ShoulderCenter);
                this.DrawBone(skeleton, drawingContext, JointType.ShoulderCenter, JointType.ShoulderLeft);
                this.DrawBone(skeleton, drawingContext, JointType.ShoulderCenter, JointType.ShoulderRight);
                this.DrawBone(skeleton, drawingContext, JointType.ShoulderCenter, JointType.Spine);
                this.DrawBone(skeleton, drawingContext, JointType.Spine, JointType.HipCenter);
                this.DrawBone(skeleton, drawingContext, JointType.HipCenter, JointType.HipLeft);
                this.DrawBone(skeleton, drawingContext, JointType.HipCenter, JointType.HipRight);
    
                // Left Arm
                this.DrawBone(skeleton, drawingContext, JointType.ShoulderLeft, JointType.ElbowLeft);
                this.DrawBone(skeleton, drawingContext, JointType.ElbowLeft, JointType.WristLeft);
                this.DrawBone(skeleton, drawingContext, JointType.WristLeft, JointType.HandLeft);
    
                // Right Arm
                this.DrawBone(skeleton, drawingContext, JointType.ShoulderRight, JointType.ElbowRight);
                this.DrawBone(skeleton, drawingContext, JointType.ElbowRight, JointType.WristRight);
                this.DrawBone(skeleton, drawingContext, JointType.WristRight, JointType.HandRight);
    
                // Left Leg
                this.DrawBone(skeleton, drawingContext, JointType.HipLeft, JointType.KneeLeft);
                this.DrawBone(skeleton, drawingContext, JointType.KneeLeft, JointType.AnkleLeft);
                this.DrawBone(skeleton, drawingContext, JointType.AnkleLeft, JointType.FootLeft);
    
                // Right Leg
                this.DrawBone(skeleton, drawingContext, JointType.HipRight, JointType.KneeRight);
                this.DrawBone(skeleton, drawingContext, JointType.KneeRight, JointType.AnkleRight);
                this.DrawBone(skeleton, drawingContext, JointType.AnkleRight, JointType.FootRight);
    
                // Render Joints
                foreach (Joint joint in skeleton.Joints)
                {
                    System.Windows.Media.Brush drawBrush = null;
    
                    if (joint.TrackingState == JointTrackingState.Tracked)
                    {
                        drawBrush = this.trackedJointBrush;
                    }
                    else if (joint.TrackingState == JointTrackingState.Inferred)
                    {
                        drawBrush = this.inferredJointBrush;
                    }
    
                    if (drawBrush != null)
                    {
                        drawingContext.DrawEllipse(drawBrush, null, this.SkeletonPointToScreen(joint.Position), JointThickness, JointThickness);
                    }
                }
            }
            /// <summary>
            /// Maps a SkeletonPoint to lie within our render space and converts to Point
            /// </summary>
            /// <param name="skelpoint">point to map</param>
            /// <returns>mapped point</returns>
            private Point SkeletonPointToScreen(SkeletonPoint skelpoint)
            {
                // Convert point to depth space.  
                // We are not using depth directly, but we do want the points in our 640x480 output resolution.
                DepthImagePoint depthPoint = this.sensorChooser.Kinect.CoordinateMapper.MapSkeletonPointToDepthPoint(skelpoint, DepthImageFormat.Resolution640x480Fps30);
                return new Point(depthPoint.X, depthPoint.Y);
            }
    
            /// Draws a bone line between two joints
            /// </summary>
            /// <param name="skeleton">skeleton to draw bones from</param>
            /// <param name="drawingContext">drawing context to draw to</param>
            /// <param name="jointType0">joint to start drawing from</param>
            /// <param name="jointType1">joint to end drawing at</param>
            private void DrawBone(Skeleton skeleton, DrawingContext drawingContext, JointType jointType0, JointType jointType1)
            {
                Joint joint0 = skeleton.Joints[jointType0];
                Joint joint1 = skeleton.Joints[jointType1];
    
                // If we can't find either of these joints, exit
                if (joint0.TrackingState == JointTrackingState.NotTracked ||
                    joint1.TrackingState == JointTrackingState.NotTracked)
                {
                    return;
                }
    
                // Don't draw if both points are inferred
                if (joint0.TrackingState == JointTrackingState.Inferred &&
                    joint1.TrackingState == JointTrackingState.Inferred)
                {
                    return;
                }
    
                // We assume all drawn bones are inferred unless BOTH joints are tracked
                System.Windows.Media.Pen drawPen = this.inferredBonePen;
                if (joint0.TrackingState == JointTrackingState.Tracked && joint1.TrackingState == JointTrackingState.Tracked)
                {
                    drawPen = this.trackedBonePen;
                }
    
                drawingContext.DrawLine(drawPen, this.SkeletonPointToScreen(joint0.Position), this.SkeletonPointToScreen(joint1.Position));
            }
    
     /************************************************************************************************************
    
            //          END skeleton basics methods
    
            //************************************************************************************************************/
    
    }
    

     i've got a skeleton image like this:



    DKF

    • Marked as answer by pink192y Tuesday, March 17, 2015 4:14 PM
    Tuesday, March 17, 2015 4:13 PM

All replies

  • copy and paste the code provided to ensure it renders correctly within your application.

    Carmine Sirignano - MSFT

    Monday, March 16, 2015 7:33 PM
  • Hello again ! 

    what if i want to include methods from other projects in the same solutions !

    i can't copy the  provided code all the time, besides some variables are have not the same name in my application and the skeleton basics example.. when i tried to include it in my application it was so long and messy and i  could not distinguish variables and what is messing .

    So i will be thankfull if you can  help me to ensure the right code ! 


    DKF


    • Edited by pink192y Tuesday, March 17, 2015 9:53 AM
    Tuesday, March 17, 2015 9:43 AM
  • well.. after many attemps i solved the problem ! 

    i will drop the code below for those who need it. 

    i unfortunately copied the skeleton basics code on my application but there were many changes to do with the instanced objects, sometimes the sdk is confused between the references so i had to fix all the bugs ! ( i work with kinect sdk 1.7) 

    the skeleton frame was displayed in a tab item control as an image source named image3

    here is the code 

    public partial class MainWindow : Window
        {
    /// <summary>
            /// Drawing group for skeleton rendering output
            /// </summary>
            private DrawingGroup drawingGroup;
            /// <summary>
            /// Width of output drawing
            /// </summary>
            private const float RenderWidth = 640.0f;
            /// <summary>
            /// Height of our output drawing
            /// </summary>
            private const float RenderHeight = 480.0f;
            /// <summary>
            /// Brush used to draw skeleton center point
            /// </summary>
            private readonly System.Windows.Media.Brush centerPointBrush = System.Windows.Media.Brushes.Blue;
            /// <summary>
            /// Thickness of body center ellipse
            /// </summary>
            private const double BodyCenterThickness = 10;
            /// <summary>
            /// Thickness of clip edge rectangles
            /// </summary>
            private const double ClipBoundsThickness = 10;
            /// <summary>
            /// Brush used for drawing joints that are currently tracked
            /// </summary>
            private readonly System.Windows.Media.Brush trackedJointBrush = new SolidColorBrush(System.Windows.Media.Color.FromArgb(255, 68, 192, 68));
            /// <summary>
            /// Brush used for drawing joints that are currently inferred
            /// </summary>        
            private readonly System.Windows.Media.Brush inferredJointBrush = System.Windows.Media.Brushes.Yellow;
            /// <summary>
            /// Thickness of drawn joint lines
            /// </summary>
            private const double JointThickness = 3;
    
            /// <summary>
            /// Pen used for drawing bones that are currently tracked
            /// </summary>
            private readonly System.Windows.Media.Pen trackedBonePen = new System.Windows.Media.Pen(System.Windows.Media.Brushes.Green, 6);
            /// <summary>
            /// Pen used for drawing bones that are currently inferred
            /// </summary>        
            private readonly System.Windows.Media.Pen inferredBonePen = new System.Windows.Media.Pen(System.Windows.Media.Brushes.Gray, 1);
    
            /// <summary>
            /// Drawing image that we will display
            /// </summary>
            private DrawingImage imageSource;
    // other code for my application
    
    
     private void SensorChooserOnKinectChanged(object sender, KinectChangedEventArgs kinectChangedEventArgs)
            {
    KinectSensor oldSensor = kinectChangedEventArgs.OldSensor;
                KinectSensor newSensor = kinectChangedEventArgs.NewSensor;
                // param de transformation
                // Create the drawing group we'll use for drawing
                this.drawingGroup = new DrawingGroup();
    
                // Create an image source that we can use in our image control
                this.imageSource = new DrawingImage(this.drawingGroup);
    
                // Display the drawing using our image control
                image3.Source = this.imageSource;
    // other code to control kinect switch status case
    
    }
    
    private void KinectSensorOnAllFramesReady(object sender, AllFramesReadyEventArgs allFramesReadyEventArgs)
            {
    // other code for rgb region and depth region
    
      #region skeleton
    
    
                   // Skeleton[] skeletons = new Skeleton[0];
    
                    using (SkeletonFrame skeletonFrame = allFramesReadyEventArgs.OpenSkeletonFrame())
                {
                    if (skeletonFrame != null)
                    {
                        skeletons = new Skeleton[skeletonFrame.SkeletonArrayLength];
                        skeletonFrame.CopySkeletonDataTo(skeletons);
                    }
    
    
    
                    using (DrawingContext dc = this.drawingGroup.Open())
                    {
                        // Draw a transparent background to set the render size
                        dc.DrawRectangle(System.Windows.Media.Brushes.Black, null, new Rect(0.0, 0.0, RenderWidth, RenderHeight));
    
                        if (skeletons.Length != 0)
                        {
                            foreach (Skeleton skel in skeletons)
                            {
                                RenderClippedEdges(skel, dc);
    
                                if (skel.TrackingState == SkeletonTrackingState.Tracked)
                                {
                                    this.DrawBonesAndJoints(skel, dc);
                                }
                                else if (skel.TrackingState == SkeletonTrackingState.PositionOnly)
                                {
                                    dc.DrawEllipse(
                                    this.centerPointBrush,
                                    null,
                                    this.SkeletonPointToScreen(skel.Position),
                                    BodyCenterThickness,
                                    BodyCenterThickness);
    
                                }
                            }
                        }
    
                        // prevent drawing outside of our render area
                        this.drawingGroup.ClipGeometry = new RectangleGeometry(new Rect(0.0, 0.0, RenderWidth, RenderHeight));
                    }
    
                }
    
          #endregion
    
    
     //************************************************************************************************************
    
            //          skeleton basics methods
    
            //************************************************************************************************************
            /// <summary>
            /// Draws indicators to show which edges are clipping skeleton data
            /// </summary>
            /// <param name="skeleton">skeleton to draw clipping information for</param>
            /// <param name="drawingContext">drawing context to draw to</param>
            private static void RenderClippedEdges(Skeleton skeleton, DrawingContext drawingContext)
            {
                if (skeleton.ClippedEdges.HasFlag(FrameEdges.Bottom))
                {
                    drawingContext.DrawRectangle(
                        System.Windows.Media.Brushes.Red,
                        null,
                        new Rect(0, RenderHeight - ClipBoundsThickness, RenderWidth, ClipBoundsThickness));
                }
    
                if (skeleton.ClippedEdges.HasFlag(FrameEdges.Top))
                {
                    drawingContext.DrawRectangle(
                        System.Windows.Media.Brushes.Red,
                        null,
                        new Rect(0, 0, RenderWidth, ClipBoundsThickness));
                }
    
                if (skeleton.ClippedEdges.HasFlag(FrameEdges.Left))
                {
                    drawingContext.DrawRectangle(
                        System.Windows.Media.Brushes.Red,
                        null,
                        new Rect(0, 0, ClipBoundsThickness, RenderHeight));
                }
    
                if (skeleton.ClippedEdges.HasFlag(FrameEdges.Right))
                {
                    drawingContext.DrawRectangle(
                        System.Windows.Media.Brushes.Red,
                        null,
                        new Rect(RenderWidth - ClipBoundsThickness, 0, ClipBoundsThickness, RenderHeight));
                }
            }
    
            /// <summary>
            /// Draws a skeleton's bones and joints
            /// </summary>
            /// <param name="skeleton">skeleton to draw</param>
            /// <param name="drawingContext">drawing context to draw to</param>
            private void DrawBonesAndJoints(Skeleton skeleton, DrawingContext drawingContext)
            {
                // Render Torso
                this.DrawBone(skeleton, drawingContext, JointType.Head, JointType.ShoulderCenter);
                this.DrawBone(skeleton, drawingContext, JointType.ShoulderCenter, JointType.ShoulderLeft);
                this.DrawBone(skeleton, drawingContext, JointType.ShoulderCenter, JointType.ShoulderRight);
                this.DrawBone(skeleton, drawingContext, JointType.ShoulderCenter, JointType.Spine);
                this.DrawBone(skeleton, drawingContext, JointType.Spine, JointType.HipCenter);
                this.DrawBone(skeleton, drawingContext, JointType.HipCenter, JointType.HipLeft);
                this.DrawBone(skeleton, drawingContext, JointType.HipCenter, JointType.HipRight);
    
                // Left Arm
                this.DrawBone(skeleton, drawingContext, JointType.ShoulderLeft, JointType.ElbowLeft);
                this.DrawBone(skeleton, drawingContext, JointType.ElbowLeft, JointType.WristLeft);
                this.DrawBone(skeleton, drawingContext, JointType.WristLeft, JointType.HandLeft);
    
                // Right Arm
                this.DrawBone(skeleton, drawingContext, JointType.ShoulderRight, JointType.ElbowRight);
                this.DrawBone(skeleton, drawingContext, JointType.ElbowRight, JointType.WristRight);
                this.DrawBone(skeleton, drawingContext, JointType.WristRight, JointType.HandRight);
    
                // Left Leg
                this.DrawBone(skeleton, drawingContext, JointType.HipLeft, JointType.KneeLeft);
                this.DrawBone(skeleton, drawingContext, JointType.KneeLeft, JointType.AnkleLeft);
                this.DrawBone(skeleton, drawingContext, JointType.AnkleLeft, JointType.FootLeft);
    
                // Right Leg
                this.DrawBone(skeleton, drawingContext, JointType.HipRight, JointType.KneeRight);
                this.DrawBone(skeleton, drawingContext, JointType.KneeRight, JointType.AnkleRight);
                this.DrawBone(skeleton, drawingContext, JointType.AnkleRight, JointType.FootRight);
    
                // Render Joints
                foreach (Joint joint in skeleton.Joints)
                {
                    System.Windows.Media.Brush drawBrush = null;
    
                    if (joint.TrackingState == JointTrackingState.Tracked)
                    {
                        drawBrush = this.trackedJointBrush;
                    }
                    else if (joint.TrackingState == JointTrackingState.Inferred)
                    {
                        drawBrush = this.inferredJointBrush;
                    }
    
                    if (drawBrush != null)
                    {
                        drawingContext.DrawEllipse(drawBrush, null, this.SkeletonPointToScreen(joint.Position), JointThickness, JointThickness);
                    }
                }
            }
            /// <summary>
            /// Maps a SkeletonPoint to lie within our render space and converts to Point
            /// </summary>
            /// <param name="skelpoint">point to map</param>
            /// <returns>mapped point</returns>
            private Point SkeletonPointToScreen(SkeletonPoint skelpoint)
            {
                // Convert point to depth space.  
                // We are not using depth directly, but we do want the points in our 640x480 output resolution.
                DepthImagePoint depthPoint = this.sensorChooser.Kinect.CoordinateMapper.MapSkeletonPointToDepthPoint(skelpoint, DepthImageFormat.Resolution640x480Fps30);
                return new Point(depthPoint.X, depthPoint.Y);
            }
    
            /// Draws a bone line between two joints
            /// </summary>
            /// <param name="skeleton">skeleton to draw bones from</param>
            /// <param name="drawingContext">drawing context to draw to</param>
            /// <param name="jointType0">joint to start drawing from</param>
            /// <param name="jointType1">joint to end drawing at</param>
            private void DrawBone(Skeleton skeleton, DrawingContext drawingContext, JointType jointType0, JointType jointType1)
            {
                Joint joint0 = skeleton.Joints[jointType0];
                Joint joint1 = skeleton.Joints[jointType1];
    
                // If we can't find either of these joints, exit
                if (joint0.TrackingState == JointTrackingState.NotTracked ||
                    joint1.TrackingState == JointTrackingState.NotTracked)
                {
                    return;
                }
    
                // Don't draw if both points are inferred
                if (joint0.TrackingState == JointTrackingState.Inferred &&
                    joint1.TrackingState == JointTrackingState.Inferred)
                {
                    return;
                }
    
                // We assume all drawn bones are inferred unless BOTH joints are tracked
                System.Windows.Media.Pen drawPen = this.inferredBonePen;
                if (joint0.TrackingState == JointTrackingState.Tracked && joint1.TrackingState == JointTrackingState.Tracked)
                {
                    drawPen = this.trackedBonePen;
                }
    
                drawingContext.DrawLine(drawPen, this.SkeletonPointToScreen(joint0.Position), this.SkeletonPointToScreen(joint1.Position));
            }
    
     /************************************************************************************************************
    
            //          END skeleton basics methods
    
            //************************************************************************************************************/
    
    }
    

     i've got a skeleton image like this:



    DKF

    • Marked as answer by pink192y Tuesday, March 17, 2015 4:14 PM
    Tuesday, March 17, 2015 4:13 PM