Kinect基本代码备忘

   //注意:1.安装Kinect SDK 1.0以上,
        //      2.引用Microsoft.Kinect.dll库
        private Graphics g;//界面画布对象 
        private KinectSensor kinect = null;//指向Kinect对象
        private Skeleton[] skeletonData;   //存储从Kinect传感器接收到的数据对象

       private void StartKinectST()
        {
            // Get first Kinect Sensor
            kinect = KinectSensor.KinectSensors.FirstOrDefault(s => s.Status == KinectStatus.Connected); 
           //注意:此判断MS目前无效
            if (null == kinect)
            {
                this.ShowDialog();
            }
            this.Text = "正在检测Kinect传感器...";
           // 允许骨骼跟踪
            kinect.SkeletonStream.Enable();
           //关闭颜色和景深数据的接收
            kinect.ColorStream.Disable();
            kinect.DepthStream.Disable(); 
            skeletonData = new Skeleton[kinect.SkeletonStream.FrameSkeletonArrayLength];
            // Get Ready for Skeleton Ready Events
            kinect.SkeletonFrameReady += new EventHandler(kinect_SkeletonFrameReady);
            // Start Kinect sensor 
            kinect.Start();
       }

        //接收到数据的处理事件
        private void kinect_SkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
        {
            try
            {

                ////Tracked that defines whether a skeleton is 'tracked' or not. 
                ////The untracked skeletons only give their position. 
                //if (SkeletonTrackingState.Tracked != data.TrackingState) continue;
                this.Text = "Kinect传感器连结成功!";

                using (SkeletonFrame skeletonFrame = e.OpenSkeletonFrame()) // Open the Skeleton frame
                {
                    // check that a frame is available
                    if (skeletonFrame != null && this.skeletonData != null) 
                    {
                        // get the skeletal information in this frame
                        skeletonFrame.CopySkeletonDataTo(this.skeletonData);

                    }
                }


                foreach (Skeleton skeleton in this.skeletonData)
                {
                    if (null != skeleton)
                    {
                        if (skeleton.TrackingState == SkeletonTrackingState.Tracked)
                        {
                            Console.WriteLine("KinectID: " + skeleton.TrackingId);
                            //只处理跟踪移动的数据
                            DrawTrackedSkeletonJoints(skeleton.Joints);
                        }
                        else if (skeleton.TrackingState == SkeletonTrackingState.PositionOnly)
                        {

                            //temDrawSkeletonPosition(skeleton.Position);
                        }
                    }
                    else
                    {
                        Console.WriteLine("null Skeleton");
                    }
                }
            }
            catch (Exception ex)
            {
                this.Text="Kinect失去连结,请检查USB或电源";
            }

        }

        //处理各个节点移动数据
        private void DrawTrackedSkeletonJoints(JointCollection jointCollection)
        {

         //   String s = kinect.UniqueKinectId;
            
            //以下为获取左右手、臂、膝盖、脚和头部的跟踪数据
            // Render Head and Shoulders
            //DrawBone(jointCollection[JointType.Head], jointCollection[JointType.ShoulderCenter]);
            //DrawBone(jointCollection[JointType.ShoulderCenter], jointCollection[JointType.ShoulderLeft]);
            //DrawBone(jointCollection[JointType.ShoulderCenter], jointCollection[JointType.ShoulderRight]);

            // Render Left Arm
            drarHand(true,jointCollection[JointType.HandLeft]);
            //DrawBone(jointCollection[JointType.ShoulderLeft], jointCollection[JointType.ElbowLeft]);
           //  DrawBone(jointCollection[JointType.ElbowLeft], jointCollection[JointType.WristLeft]);
            //DrawBone(jointCollection[JointType.WristLeft], jointCollection[JointType.HandLeft]);

            // Render Right Arm
             drarHand(false,jointCollection[JointType.HandRight]);
            //DrawBone(jointCollection[JointType.ShoulderRight], jointCollection[JointType.ElbowRight]);
           // DrawBone(jointCollection[JointType.ElbowRight], jointCollection[JointType.WristRight]);
            //DrawBone(jointCollection[JointType.WristRight], jointCollection[JointType.HandRight]);

            // Render other bones...
             
        }


        int srcXLeft, srcYLeft;
        int srcXRight, srcYRight;

       
        //画左右手的点
        private void drarHand(Boolean isLeft,Joint joint)
        {
            if (joint.TrackingState == JointTrackingState.Tracked &&
           joint.TrackingState == JointTrackingState.Tracked)
            {
                ColorImagePoint cp1 = kinect.MapSkeletonPointToColor(joint.Position, ColorImageFormat.RgbResolution640x480Fps30);
                Point po = new Point((int)cp1.X, (int)cp1.Y);
                Console.WriteLine("to draw Ellipse  x:" + po.X + " y:" + po.Y);

                if (isLeft)
                {
                    Pen srcP = new Pen(this.BackColor, 4);
                    g.DrawEllipse(srcP, srcXLeft, srcYLeft, 30,10);

                    Pen p = new Pen(Color.Red, 4);
                    g.DrawEllipse(p, po.X, po.Y, 30, 10);
                   
                    srcXLeft = po.X;
                    srcYLeft = po.Y;
                }
                else
                {
                    Pen srcP = new Pen(this.BackColor, 4);
                    g.DrawEllipse(srcP, srcXRight, srcYRight,20, 20);
                   

                    Pen p = new Pen(Color.Green, 4);
                    g.DrawEllipse(p, po.X, po.Y, 20, 20); 
                    srcXRight = po.X;
                    srcYRight = po.Y;
                   } 
            }
        } 
     
    }

 

你可能感兴趣的:(课业)