本文介绍了C# - 将Kinect关节位置X,Y,Z写入txt文件。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我想读一些关节的X,Y,Z位置,并在点击按钮时将它们写入txt文件。我设法做了一些事情,但它始终显示/写入位置:0,0,0表示x,y,z。这是我到目前为止。

使用System; 
使用System.Collections.Generic;
使用System.Linq;使用System.Windows
;
使用System.Windows.Media;
使用Microsoft.Kinect;
使用System.IO;
namespace WpfApplication1
{
///< summary>
/// MainWindow.xaml的交互逻辑
///< / summary>
public partial class MainWindow:Window
{
KinectSensor sensor = KinectSensor.KinectSensors [0];
public static Skeleton skeleton = new Skeleton();

联合rightHand = skeleton.Joints [JointType.HandRight];
Joint heaad = skeleton.Joints [JointType.Head];


#region" Variables"
///< summary>
///身体中心椭圆的厚度
///< / summary>
private const double BodyCenterThickness = 10;

///< summary>
///剪辑边缘矩形的厚度
///< / summary>
private const double ClipBoundsThickness = 10;

///< summary>
///用于绘制骨架中心点的画笔
///< / summary>
private readonly Brush centerPointBrush = Brushes.Blue;

///< summary>
///用于绘制当前跟踪的关节的画笔
///< / summary>
private readonly Brush trackedJointBrush = new SolidColorBrush(Color.FromArgb(255,68,192,68));

///< summary>
///用于绘制当前推断的关节的画笔
///< / summary>
private readonly Brush inferredJointBrush = Brushes.Yellow;

///< summary>
///用于绘制当前跟踪的骨骼的笔
///< / summary>
private readonly Pen trackedBonePen = new Pen(Brushes.Green,6);

///< summary>
///用于绘制当前推断的骨骼的笔
///< / summary>
private readonly Pen inferredBonePen = new Pen(Brushes.Gray,1);

///< summary>
///我们将展示的图片图片
///< / summary>
private DrawingImage imageSource;

///< summary>
///绘制的关节线的厚度
///< / summary>
private const double JointThickness = 3;


///< summary>
///骨架渲染输出的绘图组
///< / summary>
private DrawingGroup drawingGroup;
///< summary>
///输出宽度
///< / summary>
private const float RenderWidth = 640.0f;

///< summary>
///我们的输出图的高度
///< / summary>
private const float RenderHeight = 480.0f;
#endregion

public MainWindow()
{
InitializeComponent();
//初始化后订阅
Loaded + = MainWindow_Loaded形式的加载事件;

//初始化之后订阅
形式的卸载事件//我们使用此事件在关闭应用程序时停止传感器。
卸载+ = MainWindow_Unloaded;


}

void MainWindow_Unloaded(对象发送者,RoutedEventArgs e)
{
//停止Sestor
传感器。停止();

}


void MainWindow_Loaded(对象发送者,RoutedEventArgs e)
{

//创建一个绘图组将用于绘制
this.drawingGroup = new DrawingGroup();

//创建一个图像显示我们骨架的源
this.imageSource = new DrawingImage(this.drawingGroup);

//在我们的图像控件中显示图像
Image.Source = imageSource;

尝试
{
//检查传感器是否已连接
if(sensor.Status == KinectStatus.Connected)
{
//启动传感器
sensor.Start();
//告诉Kinect传感器使用默认模式(人体骨架)||坐着(人体骨架坐下)
sensor.SkeletonStream.TrackingMode = SkeletonTrackingMode.Default;
//订阅te Sensor的SkeletonFrameready事件以跟踪连接并创建要在我们的图像控件上显示的连接
sensor.SkeletonFrameReady + = sensor_SkeletonFrameReady;
//带有颜色的好消息,如果您的传感器工作正常,则会提醒您
Message.Text =" Kinect Ready" ;;
Message.Background = new SolidColorBrush(Colors.Green);
Message.Foreground = new SolidColorBrush(Colors.White);

//打开骨架流以接收骨架帧
this.sensor.SkeletonStream.Enable();
}
else if(sensor.Status == KinectStatus.Disconnected)
{
//带有颜色的好消息,提醒您传感器是否正常工作
Message.Text ="Kinect传感器未连接";
Message.Background = new SolidColorBrush(Colors.Orange);
Message.Foreground = new SolidColorBrush(Colors.Black);

}
else if(sensor.Status == KinectStatus.NotPowered)
{//带有颜色的好消息,提醒您传感器是否正常工作
Message.Text ="Kinect传感器未供电";
Message.Background = new SolidColorBrush(Colors.Red);
Message.Foreground = new SolidColorBrush(Colors.Black);
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);

}
}

///< summary>
//当Skeleton准备就绪时,必须绘制Skeleton
///< / summary>
///< param name =" sender">< / param>
///< param name =" e">< / param>
void sensor_SkeletonFrameReady(object sender,SkeletonFrameReadyEventArgs e)
{
//声明一个Skeletons数组
Skeleton [] skeletons = new Skeleton [1];

//打开一个SkeletonFrame对象,该对象包含一帧骨架数据。
using(SkeletonFrame skeletonframe = e.OpenSkeletonFrame())
{
//检查Frame是否确实打开
if(skeletonframe!= null)
{

skeletons = new Skeleton [skeletonframe.SkeletonArrayLength];

//将骨架数据复制到Skeletons数组,其中每个Skeleton包含一个关节集合。
skeletonframe.CopySkeletonDataTo(skeletons);

//基于默认模式(Standing),"Seated"和"Seated"来绘制Skeleton。
if(sensor.SkeletonStream.TrackingMode == SkeletonTrackingMode.Default)
{
// Draw Standing Skeleton
DrawStandingSkeletons(skeletons);
}
else if(sensor.SkeletonStream.TrackingMode == SkeletonTrackingMode.Seated)
{
//用10个关节绘制一个坐着的骨架
DrawSeatedSkeletons(骨架);
}
}

}


}



// Thi函数绘制Standing或Default Skeleton
private void DrawStandingSkeletons(Skeleton [] skeletons)
{

using(DrawingContext dc = this.drawingGroup.Open())
{
//绘制透明背景以设置渲染大小或我们的Canvas
dc.DrawRectangle(Brushes.Black,null,new Rect(0.0,0.0,RenderWidth,RenderHeight));

//如果骨架数组有项目
if(skeletons.Length!= 0)
{
//循环通过Skeleton加入
foreach (骷髅中的骨架骷髅)
{
RenderClippedEdges(skel,dc);

if(skel.TrackingState == SkeletonTrackingState.Tracked)
{
this.DrawBonesAndJoints(skel,dc);


}
else if(skel.TrackingState == SkeletonTrackingState.PositionOnly)
{
dc.DrawEllipse(this.centerPointBrush,
null,
this.SkeletonPointToScreen(skel.Position),BodyCenterThickness,BodyCenterThickness);

}

}


}

//防止在画布外画画
this.drawingGroup.ClipGeometry = new RectangleGeometry(new Rect(0.0,0.0,RenderWidth,RenderHeight));

}
}


private void DrawSeatedSkeletons(Skeleton []骷髅)
{

使用( DrawingContext dc = this.drawingGroup.Open())
{
//绘制透明背景以设置渲染大小
dc.DrawRectangle(Brushes.Black,null,new Rect(0.0, 0.0,RenderWidth,RenderHeight));

if(skeletons.Length!= 0)
{
foreach(骷髅骨架)
{
RenderClippedEdges(skel,dc);

if(skel.TrackingState == SkeletonTrackingState.Tracked)
{
this.DrawBonesAndJoints(skel,dc);


}
else if(skel.TrackingState == SkeletonTrackingState.PositionOnly)
{
dc.DrawEllipse(this.centerPointBrush,
null,
this.SkeletonPointToScreen(skel.Position),BodyCenterThickness,BodyCenterThickness);

}

}


}

//防止在画布外画画
this.drawingGroup.ClipGeometry = new RectangleGeometry(new Rect(0.0,0.0,RenderWidth,RenderHeight));

}
}



///< summary>
///绘制指标以显示哪些边是裁剪骨架数据
///< / summary>
///< param name =" skeleton">骨架,用于绘制< / param>的剪辑信息
///< param name =" drawingContext">绘制上下文以绘制到< / param>
private static void RenderClippedEdges(Skeleton skeleton,DrawingContext drawingContext)
{
if(skeleton.ClippedEdges.HasFlag(FrameEdges.Bottom))
{
drawingContext.DrawRectangle(
Brushes.Red,
null,
new Rect(0,RenderHeight - ClipBoundsThickness,RenderWidth,ClipBoundsThickness));
}

if(skeleton.ClippedEdges.HasFlag(FrameEdges.Top))
{
drawingContext.DrawRectangle(
Brushes.Red,
null,
new Rect(0,0,RenderWidth,ClipBoundsThickness));
}

if(skeleton.ClippedEdges.HasFlag(FrameEdges.Left))
{
drawingContext.DrawRectangle(
Brushes.Red,
null,
new Rect(0,0,ClipBoundsThickness,RenderHeight));
}

if(skeleton.ClippedEdges.HasFlag(FrameEdges.Right))
{
drawingContext.DrawRectangle(
Brushes.Red,
null,
new Rect(RenderWidth - ClipBoundsThickness,0,ClipBoundsThickness,RenderHeight));
}
}

///< summary>
///绘制骨架的骨骼和关节
///< / summary>
///< param name =" skeleton">要绘制的骨架< / param>
///< param name =" drawingContext">绘制上下文以绘制到< / param>
private void DrawBonesAndJoints(Skeleton skeleton,DrawingContext drawingContext)
{
// Render Torso
this.DrawBone(skeleton,drawingContext,JointType.Head,JointType.ShoulderCenter);
this.DrawBone(skeleton,drawingContext,JointType.ShoulderCenter,JointType.ShoulderLeft);
this.DrawBone(skeleton,drawingContext,JointType.ShoulderCenter,JointType.ShoulderRight);
this.DrawBone(skeleton,drawingContext,JointType.ShoulderCenter,JointType.Spine);
this.DrawBone(skeleton,drawingContext,JointType.Spine,JointType.HipCenter);
this.DrawBone(skeleton,drawingContext,JointType.HipCenter,JointType.HipLeft);
this.DrawBone(skeleton,drawingContext,JointType.HipCenter,JointType.HipRight);

//左臂
this.DrawBone(骨架,drawingContext,JointType.ShoulderLeft,JointType.ElbowLeft);
this.DrawBone(skeleton,drawingContext,JointType.ElbowLeft,JointType.WristLeft);
this.DrawBone(skeleton,drawingContext,JointType.WristLeft,JointType.HandLeft);

//右臂
this.DrawBone(骨架,drawingContext,JointType.ShoulderRight,JointType.ElbowRight);
this.DrawBone(skeleton,drawingContext,JointType.ElbowRight,JointType.WristRight);
this.DrawBone(skeleton,drawingContext,JointType.WristRight,JointType.HandRight);

//左腿
this.DrawBone(骨架,drawingContext,JointType.HipLeft,JointType.KneeLeft);
this.DrawBone(skeleton,drawingContext,JointType.KneeLeft,JointType.AnkleLeft);
this.DrawBone(skeleton,drawingContext,JointType.AnkleLeft,JointType.FootLeft);

//右腿
this.DrawBone(骨架,drawingContext,JointType.HipRight,JointType.KneeRight);
this.DrawBone(skeleton,drawingContext,JointType.KneeRight,JointType.AnkleRight);
this.DrawBone(skeleton,drawingContext,JointType.AnkleRight,JointType.FootRight);

//渲染关节
foreach(骨架中的关节.Joints)
{
Brush drawBrush = null;

if(joint.TrackingState == JointTrackingState.Tracked)
{
drawBrush = this.trackedJointBrush;
}
else if(joint.TrackingState == JointTrackingState.Inferred)
{
drawBrush = this.inferredJointBrush;
}

if(drawBrush!= null)
{
drawingContext.DrawEllipse(drawBrush,null,this.SkeletonPointToScreen(joint.Position),JointThickness,JointThickness) ;
}
}
}

///< summary>
///在两个关节之间绘制一条骨线
///< / summary>
///< param name =" skeleton">骨架,用于从< / param>中绘制骨骼
///< param name =" drawingContext">绘制上下文以绘制到< / param>
///< param name =" jointType0"> joint从< / param>开始绘制
///< param name =" jointType1">以< / param>结束绘图
private void DrawBone(Skeleton skeleton,DrawingContext drawingContext,JointType jointType0,JointType jointType1)
{
Joint joint0 = skeleton.Joints [jointType0];
Joint joint1 = skeleton.Joints [jointType1];

//如果我们找不到这些关节,请退出
if(joint0.TrackingState == JointTrackingState.NotTracked || joint1.TrackingState == JointTrackingState.NotTracked)
{
return;
}

//如果两个点都被推断,请不要绘制
if(joint0.TrackingState == JointTrackingState.Inferred&& joint1.TrackingState == JointTrackingState.Inferred )
{
return;
}

//我们假设所有绘制的骨骼都是推断的,除非跟踪了两个关节
Pen drawPen = this.inferredBonePen;

if(joint0.TrackingState == JointTrackingState.Tracked&& joint1.TrackingState == JointTrackingState.Tracked)
{
drawPen = this.trackedBonePen;
}

drawingContext.DrawLine(drawPen,this.SkeletonPointToScreen(joint0.Position),this.SkeletonPointToScreen(joint1.Position));
}


///< summary>
///将SkeletonPoint映射到我们的渲染空间并转换为Point
///< / summary>
///< param name =" skelpoint">指向地图< / param>
///< returns>映射点< / returns>
private Point SkeletonPointToScreen(SkeletonPoint skelpoint)
{
//将点转换为深度空间。
//我们没有直接使用深度,但我们确实需要640x480输出分辨率的点数。
DepthImagePoint depthPoint = this.sensor.CoordinateMapper.MapSkeletonPointToDepthPoint(skelpoint,DepthImageFormat.Resolution640x480Fps30);
返回新的Point(depthPoint.X,depthPoint.Y);
}


//按钮点击方法
private void stoji_Click(object sender,RoutedEventArgs e)
{
double rightX = rightHand。 Position.X;
double rightY = rightHand.Position.Y;
double rightZ = rightHand.Position.Z;

double rightXh = heaad.Position.X;
double rightYh = heaad.Position.Y;
double rightZh = heaad.Position.Z;

File.AppendAllText(@" E:\ skuska.txt",rightX +"," + rightY +"," + rightZ + Environment.NewLine);


}
}
}



