十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
骨骼数据不像前两个数据,转成字节数组,加载到图片控件上。
创新互联建站主营安新网站建设的网络公司,主营网站建设方案,成都app软件开发公司,安新h5成都微信小程序搭建,安新网站营销推广欢迎安新等地区企业咨询
本例可以从骨骼数据中获取这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个关节点的坐标。