locked
Kinect SDK 1.7 Skeleton Tracking RRS feed

  • Question

  • Hello everyone,

    I'm doing a project to university and i'm dealing with the problem of displaying the skeleton image on a picture box.

    Is there any way do display the skeleton points and bones on a windows form application c# to a picturebox or to other component usable in a windows form application C#?!?

    Everything i find is to WPF and i need to put it in a windows form aplication....

    Thursday, September 12, 2013 6:11 PM

Answers

  • So you are wanting to 'draw' just the skeleton right?  (vs displaying the color/depth stream image)? 

    To draw the skeleton in a WinForms app - I've found it easiest to throw a Panel on a windows form, and then draw the skeleton in the panel_paint event (creating a 'Skeleton' property - and setting that skeleton property in the sensors _AllFramesReady or _SkeletonFrameReady event, then if the skeleton is populated in the panel _paint event, draw it using a GraphicsPath object)

    Here is some really rough code (it may not even function as is) that I created to display a skeleton so I could see that my sensor was seeing the user correctly. Im sure there are more elegant ways to do it (this is put together using a combination of things I've found on the internet), but should give you the jist of what I did to display the skeleton on a WinForm form. (now if you want to overlay the skeleton onto the color/depth stream in WinForms - that gets a bit more complicated)

            private Skeleton[] skeletons = new Skeleton[0];
    
            private Skeleton _skeleton;
            public Skeleton Skeleton
            {
                get
                {
                    return _skeleton;
                }
                set
                {
                    _skeleton = value;
                    this.panel1.Refresh();
                }
            }
    
            void sensor_SkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
            {
                // Get the frame.
                using (var frame = e.OpenSkeletonFrame())
                {
                    // Ensure we have a frame.
                    if (frame != null)
                    {
                        // Resize the skeletons array if a new size (normally only on first call).
                        if (this.skeletons.Length != frame.SkeletonArrayLength)
                        {
                            this.skeletons = new Skeleton[frame.SkeletonArrayLength];
                        }
    
                        // Get the skeletons.
                        frame.CopySkeletonDataTo(this.skeletons);
    
                        // Look through the skeletons.
                        foreach (var skeleton in this.skeletons)
                        {
                            // Only consider tracked skeletons.
                            if (skeleton.TrackingState == SkeletonTrackingState.Tracked)
                            {
                                this.Skeleton = skeleton;
                            }
                        }
    
                    }
                }
            }
    
            private void panel1_Paint(object sender, PaintEventArgs e)
            {
                Graphics g = e.Graphics;
                g.Clear(System.Drawing.Color.White);
    
                if (this.Skeleton == null)
                    return;
    
                Pen skeletonPen = new Pen(sd.Color.Bisque, 3);
                Pen headPen = new Pen(sd.Color.Black, 2);
                Pen handPen = new Pen(sd.Color.Blue, 2);
    
                GraphicsPath skeletonGP = new GraphicsPath();
    
                JointCollection joints = this.Skeleton.Joints;
    
                System.Drawing.Size defaultSize = new System.Drawing.Size(5, 5);
    
                AddBone(skeletonGP, joints[JointType.Head], joints[JointType.ShoulderCenter], defaultSize);
    
                AddBone(skeletonGP, joints[JointType.ShoulderCenter], joints[JointType.ShoulderRight], defaultSize);
                AddBone(skeletonGP, joints[JointType.ShoulderRight], joints[JointType.ElbowRight], defaultSize);
                AddBone(skeletonGP, joints[JointType.ElbowRight], joints[JointType.WristRight], defaultSize);
                AddBone(skeletonGP, joints[JointType.WristRight], joints[JointType.HandRight], defaultSize);
    
                AddBone(skeletonGP, joints[JointType.ShoulderCenter], joints[JointType.ShoulderLeft], defaultSize);
                AddBone(skeletonGP, joints[JointType.ShoulderLeft], joints[JointType.ElbowLeft], defaultSize);
                AddBone(skeletonGP, joints[JointType.ElbowLeft], joints[JointType.WristLeft], defaultSize);
                AddBone(skeletonGP, joints[JointType.WristLeft], joints[JointType.HandLeft], defaultSize);
    
                AddBone(skeletonGP, joints[JointType.ShoulderCenter], joints[JointType.Spine], defaultSize);
                AddBone(skeletonGP, joints[JointType.Spine], joints[JointType.HipCenter], defaultSize);
    
                AddBone(skeletonGP, joints[JointType.ShoulderLeft], joints[JointType.HipLeft], defaultSize);
                AddBone(skeletonGP, joints[JointType.ShoulderRight], joints[JointType.HipRight], defaultSize);
    
                AddBone(skeletonGP, joints[JointType.HipCenter], joints[JointType.HipRight], defaultSize);
                AddBone(skeletonGP, joints[JointType.HipRight], joints[JointType.KneeRight], defaultSize);
                AddBone(skeletonGP, joints[JointType.KneeRight], joints[JointType.AnkleRight], defaultSize);
                AddBone(skeletonGP, joints[JointType.AnkleRight], joints[JointType.FootRight], defaultSize);
    
                AddBone(skeletonGP, joints[JointType.HipCenter], joints[JointType.HipLeft], defaultSize);
                AddBone(skeletonGP, joints[JointType.HipLeft], joints[JointType.KneeLeft], defaultSize);
                AddBone(skeletonGP, joints[JointType.KneeLeft], joints[JointType.AnkleLeft], defaultSize);
                AddBone(skeletonGP, joints[JointType.AnkleLeft], joints[JointType.FootLeft], defaultSize);
    
                GraphicsPath headGP = new GraphicsPath();
                AddJoint(headGP, joints[JointType.Head], new System.Drawing.Size(20, 20));
    
    
                g.DrawPath(skeletonPen, skeletonGP);
                skeletonGP.Dispose();
                g.DrawPath(headPen, headGP);
                headGP.Dispose();
    
                skeletonPen.Dispose();
                handPen.Dispose();
                headPen.Dispose();
            }
    
            private void AddBone(GraphicsPath gp, Joint joint1, Joint joint2, System.Drawing.Size jointSize)
            {
                AddJoint(gp, joint1, jointSize);
                AddJoint(gp, joint2, jointSize);
                if (joint1.TrackingState != JointTrackingState.NotTracked && joint2.TrackingState != JointTrackingState.NotTracked)
                {
                    gp.AddLine(JointToPoint(joint1.Position), JointToPoint(joint2.Position));
                }
            }
    
            private void AddJoint(GraphicsPath gp, Joint joint1, System.Drawing.Size jointSize)
            {
                if (joint1.TrackingState != JointTrackingState.NotTracked)
                {
                    System.Drawing.Point p = JointToPoint(joint1.Position);
                    p.Offset(-jointSize.Width / 2, -jointSize.Height / 2);
                    gp.AddEllipse(new RectangleF(p, jointSize));
                }
            }
    
            private System.Drawing.Point JointToPoint(SkeletonPoint sp)
            {
                DepthImagePoint dip = this.sensor.MapSkeletonPointToDepth(sp, DepthImageFormat.Resolution640x480Fps30);
                int xPos = (int)((dip.X / 640f) * this.panel1.DisplayRectangle.Width);
                int yPos = (int)((dip.Y / 480f) * this.panel1.DisplayRectangle.Height);
    
                return new System.Drawing.Point(xPos, yPos);
            }
    
    

    Thursday, September 12, 2013 7:10 PM