[# KINECTSDK] HowTo: Paint a skeleton

image

Buenas,

a couple of days ago I asked how to paint a skeleton with the new KinectSDK in this post. Today’s post explains the basics in a few steps to paint the skeleton.

For this example we will use a form of WPF, which add a Canvas brush where the skeleton.

   1: <Window x:Class="KinectSkeleton01.MainWindow"

   2:         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

   3:         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

   4:         Title="MainWindow" Height="480" Width="640">

   5:     <Grid>

   6:         <Canvas Name="skeletonCanvas" 

   7:                 Height="480" Width="640" 

   8:                 HorizontalAlignment="Center"/>

   9:     </Grid>

  10: </Window>

The following to keep in mind is to work with the sensor Kinect as a local variable of the form. In this post (link) talk a little in this regard.

Once controlled the State of the Kinect, the following is initialize the capture of skeleton (line 6 and 7) and subscribe to the event of change of frame for the skeleton (line 8).

In the implementation of this event, firstly we clean the canvas (line 14) and once validated the received frame (line 19) copy the array of skeletons to a local variable (line 21 and 22).

The concluding lines verify the State of the Joint of the head to see if tracking is correct and then use a helper of ElBruno.Kinect to paint the skeleton.

   1: void MainWindowLoaded(object sender, RoutedEventArgs e)

   2: {

   3:     if(KinectSensor.KinectSensors.Count == 0)

   4:         return;

   5:     _kinect = KinectSensor.KinectSensors[0];

   6:     _kinect.SkeletonStream.Enable();

   7:     _kinect.Start();

   8:     _kinect.SkeletonFrameReady += KinectSkeletonFrameReady;

   9: }

  10:  

  11: void KinectSkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)

  12: {

  13:     // Remove the old skeleton

  14:     skeletonCanvas.Children.Clear();

  15:     Skeleton[] skeletons = null;

  16:  

  17:     using (var frame = e.OpenSkeletonFrame())

  18:     {

  19:         if (frame != null)

  20:         {

  21:             skeletons = new Skeleton[frame.SkeletonArrayLength];

  22:             frame.CopySkeletonDataTo(skeletons);

  23:         }

  24:     }

  25:  

  26:     if (skeletons == null) return;

  27:  

  28:     foreach (var skeleton in skeletons)

  29:     {

  30:         if (skeleton.TrackingState != SkeletonTrackingState.Tracked) continue;

  31:         var headJoint = skeleton.Joints[JointType.Head];

  32:         if (headJoint.TrackingState != JointTrackingState.NotTracked)

  33:         {

  34:             var skeletonDraw = new SkeletonDraw();

  35:             skeletonDraw.DrawSkeleton(_kinect, skeletonCanvas, skeleton);

  36:         }

  37:     }

  38: }

The class responsible for painting the skeleton basically paints lines between each of the joints of the same. As we can see in the following code lines are painted between 2 points with the canvas and the sensor as benchmarks.

   1: void AddLine(KinectSensor kinectSensor, Canvas drawCanvas, Joint j1, Joint j2)

   2:  

   3:    var boneLine = new Line {Stroke = SkeletonBrush, StrokeThickness = 5};

   4:  

   5:    var j1P = kinectSensor.MapSkeletonPointToDepth(j1.Position, DepthImageFormat.Resolution640x480Fps30);

   6:    boneLine.X1 = j1P.X;

   7:    boneLine.Y1 = j1P.Y;

   8:  

   9:    DepthImagePoint j2P = kinectSensor.MapSkeletonPointToDepth(j2.Position, DepthImageFormat.Resolution640x480Fps30);

  10:    boneLine.X2 = j2P.X;

  11:    boneLine.Y2 = j2P.Y;

  12:  

  13:    drawCanvas.Children.Add(boneLine);

  14:  

  15:  

  16: ublic  float JointDistance(Joint first, Joint second)

  17:  

  18:    float dx = first.Position.X - second.Position.X;

  19:    float dy = first.Position.Y - second.Position.Y;

  20:    float dz = first.Position.Z - second.Position.Z;

  21:  

  22:    return (float)Math.Sqrt((dx * dx) + (dy * dy) + (dz * dz));

The sample code can be downloaded from

https://skydrive.live.com/redir.aspx?cid=bef06dffdb192125 & SPL = BEF06DFFDB192125! 3903 & parid = BEF06DFFDB192125! 1932 & authkey =!AKQC01rb-avYBVg

Saludos @ Home

El Bruno

image image image

Resources

http://elbruno.com/2012/03/06/kinectsdk-HOWTO-detectar-El-Cambio-de-Estado-o-desconexion-del-sensor/

About these ads

Deja un comentario

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión / Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión / Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión / Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión / Cambiar )

Conectando a %s