C# 将Kinect Beta版转换为1.8版

C# 将Kinect Beta版转换为1.8版,c#,kinect,C#,Kinect,因此,我将“Kinect SkeletalTrackingGetJointData”从Kinect SDK的测试版转换为1.8版。这是我现在拥有的 原件 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Wind

因此,我将“Kinect SkeletalTrackingGetJointData”从Kinect SDK的测试版转换为1.8版。这是我现在拥有的

原件

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();
    }

    }
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
使用Microsoft.Research.Kinect;
使用Microsoft.Research.Kinect.Nui;
使用Coding4Fun.Kinect.WinForm;
命名空间KinectSkeletalTrackingGetJointData
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
}
运行时nui;
私有void Form1\u加载(对象发送方、事件参数e)
{
nui=新运行时();
numi.Initialize(RuntimeOptions.UseSkeletalTracking | RuntimeOptions.UseColor);
打开(ImageStreamType.Video,2,ImageResolution.Resolution640x480,ImageType.Color);
nui.SkeletonFrameReady+=新事件处理程序(nui_SkeletonFrameReady);
nui.VideoFrameReady+=新事件处理程序(nui\u VideoFrameReady);
}
void nui\u VideoFrameReady(对象发送方,ImageFrameReadyEventArgs e)
{
picColorVideo.Image=e.ImageFrame.ToBitmap();
}
void nui_SkeletonFrameReady(对象发送方,SkeletonFrameReadyEventArgs e)
{
// 迴圈有可能跑多次 (多個玩家被追蹤)
foreach(e.SkeletonFrame.Skeletons中的SkeletonData数据)
{
// 只要處理正在被追蹤的玩家
if(data.TrackingState==SkeletonTrackingState.Tracked)
{
JointCollection jc=数据。关节;
StringBuilder sb=新的StringBuilder();
foreach(Enum.GetValues中的JointID jid(typeof(JointID)))
{
if(jid!=JointID.Count)
{
//向量v=jc[jid]。位置;
//sb.Append(string.Format(“{0}={1},{2},{3}\n”,jid,v.X,v.Y,v.Z));
点p=GetDisplayPosition(jc[jid]);
sb.Append(string.Format(“{0}={1},{2}\n”,jid,p.X,p.Y));
}
}
lblJoins.Text=sb.ToString();
}
}
}
// 轉換關節座標到螢幕座標
专用点显示位置(关节)
{
浮动深度,深度;
nui.SkeletonEngine.SKELETONDEPTHIMAGE(关节位置、外深、外深);
depthX=Math.Max(0,Math.Min(depthX*320320));//轉換到 320 x 240空間系統
depthY=Math.Max(0,Math.Min(depthY*240240));
int colorX,colorY;
nui.NuiCamera.GetColorPixelCoordinationfromDepthPixel(imagesresolution.Resolution640x480,new ImageViewArea(),(int)depthX,(int)depthY,(short)0,out colorX,out colorY);
返回新点(colorX*picColorVideo.Width/640,colorY*picColorVideo.Height/480);
}
私有void Form1\u FormClosed(对象发送方,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();
    }

    }
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
使用Microsoft.Kinect;
使用Coding4Fun.Kinect.WinForm;
命名空间KinectSkeletalTrackingGetJointData
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
}
运动传感器;
私有void Form1\u加载(对象发送方、事件参数e)
{
传感器=Kinect传感器。Kinect传感器[0];
sensor.Start();
sensor.ColorStream.Enable(ColorImageFormat.RgbResolution640x480Fps30);
sensor.SkeletonFrameReady+=新事件处理程序(nui_SkeletonFrameReady);
sensor.ColorFrameReady+=新事件处理程序(ColorImageReady);
//sensor.VideoFrameReady+=新事件处理程序(nui\U VideoFrameReady);
}
字节[]像素数据;
void ColorImageReady(对象发送方,ColorImageFrameReadyEventArgs e)
{
bool receivedData=false;
使用(ColorImageFrame ColorImageFrame=e.OpenColorImageFrame())
{
if(colorImageFrame!=null)
{
如果(pixelData==null)
//第一次分配
{
pixelData=新字节[colorImageFrame.PixelDataLength];
}
colorImageFrame.CopyPixelDataTo(pixelData);
receivedData=true;
}
其他的
{
//应用程序处理图像数据的时间太长,延迟了2帧以上。
//数据不再是可保存的。
}
如果(接收数据)
{
//在此处显示或处理像素数据中的图像数据
}
}
}
//void nui\u VideoFrameReady(对象发送方,ImageFrameReadyEventArgs e)
//{
//picColorVideo.Image=e.ImageFrame.ToBitmap();
//}
void nui_SkeletonFrameReady(对象发送方,SkeletonFrameReadyEventArgs e)
{
// 迴圈有可能跑多次 (多個玩家被追蹤)
foreach(e.SkeletonFrame.CopySkeletonDataTo中的骨架数据)
{
// 只要處理正在被追蹤的玩家
if(data.TrackingState==SkeletonTrackingState.Tracked)
{
JointCollection jc=数据。接头;
StringBuilder sb=新的StringBuilder();
foreach(Enum.GetValues中的JointID jid(typeof(JointID)))
{
if(jid!=JointID.Count)
{
//向量v=jc[jid]。位置;
//sb.Append(string.Format(“{0}={1},{2},{3}\n”,jid,v.X,v.Y,v.Z));
点p=GetDisplayPosition(jc[jid]);
sb.Append(string.Format(“{0}={1},{2}\n”,jid,p.X,p.Y));