none
what can't obtain skeleton in polling model ? RRS feed

  • Question

  • hi, guys.

    using WPF and polling-model.

    I have a problem which to obtain hipcenter of skeleton.

    I want to read position values of HipCenter1 in my code, but i can't get the data.

    The data have trackID, but have not the skeleton information of  trackID

    I don't know how to do.

    Any suggestions are good.

    code:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    using Microsoft.Kinect;
    using System.IO.Ports;
    using System.Threading;
    
    
    namespace BeginningKinect.Chapter2.Polling
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
    
            //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
            private ColorImageStream colorStream;
            private KinectSensor _Kinect;
    
            private WriteableBitmap _ColorImageBitmap;
            private Int32Rect _ColorImageBitmapRect;
            private int _ColorImageStride;
            private byte[] _ColorImagePixelData;
            private byte[] _ColorImagePixelData1;
            private WriteableBitmap _ColorImageBitmap1;
            private Int32Rect _ColorImageBitmapRect1;
            private int _ColorImageStride1;
            //     
    
            private Skeleton[] skeletonData;
            private Skeleton[] skeletonData1;
            //
    
            private int mark;
            //
    
            private bool switch_1 = false;
            //
    
            private float Hip_X, Hip_G,Hip_NEW_G,Hip_ERROR_G;
            private float Hip_Y, Hip_B, Hip_NEW_B, Hip_ERROR_B;
            private float Hip_Z, Hip_R, Hip_NEW_R, Hip_ERROR_R;
            //private double total, scalar = 0,scalar1 = 0;
            //private float x, y, z;
            private int closestID = 0;
            int memory_1, memory_2;
            int count = 0;
            //
    
    
            #region 
            //private WriteableBitmap _DepthImageBitmap;
            //private Int32Rect _DepthImageBitmapRect;
            //private int _DepthImageStride;
            //private short[] _DepthImagePixelData;
            //
            #endregion
            //
            System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
    
            //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
            public MainWindow()
            {
                InitializeComponent();
    
    
                
                
                CompositionTarget.Rendering += CompositionTarget_Rendering;
            }
    
    
            private void CompositionTarget_Rendering(object sender, EventArgs e)
            {
                DiscoverKinectSensor();
                PollStream();
                textBox3.Text = Convert.ToString(sw.ElapsedTicks);
                
            }
            //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
            private void DiscoverKinectSensor()
    
            {
    
                if (this._Kinect != null && this._Kinect.Status != KinectStatus.Connected)
                {
                    this._Kinect = null;
                }
    
                if (this._Kinect == null)
                {
                    this._Kinect = KinectSensor.KinectSensors.FirstOrDefault(x => x.Status == KinectStatus.Connected);
                    
                    #region
                    if (this._Kinect != null)
                    {
                        this._Kinect.ColorStream.Enable();
                        this._Kinect.SkeletonStream.Enable();
                        this._Kinect.Start();
    
                        colorStream = this._Kinect.ColorStream;
                        this._ColorImageBitmap = new WriteableBitmap(colorStream.FrameWidth, colorStream.FrameHeight, 96, 96, PixelFormats.Bgr32, null);
                        this._ColorImageBitmapRect = new Int32Rect(0, 0, colorStream.FrameWidth, colorStream.FrameHeight);
                        this._ColorImageStride = colorStream.FrameWidth * colorStream.FrameBytesPerPixel;
                        this.Color_Image.Source = this._ColorImageBitmap;
    
                        this._ColorImageBitmap1 = new WriteableBitmap(colorStream.FrameWidth, colorStream.FrameHeight, 96, 96, PixelFormats.Bgr32, null);
                        this._ColorImageBitmapRect1 = new Int32Rect(0, 0, colorStream.FrameWidth, colorStream.FrameHeight);
                        this._ColorImageStride1 = colorStream.FrameWidth * colorStream.FrameBytesPerPixel;
                    }
                    #endregion
                    //
                    
                    #region 
                    //if (this._Kinect != null)
                    //{
                    //    this._Kinect.DepthStream.Enable(DepthImageFormat.Resolution640x480Fps30);
                    //    this._Kinect.Start();                
                    //
                    //    DepthImageStream depthStream    = this._Kinect.DepthStream;
                    //    this._DepthImageBitmap = new WriteableBitmap(depthStream.FrameWidth, depthStream.FrameHeight, 96, 96, PixelFormats.Gray16, null);
                    //    this._DepthImageBitmapRect = new Int32Rect(0, 0, depthStream.FrameWidth, depthStream.FrameHeight);
                    //    this._DepthImageStride = depthStream.FrameWidth * depthStream.FrameBytesPerPixel;
                    //    this.DepthImageElement.Source = this._DepthImageBitmap;
                    //    this._DepthImagePixelData = new short[depthStream.FramePixelDataLength];                                
                    //}
    
                    //if (this._Kinect != null)
                    //{
                    //    this._Kinect.SkeletonStream.Enable();
                    //    this._Kinect.Start();  
                    //}
                    #endregion
                    //
                }
            }
    
    
            private void PollStream()
            {
                if (this._Kinect == null)
                {
                    //TODO: Display a message to plug-in a Kinect.
                }
                else
                {
    
                    
                    #region
                    using (ColorImageFrame frame = this._Kinect.ColorStream.OpenNextFrame(100))
                    {
    
                        if (frame != null)
                        {
                            this._ColorImagePixelData = new byte[colorStream.FramePixelDataLength];
                            frame.CopyPixelDataTo(this._ColorImagePixelData);
                            
                            this._ColorImageBitmap.WritePixels(this._ColorImageBitmapRect, this._ColorImagePixelData, this._ColorImageStride, 0);
                            //
                        }
                    }
                    #endregion
    
    
                    
                    #region
                    //using(DepthImageFrame frame = this._Kinect.DepthStream.OpenNextFrame(100))
                    //{
                    //    if(frame != null)
                    //    {                            
                    //        frame.CopyPixelDataTo(this._DepthImagePixelData);
                    //        
                    //        this._DepthImageBitmap.WritePixels(this._DepthImageBitmapRect, this._DepthImagePixelData, this._DepthImageStride, 0);      
                    //        //
                    //    }
                    //}
                    #endregion
    
    
    
                    using (SkeletonFrame frame = this._Kinect.SkeletonStream.OpenNextFrame(100))
                    {
    
                        if (frame != null)
                        {
                            skeletonData = new Skeleton[_Kinect.SkeletonStream.FrameSkeletonArrayLength];
                            frame.CopySkeletonDataTo(this.skeletonData);
    
    
                            if (this._Kinect != null && this._Kinect.SkeletonStream != null)
                            {
                                if (!this._Kinect.SkeletonStream.AppChoosesSkeletons)
                                {
                                    this._Kinect.SkeletonStream.AppChoosesSkeletons = true;
                                }
    
                                float closestDistance = 10000f;
                                count = 0;
    
                                foreach (Skeleton skeleton in this.skeletonData.Where(s => s.TrackingState != SkeletonTrackingState.NotTracked))
                                {
                                    count = count + 1;
                                    if (count == 1)
                                    {
                                        memory_1 = skeleton.TrackingId;
                                    }
                                    if (count == 2)
                                    {
                                        memory_2 = skeleton.TrackingId;
                                    }
                                }
    
                                foreach (Skeleton skeleton1 in this.skeletonData.Where(s => s.TrackingState != SkeletonTrackingState.NotTracked))
                                {
                                    if (skeleton1.Position.Z < closestDistance && switch_1 == false)
                                    {
                                        closestID = skeleton1.TrackingId;
                                        closestDistance = skeleton1.Position.Z;
                                    }
                                }
    
                                if (memory_1 != closestID && switch_1 == true)
                                {
    
                                    using (ColorImageFrame frame1 = this._Kinect.ColorStream.OpenNextFrame(100))
                                    {
                                        if (frame1 != null)
                                        {
                                            colorStream = this._Kinect.ColorStream;
    
                                            this._ColorImagePixelData1 = new byte[colorStream.FramePixelDataLength];
                                            frame1.CopyPixelDataTo(_ColorImagePixelData1);
    
                                            this._ColorImageBitmap1.WritePixels(this._ColorImageBitmapRect1, this._ColorImagePixelData1, this._ColorImageStride1, 0);
                                            this.Color_Image2.Source = this._ColorImageBitmap1;
    
                                        }
                                    }
    
                                    using (SkeletonFrame frame2 = this._Kinect.SkeletonStream.OpenNextFrame(1000))
                                    {
                                        if (frame2 != null)
                                        {
    
                                            
                                            skeletonData1 = new Skeleton[_Kinect.SkeletonStream.FrameSkeletonArrayLength];
                                            frame2.CopySkeletonDataTo(this.skeletonData1);
    
                                            foreach (Skeleton skeleton4 in this.skeletonData1.Where(s => s.TrackingState != SkeletonTrackingState.NotTracked))
                                            {
    
                                                var HipCenter1 = skeleton4.Joints[JointType.HipCenter].Position;
    
    
                                                ColorImagePoint colorImagePoint1 = _Kinect.CoordinateMapper.MapSkeletonPointToColorPoint(HipCenter1, ColorImageFormat.RgbResolution640x480Fps30);
                                                //
                                                textBox1.Text = Convert.ToString(colorImagePoint1.X) + "," + Convert.ToString(colorImagePoint1.Y) + "," +  Convert.ToString(skeleton4.TrackingId);
                                                //
                                                if (colorImagePoint1.X <= 608 && 0 <= colorImagePoint1.X && 0 <= colorImagePoint1.Y && colorImagePoint1.Y <= 306)
                                                {
                                                    Hip_NEW_B = _ColorImagePixelData1[colorImagePoint1.X * 4 + ((colorImagePoint1.Y - 1) * 640 * 4)];
                                                    Hip_NEW_G = _ColorImagePixelData1[colorImagePoint1.X * 4 + ((colorImagePoint1.Y - 1) * 640 * 4) + 1];
                                                    Hip_NEW_R = _ColorImagePixelData1[colorImagePoint1.X * 4 + ((colorImagePoint1.Y - 1) * 640 * 4) + 2];
    
                                                }
    
                                                Hip_ERROR_G = Math.Abs(Hip_NEW_G - Hip_G);
                                                Hip_ERROR_B = Math.Abs(Hip_NEW_B - Hip_B);
                                                Hip_ERROR_R = Math.Abs(Hip_NEW_R - Hip_R);
    
    
                                                if (Hip_ERROR_G <= 30 && Hip_ERROR_B <= 30 && Hip_ERROR_R <= 30)
                                                {
                                                    closestID = skeleton4.TrackingId;
                                                    this._Kinect.SkeletonStream.ChooseSkeletons(closestID);
                                                    //textBox1.Text = "sucessful";
                                                }
                                            }
    
                                        }
                                    }
    
    
    
    
    
                                }
    
                                if (switch_1 == false && count == 1)
                                {
                                    sw.Start();
                                }
                                if (closestID != 0)
                                {
                                    switch_1 = true;
                                }
    
    
                                if (closestID > 0)
                                {
                                    this._Kinect.SkeletonStream.ChooseSkeletons(closestID); // Track this skeleton
                                }
    
    
                                Skeleton skeleton3 = (from s in skeletonData
                                                      where s.TrackingState == SkeletonTrackingState.Tracked
                                                      select s).FirstOrDefault();
    
                                if (skeleton3 != null)
                                {
    
                                    var HipCenter = skeleton3.Joints[JointType.HipCenter];
                                    //var HipRight = skeleton3.Joints[JointType.HipRight];
                                    //var HipLeft = skeleton3.Joints[JointType.HipLeft];
                                    //var handright = skeleton3.Joints[JointType.HandRight];
                                    //var handleft = skeleton3.Joints[JointType.HandLeft];
    
    
                                    SetPointPosition(hipPoint, HipCenter);
                                    //SetPointPosition(hiprightPoint, HipRight);
                                    //SetPointPosition(hipleftPoint, HipLeft);
                                    //SetPointPosition(handrightPoint, handright);
                                    //SetPointPosition(handleftPoint, handleft);
    
                                    Hip_X = HipCenter.Position.X;
                                    Hip_Y = HipCenter.Position.Y;
                                    Hip_Z = HipCenter.Position.Z;
    
                                    textBox2.Text = Hip_R + "," + Hip_G + "," + Hip_B;
                                    textBox4.Text = Hip_ERROR_B + "," + Hip_ERROR_G + "," + Hip_ERROR_R;
                                }
                            }
    
    
    
                        }
                    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
                }
            }
    
            private void SetPointPosition(FrameworkElement ellipse, Joint joint)
            {
    
                ColorImagePoint colorImagePoint = _Kinect.CoordinateMapper.MapSkeletonPointToColorPoint(joint.Position, ColorImageFormat.RgbResolution640x480Fps30);
                Canvas.SetLeft(ellipse, colorImagePoint.X);
                Canvas.SetTop(ellipse, colorImagePoint.Y);
    
                if (colorImagePoint.X <= 608 && 0 <= colorImagePoint.X && 0 <= colorImagePoint.Y && colorImagePoint.Y <= 306 && sw.ElapsedTicks>= 5000000)
                {
                Hip_B = _ColorImagePixelData[colorImagePoint.X * 4 + ((colorImagePoint.Y - 1) * 640 * 4)];
                Hip_G = _ColorImagePixelData[colorImagePoint.X * 4 + ((colorImagePoint.Y - 1) * 640 * 4) + 1];
                Hip_R = _ColorImagePixelData[colorImagePoint.X * 4 + ((colorImagePoint.Y - 1) * 640 * 4) + 2];
                sw.Stop();   
                sw.Reset();
    
                }
                
            }
    Monday, December 15, 2014 8:48 AM