您好,登錄后才能下訂單哦!
骨骼數據不像前兩個數據,轉成字節數組,加載到圖片控件上。
本例可以從骨骼數據中獲取這6個人的點,再找到被完整跟蹤的人(最多兩個人)的20個關節點顯示在屏幕上。
新建一個 Winform項目。
然后寫如下代碼:
- KinectSensor kinectsensor = null;
- private void Form1_Shown(object sender, EventArgs e)
- {
- //從Kinect集合中找到連接上的Kinect
- foreach (KinectSensor ks in KinectSensor.KinectSensors)
- {
- //找到連接的Kinect
- if (ks.Status == KinectStatus.Connected)
- {
- kinectsensor = ks;
- //平滑參數
- TransformSmoothParameters smoothingParam = new TransformSmoothParameters();
- //調用骨骼流
- kinectsensor.SkeletonStream.Enable(smoothingParam);
- //設置更近模
- kinectsensor.DepthStream.Range = DepthRange.Near;
- //骨骼模式為更近模式
- kinectsensor.SkeletonStream.EnableTrackingInNearRange = true;
- //坐姿或站姿 坐姿中識別上半身的10個點,默認模式識別全身20個點
- //kinectsensor.SkeletonStream.TrackingMode = SkeletonTrackingMode.Seated;
- //訂閱骨骼識別事件
- kinectsensor.SkeletonFrameReady += kinectsensor_SkeletonFrameReady;
- kinectsensor.Start();//開始工作,即可以采集攝像頭和紅外攝像頭信息
- this.Text = "Kinect開始工作……";
- return;
- }
- }
- }
- //定義骨骼數組,因為一個Kinect最多可識別6個人,其中兩個人的骨骼的20個點可以識別,另4個人只識別成4個點
- Skeleton[] skeletonDataArr;
- void kinectsensor_SkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
- {
- this.Refresh();
- using (SkeletonFrame skeletonFrame = e.OpenSkeletonFrame()) // Open the Skeleton frame
- {
- //實例化骨骼數組
- this.skeletonDataArr = new Skeleton[kinectsensor.SkeletonStream.FrameSkeletonArrayLength];
- if (skeletonFrame != null && this.skeletonDataArr != null)
- {
- //復制數據到骨骼數組中
- skeletonFrame.CopySkeletonDataTo(this.skeletonDataArr);
- //設定人與人間的距離間隔
- int Distance = 0;
- //遍歷識別最多識別的6個人
- foreach (var skeletondata in skeletonDataArr)
- {
- Graphics gra = this.CreateGraphics();
- //設定放大步長為200
- int step = 200;
- //繪制6個人的位置
- gra.FillEllipse(new SolidBrush(Color.Blue), (skeletondata.Position.X + 1) * step + Distance, (1 - skeletondata.Position.Y) * step, 10, 10);
- #region 繪制被跟蹤到的完整的人
- if (skeletondata.TrackingState == SkeletonTrackingState.Tracked)
- {
- if (skeletondata.Joints.Count > 0)
- {
- //定義紅色的筆
- Pen pen = new Pen(Color.Red, 3f);
- //得到頭部聯合點
- Joint HeadJoint = skeletondata.Joints.SingleOrDefault(joi => joi.JointType == JointType.Head);
- //得到肩中聯合點
- Joint ShoulderCenterJoint = skeletondata.Joints.SingleOrDefault(joi => joi.JointType == JointType.ShoulderCenter);
- //畫頭和肩中的連線
- gra.DrawLine(pen, (HeadJoint.Position.X + 1) * step + Distance, (1 - HeadJoint.Position.Y) * step,
- (ShoulderCenterJoint.Position.X + 1) * step + Distance, (1 - ShoulderCenterJoint.Position.Y) * step);
- //得到左肩聯合點
- Joint ShoulderLeftJoint = skeletondata.Joints.SingleOrDefault(joi => joi.JointType == JointType.ShoulderLeft);
- //畫左肩和肩中的連線
- gra.DrawLine(pen, (ShoulderCenterJoint.Position.X + 1) * step + Distance, (1 - ShoulderCenterJoint.Position.Y) * step,
- (ShoulderLeftJoint.Position.X + 1) * step + Distance, (1 - ShoulderLeftJoint.Position.Y) * step);
- //得到右肩聯合點
- Joint ShoulderRightJoint = skeletondata.Joints.SingleOrDefault(joi => joi.JointType == JointType.ShoulderRight);
- //畫右肩和肩中的連線
- gra.DrawLine(pen, (ShoulderRightJoint.Position.X + 1) * step + Distance, (1 - ShoulderRightJoint.Position.Y) * step,
- (ShoulderCenterJoint.Position.X + 1) * step + Distance, (1 - ShoulderCenterJoint.Position.Y) * step);
- //得到左肘聯合點
- Joint ElbowLeftJoint = skeletondata.Joints.SingleOrDefault(joi => joi.JointType == JointType.ElbowLeft);
- //畫左肩和左肘的連線
- gra.DrawLine(pen, (ShoulderLeftJoint.Position.X + 1) * step + Distance, (1 - ShoulderLeftJoint.Position.Y) * step,
- (ElbowLeftJoint.Position.X + 1) * step + Distance, (1 - ElbowLeftJoint.Position.Y) * step);
- //得到右肘聯合點
- Joint ElbowRightJoint = skeletondata.Joints.SingleOrDefault(joi => joi.JointType == JointType.ElbowRight);
- //畫右肩和右肘的連線
- gra.DrawLine(pen, (ShoulderRightJoint.Position.X + 1) * step + Distance, (1 - ShoulderRightJoint.Position.Y) * step,
- (ElbowRightJoint.Position.X + 1) * step + Distance, (1 - ElbowRightJoint.Position.Y) * step);
- //得到左腕聯合點
- Joint WristLeftJoint = skeletondata.Joints.SingleOrDefault(joi => joi.JointType == JointType.WristLeft);
- //畫左肘和左腕的連線
- gra.DrawLine(pen, (WristLeftJoint.Position.X + 1) * step + Distance, (1 - WristLeftJoint.Position.Y) * step,
- (ElbowLeftJoint.Position.X + 1) * step + Distance, (1 - ElbowLeftJoint.Position.Y) * step);
- //得到右腕聯合點
- Joint WristRightJoint = skeletondata.Joints.SingleOrDefault(joi => joi.JointType == JointType.WristRight);
- //畫右肘和右腕的連線
- gra.DrawLine(pen, (WristRightJoint.Position.X + 1) * step + Distance, (1 - WristRightJoint.Position.Y) * step,
- (ElbowRightJoint.Position.X + 1) * step + Distance, (1 - ElbowRightJoint.Position.Y) * step);
- //得到左手聯合點
- Joint HandLeftJoint = skeletondata.Joints.SingleOrDefault(joi => joi.JointType == JointType.HandLeft);
- //畫左手和左腕的連線
- gra.DrawLine(pen, (WristLeftJoint.Position.X + 1) * step + Distance, (1 - WristLeftJoint.Position.Y) * step,
- (HandLeftJoint.Position.X + 1) * step + Distance, (1 - HandLeftJoint.Position.Y) * step);
- //得到右手聯合點
- Joint HandRightJoint = skeletondata.Joints.SingleOrDefault(joi => joi.JointType == JointType.HandRight);
- //畫右手和右腕的連線
- gra.DrawLine(pen, (WristRightJoint.Position.X + 1) * step + Distance, (1 - WristRightJoint.Position.Y) * step,
- (HandRightJoint.Position.X + 1) * step + Distance, (1 - HandRightJoint.Position.Y) * step);
- //得到脊柱聯合點
- Joint SpineJoint = skeletondata.Joints.SingleOrDefault(joi => joi.JointType == JointType.Spine);
- //畫脊柱和肩中的連線
- gra.DrawLine(pen, (SpineJoint.Position.X + 1) * step + Distance, (1 - SpineJoint.Position.Y) * step,
- (ShoulderCenterJoint.Position.X + 1) * step + Distance, (1 - ShoulderCenterJoint.Position.Y) * step);
- //得到髖中聯合點
- Joint HipCenterJoint = skeletondata.Joints.SingleOrDefault(joi => joi.JointType == JointType.HipCenter);
- //畫脊柱和髖中的連線
- gra.DrawLine(pen, (SpineJoint.Position.X + 1) * step + Distance, (1 - SpineJoint.Position.Y) * step,
- (HipCenterJoint.Position.X + 1) * step + Distance, (1 - HipCenterJoint.Position.Y) * step);
- //得到左髖聯合點
- Joint HipLeftJoint = skeletondata.Joints.SingleOrDefault(joi => joi.JointType == JointType.HipLeft);
- //畫左髖和髖中的連線
- gra.DrawLine(pen, (HipLeftJoint.Position.X + 1) * step + Distance, (1 - HipLeftJoint.Position.Y) * step,
- (HipCenterJoint.Position.X + 1) * step + Distance, (1 - HipCenterJoint.Position.Y) * step);
- //得到右髖聯合點
- Joint HipRightJoint = skeletondata.Joints.SingleOrDefault(joi => joi.JointType == JointType.HipRight);
- //畫右髖和髖中的連線
- gra.DrawLine(pen, (HipRightJoint.Position.X + 1) * step + Distance, (1 - HipRightJoint.Position.Y) * step,
- (HipCenterJoint.Position.X + 1) * step + Distance, (1 - HipCenterJoint.Position.Y) * step);
- //得到左膝聯合點
- Joint KneeLeftJoint = skeletondata.Joints.SingleOrDefault(joi => joi.JointType == JointType.KneeLeft);
- //畫左髖和左膝的連線
- gra.DrawLine(pen, (HipLeftJoint.Position.X + 1) * step + Distance, (1 - HipLeftJoint.Position.Y) * step,
- (KneeLeftJoint.Position.X + 1) * step + Distance, (1 - KneeLeftJoint.Position.Y) * step);
- //得到右膝聯合點
- Joint KneeRightJoint = skeletondata.Joints.SingleOrDefault(joi => joi.JointType == JointType.KneeRight);
- //畫右髖和右膝的連線
- gra.DrawLine(pen, (HipRightJoint.Position.X + 1) * step + Distance, (1 - HipRightJoint.Position.Y) * step,
- (KneeRightJoint.Position.X + 1) * step + Distance, (1 - KneeRightJoint.Position.Y) * step);
- //得到左踝聯合點
- Joint AnkleLeftJoint = skeletondata.Joints.SingleOrDefault(joi => joi.JointType == JointType.AnkleLeft);
- //畫左踝和左膝的連線
- gra.DrawLine(pen, (AnkleLeftJoint.Position.X + 1) * step + Distance, (1 - AnkleLeftJoint.Position.Y) * step,
- (KneeLeftJoint.Position.X + 1) * step + Distance, (1 - KneeLeftJoint.Position.Y) * step);
- //得到右踝聯合點
- Joint AnkleRightJoint = skeletondata.Joints.SingleOrDefault(joi => joi.JointType == JointType.AnkleRight);
- //畫右踝和右膝的連線
- gra.DrawLine(pen, (AnkleRightJoint.Position.X + 1) * step + Distance, (1 - AnkleRightJoint.Position.Y) * step,
- (KneeRightJoint.Position.X + 1) * step + Distance, (1 - KneeRightJoint.Position.Y) * step);
- //得到左腳聯合點
- Joint FootLeftJoint = skeletondata.Joints.SingleOrDefault(joi => joi.JointType == JointType.FootLeft);
- //畫左踝和左腳的連線
- gra.DrawLine(pen, (AnkleLeftJoint.Position.X + 1) * step + Distance, (1 - AnkleLeftJoint.Position.Y) * step,
- (FootLeftJoint.Position.X + 1) * step + Distance, (1 - FootLeftJoint.Position.Y) * step);
- //得到右腳聯合點
- Joint FootRightJoint = skeletondata.Joints.SingleOrDefault(joi => joi.JointType == JointType.FootRight);
- //畫右踝和右腳的連線
- gra.DrawLine(pen, (AnkleRightJoint.Position.X + 1) * step + Distance, (1 - AnkleRightJoint.Position.Y) * step,
- (FootRightJoint.Position.X + 1) * step + Distance, (1 - FootRightJoint.Position.Y) * step);
- }
- }
- #endregion
- //下一個人的位置往右偏200個像素
- Distance += 200;
- }
- }
- }
- }
- private void Form1_FormClosing(object sender, FormClosingEventArgs e)
- {
- if (kinectsensor.Status == KinectStatus.Connected)
- {
- kinectsensor.Stop();//結束Kinect采集工作
- MessageBox.Show("Kinect結束工作!");
- }
- }
效果如下:
我們看到屏幕上有6個藍色的點,代表可識別6個人,其中有兩個完整的人體骨骼,每個完整的人是有20個關節點的坐標。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。