所以我转换了" KinectSkeletalTrackingGetJointData"从Kinect SDK的beta版到1.8版。这就是我现在所拥有的
The Original
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Research.Kinect;
using Microsoft.Research.Kinect.Nui;
using Coding4Fun.Kinect.WinForm;
namespace KinectSkeletalTrackingGetJointData
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Runtime nui;
private void Form1_Load(object sender, EventArgs e)
{
nui = new Runtime();
nui.Initialize(RuntimeOptions.UseSkeletalTracking | RuntimeOptions.UseColor);
nui.VideoStream.Open(ImageStreamType.Video, 2, ImageResolution.Resolution640x480, ImageType.Color);
nui.SkeletonFrameReady += new EventHandler<SkeletonFrameReadyEventArgs>(nui_SkeletonFrameReady);
nui.VideoFrameReady += new EventHandler<ImageFrameReadyEventArgs>(nui_VideoFrameReady);
}
void nui_VideoFrameReady(object sender, ImageFrameReadyEventArgs e)
{
picColorVideo.Image = e.ImageFrame.ToBitmap();
}
void nui_SkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
{
// 迴圈有可能跑多次 (多個玩家被追蹤)
foreach(SkeletonData data in e.SkeletonFrame.Skeletons)
{
// 只要處理正在被追蹤的玩家
if (data.TrackingState == SkeletonTrackingState.Tracked)
{
JointsCollection jc = data.Joints;
StringBuilder sb = new StringBuilder();
foreach (JointID jid in Enum.GetValues(typeof(JointID)))
{
if (jid != JointID.Count)
{
//Vector v = jc[jid].Position;
//sb.Append(string.Format("{0} = {1}, {2}, {3}\n", jid, v.X, v.Y, v.Z));
Point p = GetDisplayPosition(jc[jid]);
sb.Append(string.Format("{0} = {1}, {2}\n", jid, p.X, p.Y));
}
}
lblJoins.Text = sb.ToString();
}
}
}
// 轉換關節座標到螢幕座標
private Point GetDisplayPosition(Joint joint)
{
float depthX, depthY;
nui.SkeletonEngine.SkeletonToDepthImage(joint.Position, out depthX, out depthY);
depthX = Math.Max(0, Math.Min(depthX * 320, 320)); // 轉換到 320 x 240 空間系統
depthY = Math.Max(0, Math.Min(depthY * 240, 240));
int colorX, colorY;
nui.NuiCamera.GetColorPixelCoordinatesFromDepthPixel(ImageResolution.Resolution640x480, new ImageViewArea(), (int)depthX, (int)depthY, (short)0, out colorX, out colorY);
return new Point(colorX * picColorVideo.Width / 640, colorY * picColorVideo.Height / 480);
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
nui.Uninitialize();
}
}
}
我的版本
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Kinect;
using Coding4Fun.Kinect.WinForm;
namespace KinectSkeletalTrackingGetJointData
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
KinectSensor sensor;
private void Form1_Load(object sender, EventArgs e)
{
sensor = KinectSensor.KinectSensors[0];
sensor.Start();
sensor.ColorStream.Enable(ColorImageFormat.RgbResolution640x480Fps30);
sensor.SkeletonFrameReady += new EventHandler<SkeletonFrameReadyEventArgs>(nui_SkeletonFrameReady);
sensor.ColorFrameReady += new EventHandler<ColorImageFrameReadyEventArgs>(ColorImageReady);
//sensor.VideoFrameReady += new EventHandler<ImageFrameReadyEventArgs>(nui_VideoFrameReady);
}
byte[] pixelData;
void ColorImageReady(object sender, ColorImageFrameReadyEventArgs e)
{
bool receivedData = false;
using (ColorImageFrame colorImageFrame = e.OpenColorImageFrame())
{
if (colorImageFrame != null)
{
if (pixelData == null)
//allocate the first time
{
pixelData = new byte[colorImageFrame.PixelDataLength];
}
colorImageFrame.CopyPixelDataTo(pixelData);
receivedData = true;
}
else
{
// apps processing of image data is taking too long, it got more than 2 frames behind.
// the data is no longer avabilable.
}
if (receivedData)
{
// DISPLAY OR PROCESS IMAGE DATA IN pixelData HERE
}
}
}
//void nui_VideoFrameReady(object sender, ImageFrameReadyEventArgs e)
//{
// picColorVideo.Image = e.ImageFrame.ToBitmap();
//}
void nui_SkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
{
// 迴圈有可能跑多次 (多個玩家被追蹤)
foreach(Skeleton data in e.SkeletonFrame.CopySkeletonDataTo)
{
// 只要處理正在被追蹤的玩家
if (data.TrackingState == SkeletonTrackingState.Tracked)
{
JointCollection jc = data.Joints;
StringBuilder sb = new StringBuilder();
foreach (JointID jid in Enum.GetValues(typeof(JointID)))
{
if (jid != JointID.Count)
{
//Vector v = jc[jid].Position;
//sb.Append(string.Format("{0} = {1}, {2}, {3}\n", jid, v.X, v.Y, v.Z));
Point p = GetDisplayPosition(jc[jid]);
sb.Append(string.Format("{0} = {1}, {2}\n", jid, p.X, p.Y));
}
}
lblJoins.Text = sb.ToString();
}
}
}
// 轉換關節座標到螢幕座標
//private Point GetDisplayPosition(Joint joint)
//{
// float depthX, depthY;
// sensor.SkeletonStream.SkeletonToDepthImage(joint.Position, out depthX, out depthY);
// depthX = Math.Max(0, Math.Min(depthX * 320, 320)); // 轉換到 320 x 240 空間系統
// depthY = Math.Max(0, Math.Min(depthY * 240, 240));
// int colorX, colorY;
// sensor.NuiCamera.GetColorPixelCoordinatesFromDepthPixel(ImageResolution.Resolution640x480, new ImageViewArea(), (int)depthX, (int)depthY, (short)0, out colorX, out colorY);
// return new Point(colorX * picColorVideo.Width / 640, colorY * picColorVideo.Height / 480);
//}
private Point getDisplayPosition(DepthImageFrame depthFrame, Joint joint)
{
float depthX, depthY;
DepthImagePoint depthPoint = sensor.MapSkeletonPointToDepth(joint.Position, depthImageFormat);
depthX = depthPoint.X;
depthY = depthPoint.Y;
depthX = Math.Max(0, Math.Min(depthX * 320, 320));
depthY = Math.Max(0, Math.Min(depthY * 240, 240));
int colorX, colorY;
ColorImagePoint colorPoint = depthFrame.MapToColorImagePoint(depthPoint.X, depthPoint.Y, sensor.ColorStream.Format);
colorX = colorPoint.X;
colorY = colorPoint.Y;
return new Point((int)(skeleton.Width * colorX / 640.0), (int)(skeleton.Height * colorY / 480));
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
sensor.Stop();
}
}
}
它仍有一些问题,如何解决它
错误:
1。 &#39; Microsoft.Kinect.SkeletonFrameReadyEventArgs&#39;不包含&#39; SkeletonFrame&#39;的定义没有扩展方法&#39; SkeletonFrame&#39;接受类型&#39; Microsoft.Kinect.SkeletonFrameReadyEventArgs&#39;的第一个参数。可以找到(你错过了使用指令或汇编引用吗?)D:\ 06。骨架追踪与萤幕座标转换\ KinectSkeletalTrackingGetJointData \ Form1.cs 73
2。 &#39; Microsoft.Kinect.SkeletonStream&#39;不包含&#39; SkeletonToDepthImage&#39;的定义没有扩展方法&#39; SkeletonToDepthImage&#39;接受类型&#39; Microsoft.Kinect.SkeletonStream&#39;的第一个参数。可以找到(你错过了使用指令或汇编引用吗?)D:\ 06。骨架追踪与萤幕座标转换\ KinectSkeletalTrackingGetJointData \ Form1.cs 102
3。 &#39; Microsoft.Kinect.KinectSensor&#39;不包含&#39; NuiCamera&#39;的定义没有扩展方法&#39; NuiCamera&#39;接受第一个类型&#39; Microsoft.Kinect.KinectSensor&#39;可以找到(你错过了使用指令或汇编引用吗?)D:\ 06。骨架追踪与萤幕座标转换\ KinectSkeletalTrackingGetJointData \ Form1.cs 106
4。 名称&#39; ImageResolution&#39;在当前上下文中不存在D:\ 06。骨架追踪与萤幕座标转换\ KinectSkeletalTrackingGetJointData \ Form1.cs 106
5。 类型或命名空间名称&#39; ImageViewArea&#39;找不到(你错过了使用指令或程序集引用吗?) d:\ 06。骨架追踪与萤幕座标转换\ KinectSkeletalTrackingGetJointData \ Form1.cs 106
----------------------------------------------- ----------------------------- 2014年5月13日