骨骼流与以往的色彩与深度流不同,他需要下载一个辅助类库,下载完之后还要添加这个引用,再加using 。
这个代码是在原来彩色流的基础上写的。
1.首先准备骨骼流启动函数及相应的事件
2.准备wpf界面,添加20个小圆点并标记为不同的颜色
3.连接好设备,出发事件处理函数
4.骨骼点位置坐标的缩放
5.下载类库,添加引用
6.将骨骼点坐标映射到ellipse控件的left和top属性上
7.poly表示骨架线,遍历所有骨骼点并分类储存相应的点
8.运行程序
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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 Coding4Fun.Kinect.Wpf;
namespace 骨骼追踪数据的处理方法
{
///
/// MainWindow.xaml 的交互逻辑
///
public partial class MainWindow : Window
{
KinectSensor kinectSensor;
private byte[] pixelData;
Skeleton[] skeletonData;
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
kinectSensor = (from sensor in KinectSensor.KinectSensors
where sensor.Status == //寻找设备
KinectStatus.Connected
select sensor).FirstOrDefault();
kinectSensor = KinectSensor.KinectSensors[0];
kinectSensor.SkeletonStream.Enable();
kinectSensor.ColorStream.Enable();
// kinectSensor.ColorStream.Enable(ColorImageFormat.InfraredResolution640x480Fps30);//修改即可得到红外图//启动
kinectSensor.ColorFrameReady += kinectSensor_ColorFrameReady;//调用函数
// kinectSensor.SkeletonStream.TrackingMode = SkeletonTrackingMode.Seated;
kinectSensor.SkeletonFrameReady += kinectSensor_SkeletonFrameReady;
kinectSensor.Start();
/* foreach (Joint joint in skeleton.Joints)
{
Point jointPoint = GetDisplayPosition(joint);
switch (joint.JointType)
{
case JointType.Head:
SetPointPosition(headPoint, joint);
headPolyline.Points.Add(jointPoint);
break;
....
}
}
* */
}
private void kinectSensor_ColorFrameReady(object sender, ColorImageFrameReadyEventArgs e)//e是一个参数,前面是一个参数名称
{
using (ColorImageFrame imageFrame = e.OpenColorImageFrame())//调用方法
{
if (imageFrame != null)
{
this.pixelData = new byte[imageFrame.PixelDataLength];//定义数组
imageFrame.CopyPixelDataTo(this.pixelData);//将数据复制到数组中
this.ColorImage.Source = BitmapSource.Create(imageFrame.Width, imageFrame.Height, 96, 96,
PixelFormats.Bgr32, null, pixelData, imageFrame.Width * imageFrame.BytesPerPixel);//创建bit。。。
}
}
}
private void kinectSensor_SkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
{
using (SkeletonFrame skeletonFrame = e.OpenSkeletonFrame())
{
if (skeletonFrame != null)
{
skeletonData = new Skeleton[kinectSensor.SkeletonStream.FrameSkeletonArrayLength];
skeletonFrame.CopySkeletonDataTo(this.skeletonData);
Skeleton skeleton = (from s in skeletonData where s.TrackingState == SkeletonTrackingState.Tracked select s).FirstOrDefault();
if (skeleton != null)
{
SetAllPointPosition(skeleton);
}
}
}
}
// Joint headJoint = (from j in skeleton.Joints where j.JointType == JointType.Head select j).FirstOrDefault();
//为了将20 个点更好的显示出来,对position的x与y进行缩放
private Point GetDisplayPosition(Joint joint)
{
var scaledJoint = joint.ScaleTo(640, 480);
return new Point(scaledJoint.Position.X, scaledJoint.Position.Y);
}
//编写一个函数,将每个骨骼点转换后的坐标值分别映射到相应的控件属性上
private void SetPointPosition(FrameworkElement ellipse, Joint joint)
{
var scaledJoint = joint.ScaleTo(640, 480);
Canvas.SetLeft(ellipse, scaledJoint.Position.X);
Canvas.SetTop(ellipse, scaledJoint.Position.Y);
SkeletonCanvas.Children.Add(ellipse);
}
//使用polyline表示骨架线
Polyline headPolyline = new Polyline();
Polyline handleftPolyline = new Polyline();
Polyline handrightPolyline = new Polyline();
Polyline footleftPolyline = new Polyline();
Polyline footrightPolyline = new Polyline();
private void SetAllPointPosition(Skeleton skeleton)
{
SkeletonCanvas.Children.Clear();
headPolyline.Points.Clear();
handleftPolyline.Points.Clear();
handrightPolyline.Points.Clear();
footleftPolyline.Points.Clear();
footrightPolyline.Points.Clear();
//遍历所有骨骼点,并分类储存//哪里来的所有的骨骼点的代码
foreach (Joint joint in skeleton.Joints)
{
Point jointPoint = GetDisplayPosition(joint);
switch (joint.JointType)
{
case JointType.Head:
SetPointPosition(headPoint, joint);
headPolyline.Points.Add(jointPoint);
break;
case JointType.ShoulderCenter:
SetPointPosition(shouldercenterPoint, joint);
headPolyline.Points.Add(jointPoint);
handleftPolyline.Points.Add(jointPoint);
handrightPolyline.Points.Add(jointPoint);
break;
case JointType.ShoulderLeft:
SetPointPosition(shoulderleftPoint, joint);
handleftPolyline.Points.Add(jointPoint);
break;
case JointType.ShoulderRight:
SetPointPosition(shoulderrightPoint, joint);
handrightPolyline.Points.Add(jointPoint);
break;
case JointType.ElbowRight:
SetPointPosition(elbowrightPoint, joint);
handrightPolyline.Points.Add(jointPoint);
break;
case JointType.ElbowLeft:
SetPointPosition(elbowleftPoint, joint);
handleftPolyline.Points.Add(jointPoint);
break;
case JointType.WristRight:
SetPointPosition(wristrightPoint, joint);
handrightPolyline.Points.Add(jointPoint);
break;
case JointType.WristLeft:
SetPointPosition(wristleftPoint, joint);
handleftPolyline.Points.Add(jointPoint);
break;
case JointType.HandRight:
SetPointPosition(handrightPoint, joint);
handrightPolyline.Points.Add(jointPoint);
break;
case JointType.HandLeft:
SetPointPosition(handleftPoint, joint);
handleftPolyline.Points.Add(jointPoint);
break;
case JointType.Spine:
SetPointPosition(spinePoint, joint);
headPolyline.Points.Add(jointPoint);
break;
case JointType.HipCenter:
SetPointPosition(hipcenterPoint, joint);
headPolyline.Points.Add(jointPoint);
footleftPolyline.Points.Add(jointPoint);
footrightPolyline.Points.Add(jointPoint);
break;
case JointType.HipLeft:
SetPointPosition(hipleftPoint, joint);
footleftPolyline.Points.Add(jointPoint);
break;
case JointType.HipRight:
SetPointPosition(hiprightPoint, joint);
footrightPolyline.Points.Add(jointPoint);
break;
case JointType.KneeRight:
SetPointPosition(kneerightPoint, joint);
footrightPolyline.Points.Add(jointPoint);
break;
case JointType.KneeLeft:
SetPointPosition(kneeleftPoint, joint);
footleftPolyline.Points.Add(jointPoint);
break;
case JointType.AnkleRight:
SetPointPosition(anklerightPoint, joint);
footrightPolyline.Points.Add(jointPoint);
break;
case JointType.AnkleLeft:
SetPointPosition(anklerleftPoint, joint);
footleftPolyline.Points.Add(jointPoint);
break;
case JointType.FootRight:
SetPointPosition(footrightPoint, joint);
footrightPolyline.Points.Add(jointPoint);
break;
case JointType.FootLeft:
SetPointPosition(footleftPoint, joint);
footleftPolyline.Points.Add(jointPoint);
break;
default:
break;
}
}
headPolyline.Stroke = new SolidColorBrush(Colors.Blue); //Stroke画 Brush刷 Solid固体的 实心的 Canvas帆布油画
headPolyline.StrokeThickness = 5;
SkeletonCanvas.Children.Add(headPolyline);
handleftPolyline.Stroke = new SolidColorBrush(Colors.Blue);
handleftPolyline.StrokeThickness = 5;
SkeletonCanvas.Children.Add(handleftPolyline);
handrightPolyline.Stroke = new SolidColorBrush(Colors.Blue);
handrightPolyline.StrokeThickness = 5;
SkeletonCanvas.Children.Add(handrightPolyline);
footleftPolyline.Stroke = new SolidColorBrush(Colors.Blue);
footleftPolyline.StrokeThickness = 5;
SkeletonCanvas.Children.Add(footleftPolyline);
footrightPolyline.Stroke = new SolidColorBrush(Colors.Blue);
footrightPolyline.StrokeThickness = 5;
SkeletonCanvas.Children.Add(footrightPolyline);
}
private void Windows_Unloaded(object sender, RoutedEventArgs e)
{
kinectSensor.Stop();
}
}
}
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">