解决方案

Hi,

I would like to read X,Y,Z positions of some joints and write them into a txt file on button click. I managed to do something, but it always shows/ writes positions: 0, 0, 0 for x,y,z. This is what I have so far.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Media;
using Microsoft.Kinect;
using System.IO;
namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        KinectSensor sensor = KinectSensor.KinectSensors[0];
        public static Skeleton skeleton=new Skeleton();

        Joint rightHand = skeleton.Joints[JointType.HandRight];
        Joint heaad = skeleton.Joints[JointType.Head];


        #region "Variables"
        /// <summary>
        /// Thickness of body center ellipse
        /// </summary>
        private const double BodyCenterThickness = 10;

        /// <summary>
        /// Thickness of clip edge rectangles
        /// </summary>
        private const double ClipBoundsThickness = 10;

        /// <summary>
        /// Brush used to draw skeleton center point
        /// </summary>
        private readonly Brush centerPointBrush = Brushes.Blue;

        /// <summary>
        /// Brush used for drawing joints that are currently tracked
        /// </summary>
        private readonly Brush trackedJointBrush = new SolidColorBrush(Color.FromArgb(255, 68, 192, 68));

        /// <summary>
        /// Brush used for drawing joints that are currently inferred
        /// </summary>
        private readonly Brush inferredJointBrush = Brushes.Yellow;

        /// <summary>
        /// Pen used for drawing bones that are currently tracked
        /// </summary>
        private readonly Pen trackedBonePen = new Pen(Brushes.Green, 6);

        /// <summary>
        /// Pen used for drawing bones that are currently inferred
        /// </summary>
        private readonly Pen inferredBonePen = new Pen(Brushes.Gray, 1);

        /// <summary>
        /// Drawing image that we will display
        /// </summary>
        private DrawingImage imageSource;

        /// <summary>
        /// Thickness of drawn joint lines
        /// </summary>
        private const double JointThickness = 3;


        /// <summary>
        /// Drawing group for skeleton rendering output
        /// </summary>
        private DrawingGroup drawingGroup;
        /// <summary>
        /// Width of output drawing
        /// </summary>
        private const float RenderWidth = 640.0f;

        /// <summary>
        /// Height of our output drawing
        /// </summary>
        private const float RenderHeight = 480.0f;
        #endregion

        public MainWindow()
        {
            InitializeComponent();
            //After Initialization subscribe to the loaded event of the form
            Loaded += MainWindow_Loaded;

            //After Initialization subscribe to the unloaded event of the form
            //We use this event to stop the sensor when the application is being closed.
            Unloaded += MainWindow_Unloaded;


        }

        void MainWindow_Unloaded(object sender, RoutedEventArgs e)
        {
            //stop the Sestor
            sensor.Stop();

        }


        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {

            //Create a Drawing Group that will be used for Drawing
            this.drawingGroup = new DrawingGroup();

            //Create an image Source that will display our skeleton
            this.imageSource = new DrawingImage(this.drawingGroup);

            //Display the Image in our Image control
            Image.Source = imageSource;

            try
            {
                //Check if the Sensor is Connected
                if (sensor.Status == KinectStatus.Connected)
                {
                    //Start the Sensor
                    sensor.Start();
                    //Tell Kinect Sensor to use the Default Mode(Human Skeleton Standing) || Seated(Human Skeleton Sitting Down)
                    sensor.SkeletonStream.TrackingMode = SkeletonTrackingMode.Default;
                    //Subscribe to te  Sensor's SkeletonFrameready event to track the joins and create the joins to display on our image control
                    sensor.SkeletonFrameReady += sensor_SkeletonFrameReady;
                    //nice message with Colors to alert you if your sensor is working or not
                    Message.Text = "Kinect Ready";
                    Message.Background = new SolidColorBrush(Colors.Green);
                    Message.Foreground = new SolidColorBrush(Colors.White);

                    // Turn on the skeleton stream to receive skeleton frames
                    this.sensor.SkeletonStream.Enable();
                }
                else if (sensor.Status == KinectStatus.Disconnected)
                {
                    //nice message with Colors to alert you if your sensor is working or not
                    Message.Text = "Kinect Sensor is not Connected";
                    Message.Background = new SolidColorBrush(Colors.Orange);
                    Message.Foreground = new SolidColorBrush(Colors.Black);

                }
                else if (sensor.Status == KinectStatus.NotPowered)
                {//nice message with Colors to alert you if your sensor is working or not
                    Message.Text = "Kinect Sensor is not Powered";
                    Message.Background = new SolidColorBrush(Colors.Red);
                    Message.Foreground = new SolidColorBrush(Colors.Black);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);

            }
        }

        /// <summary>
        //When the Skeleton is Ready it must draw the Skeleton
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void sensor_SkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
        {
            //declare an array of Skeletons
            Skeleton[] skeletons = new Skeleton[1];

            //Opens a SkeletonFrame object, which contains one frame of skeleton data.
            using (SkeletonFrame skeletonframe = e.OpenSkeletonFrame())
            {
                //Check if the Frame is Indeed open
                if (skeletonframe != null)
                {

                    skeletons = new Skeleton[skeletonframe.SkeletonArrayLength];

                    // Copies skeleton data to an array of Skeletons, where each Skeleton contains a collection of the joints.
                    skeletonframe.CopySkeletonDataTo(skeletons);

                    //draw the Skeleton based on the Default Mode(Standing), "Seated"
                    if (sensor.SkeletonStream.TrackingMode == SkeletonTrackingMode.Default)
                    {
                        //Draw standing Skeleton
                        DrawStandingSkeletons(skeletons);
                    }
                    else if (sensor.SkeletonStream.TrackingMode == SkeletonTrackingMode.Seated)
                    {
                        //Draw a Seated Skeleton with 10 joints
                        DrawSeatedSkeletons(skeletons);
                    }
                }

            }


        }



        //Thi Function Draws the Standing  or Default Skeleton
        private void DrawStandingSkeletons(Skeleton[] skeletons)
        {

            using (DrawingContext dc = this.drawingGroup.Open())
            {
                //Draw a Transparent background to set the render size or our Canvas
                dc.DrawRectangle(Brushes.Black, null, new Rect(0.0, 0.0, RenderWidth, RenderHeight));

                //If the skeleton Array has items
                if (skeletons.Length != 0)
                {
                    //Loop through the Skeleton joins
                    foreach (Skeleton skel in skeletons)
                    {
                        RenderClippedEdges(skel, dc);

                        if (skel.TrackingState == SkeletonTrackingState.Tracked)
                        {
                            this.DrawBonesAndJoints(skel, dc);


                        }
                        else if (skel.TrackingState == SkeletonTrackingState.PositionOnly)
                        {
                            dc.DrawEllipse(this.centerPointBrush,
                                           null,
                                           this.SkeletonPointToScreen(skel.Position), BodyCenterThickness, BodyCenterThickness);

                        }

                    }


                }

                //Prevent Drawing outside the canvas
                this.drawingGroup.ClipGeometry = new RectangleGeometry(new Rect(0.0, 0.0, RenderWidth, RenderHeight));

            }
        }


        private void DrawSeatedSkeletons(Skeleton[] skeletons)
        {

            using (DrawingContext dc = this.drawingGroup.Open())
            {
                //Draw a Transparent background to set the render size
                dc.DrawRectangle(Brushes.Black, null, new Rect(0.0, 0.0, RenderWidth, RenderHeight));

                if (skeletons.Length != 0)
                {
                    foreach (Skeleton skel in skeletons)
                    {
                        RenderClippedEdges(skel, dc);

                        if (skel.TrackingState == SkeletonTrackingState.Tracked)
                        {
                            this.DrawBonesAndJoints(skel, dc);


                        }
                        else if (skel.TrackingState == SkeletonTrackingState.PositionOnly)
                        {
                            dc.DrawEllipse(this.centerPointBrush,
                                           null,
                                           this.SkeletonPointToScreen(skel.Position), BodyCenterThickness, BodyCenterThickness);

                        }

                    }


                }

                //Prevent Drawing outside the canvas
                this.drawingGroup.ClipGeometry = new RectangleGeometry(new Rect(0.0, 0.0, RenderWidth, RenderHeight));

            }
        }



        /// <summary>
        /// Draws indicators to show which edges are clipping skeleton data
        /// </summary>
        /// <param name="skeleton">skeleton to draw clipping information for</param>
        /// <param name="drawingContext">drawing context to draw to</param>
        private static void RenderClippedEdges(Skeleton skeleton, DrawingContext drawingContext)
        {
            if (skeleton.ClippedEdges.HasFlag(FrameEdges.Bottom))
            {
                drawingContext.DrawRectangle(
                    Brushes.Red,
                    null,
                    new Rect(0, RenderHeight - ClipBoundsThickness, RenderWidth, ClipBoundsThickness));
            }

            if (skeleton.ClippedEdges.HasFlag(FrameEdges.Top))
            {
                drawingContext.DrawRectangle(
                    Brushes.Red,
                    null,
                    new Rect(0, 0, RenderWidth, ClipBoundsThickness));
            }

            if (skeleton.ClippedEdges.HasFlag(FrameEdges.Left))
            {
                drawingContext.DrawRectangle(
                    Brushes.Red,
                    null,
                    new Rect(0, 0, ClipBoundsThickness, RenderHeight));
            }

            if (skeleton.ClippedEdges.HasFlag(FrameEdges.Right))
            {
                drawingContext.DrawRectangle(
                    Brushes.Red,
                    null,
                    new Rect(RenderWidth - ClipBoundsThickness, 0, ClipBoundsThickness, RenderHeight));
            }
        }

        /// <summary>
        /// Draws a skeleton's bones and joints
        /// </summary>
        /// <param name="skeleton">skeleton to draw</param>
        /// <param name="drawingContext">drawing context to draw to</param>
        private void DrawBonesAndJoints(Skeleton skeleton, DrawingContext drawingContext)
        {
            // Render Torso
            this.DrawBone(skeleton, drawingContext, JointType.Head, JointType.ShoulderCenter);
            this.DrawBone(skeleton, drawingContext, JointType.ShoulderCenter, JointType.ShoulderLeft);
            this.DrawBone(skeleton, drawingContext, JointType.ShoulderCenter, JointType.ShoulderRight);
            this.DrawBone(skeleton, drawingContext, JointType.ShoulderCenter, JointType.Spine);
            this.DrawBone(skeleton, drawingContext, JointType.Spine, JointType.HipCenter);
            this.DrawBone(skeleton, drawingContext, JointType.HipCenter, JointType.HipLeft);
            this.DrawBone(skeleton, drawingContext, JointType.HipCenter, JointType.HipRight);

            // Left Arm
            this.DrawBone(skeleton, drawingContext, JointType.ShoulderLeft, JointType.ElbowLeft);
            this.DrawBone(skeleton, drawingContext, JointType.ElbowLeft, JointType.WristLeft);
            this.DrawBone(skeleton, drawingContext, JointType.WristLeft, JointType.HandLeft);

            // Right Arm
            this.DrawBone(skeleton, drawingContext, JointType.ShoulderRight, JointType.ElbowRight);
            this.DrawBone(skeleton, drawingContext, JointType.ElbowRight, JointType.WristRight);
            this.DrawBone(skeleton, drawingContext, JointType.WristRight, JointType.HandRight);

            // Left Leg
            this.DrawBone(skeleton, drawingContext, JointType.HipLeft, JointType.KneeLeft);
            this.DrawBone(skeleton, drawingContext, JointType.KneeLeft, JointType.AnkleLeft);
            this.DrawBone(skeleton, drawingContext, JointType.AnkleLeft, JointType.FootLeft);

            // Right Leg
            this.DrawBone(skeleton, drawingContext, JointType.HipRight, JointType.KneeRight);
            this.DrawBone(skeleton, drawingContext, JointType.KneeRight, JointType.AnkleRight);
            this.DrawBone(skeleton, drawingContext, JointType.AnkleRight, JointType.FootRight);

            // Render Joints
            foreach (Joint joint in skeleton.Joints)
            {
                Brush drawBrush = null;

                if (joint.TrackingState == JointTrackingState.Tracked)
                {
                    drawBrush = this.trackedJointBrush;
                }
                else if (joint.TrackingState == JointTrackingState.Inferred)
                {
                    drawBrush = this.inferredJointBrush;
                }

                if (drawBrush != null)
                {
                    drawingContext.DrawEllipse(drawBrush, null, this.SkeletonPointToScreen(joint.Position), JointThickness, JointThickness);
                }
            }
        }

        /// <summary>
        /// Draws a bone line between two joints
        /// </summary>
        /// <param name="skeleton">skeleton to draw bones from</param>
        /// <param name="drawingContext">drawing context to draw to</param>
        /// <param name="jointType0">joint to start drawing from</param>
        /// <param name="jointType1">joint to end drawing at</param>
        private void DrawBone(Skeleton skeleton, DrawingContext drawingContext, JointType jointType0, JointType jointType1)
        {
            Joint joint0 = skeleton.Joints[jointType0];
            Joint joint1 = skeleton.Joints[jointType1];

            // If we can't find either of these joints, exit
            if (joint0.TrackingState == JointTrackingState.NotTracked || joint1.TrackingState == JointTrackingState.NotTracked)
            {
                return;
            }

            // Don't draw if both points are inferred
            if (joint0.TrackingState == JointTrackingState.Inferred && joint1.TrackingState == JointTrackingState.Inferred)
            {
                return;
            }

            // We assume all drawn bones are inferred unless BOTH joints are tracked
            Pen drawPen = this.inferredBonePen;

            if (joint0.TrackingState == JointTrackingState.Tracked && joint1.TrackingState == JointTrackingState.Tracked)
            {
                drawPen = this.trackedBonePen;
            }

            drawingContext.DrawLine(drawPen, this.SkeletonPointToScreen(joint0.Position), this.SkeletonPointToScreen(joint1.Position));
        }


        /// <summary>
        /// Maps a SkeletonPoint to lie within our render space and converts to Point
        /// </summary>
        /// <param name="skelpoint">point to map</param>
        /// <returns>mapped point</returns>
        private Point SkeletonPointToScreen(SkeletonPoint skelpoint)
        {
            // Convert point to depth space.
            // We are not using depth directly, but we do want the points in our 640x480 output resolution.
            DepthImagePoint depthPoint = this.sensor.CoordinateMapper.MapSkeletonPointToDepthPoint(skelpoint, DepthImageFormat.Resolution640x480Fps30);
            return new Point(depthPoint.X, depthPoint.Y);
        }


        // button click method
        private void stoji_Click(object sender, RoutedEventArgs e)
        {
            double rightX = rightHand.Position.X;
            double rightY = rightHand.Position.Y;
            double rightZ = rightHand.Position.Z;

            double rightXh = heaad.Position.X;
            double rightYh = heaad.Position.Y;
            double rightZh = heaad.Position.Z;

            File.AppendAllText(@"E:\skuska.txt", rightX + ", " + rightY + ", " +rightZ + Environment.NewLine);


        }
    }
}


解决方案


这篇关于C# - 将Kinect关节位置X,Y,Z写入txt文件。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 15:32