Kinect没有跟踪Skelton [英] Kinect not tracking Skelton

查看:109
本文介绍了Kinect没有跟踪Skelton的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用System;
使用System.Collections.Generic;
使用System.Linq;
使用System.Text;使用System.Windows
;使用System.Windows.Controls
;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Shapes;
使用Microsoft.Kinect;
使用Microsoft.Kinect.Toolkit;
使用Microsoft.Kinect.Toolkit.Controls;
使用Coding4Fun.Kinect.Wpf;

名称空间KinectStrokeRecovery
{
///< summary>
/// SessionMenu.xaml的交互逻辑
///< / summary>
public partial class SessionMenu:Window
{
private KinectSensorChooser sensorChooser;
public SessionMenu()
{
InitializeComponent();
已加载+ = OnLoaded;
}
private void OnLoaded(对象发送者,RoutedEventArgs routedEventArgs)
{

this.sensorChooser = new KinectSensorChooser();
this.sensorChooser.KinectChanged + = SensorChooserOnKinectChanged;
this.sensorChooserUi.KinectSensorChooser = this.sensorChooser;
this.sensorChooser.Start();

//填充滚动内容
/ * for(int i = 1; i< 20; i ++)
{
var button = new KinectCircleButton
{
Content = i,
Height = 200
};

int i1 = i;
button.Click + =
(o,args)=> MessageBox.Show(" You clicked button#" + i1);

scrollContent.Children.Add(button);
} * /
}

private void SensorChooserOnKinectChanged(object sender,KinectChangedEventArgs args)
{
bool error = false;
if(args.OldSensor!= null)
{
try
{
args.OldSensor.DepthStream.Range = DepthRange.Default;
args.OldSensor.SkeletonStream.EnableTrackingInNearRange = false;
args.OldSensor.DepthStream.Disable();
args.OldSensor.SkeletonStream.Disable();
}
catch(InvalidOperationException)
{
// KinectSensor在启用/禁用流或流功能时可能会进入无效状态。
// E.g。:传感器可能会突然拔掉。
error = true;
}
}

if(args.NewSensor!= null)
{
try
{
args.NewSensor。 DepthStream.Enable(DepthImageFormat.Resolution640x480Fps30);
args.NewSensor.SkeletonStream.Enable();

/ * try
{
args.NewSensor.DepthStream.Range = DepthRange.Near;
args.NewSensor.SkeletonStream.EnableTrackingInNearRange = true;
args.NewSensor.SkeletonStream.TrackingMode = SkeletonTrackingMode.Seated;
}
catch(InvalidOperationException)
{
// Windows设备的非Kinect不支持Near模式,因此重置回默认模式。
args.NewSensor.DepthStream.Range = DepthRange.Default;
args.NewSensor.SkeletonStream.EnableTrackingInNearRange = false;
error = true;
} * /
}
catch(InvalidOperationException)
{
error = true;
// KinectSensor在启用/禁用流或流功能时可能会进入无效状态。
// E.g。:传感器可能会突然拔掉。
}
}

if(!error)
kinectRegion.KinectSensor = args.NewSensor;
}

private void TrackButtonOnClick(object sender,RoutedEventArgs e)
{
sensorChooser.Stop();
练习ex = new练习();
this.Close();
ex.Show();
}
}
}

using System; using System.Collections.Generic; using System.Linq; using System.Text; 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.Shapes; using Microsoft.Kinect; using Microsoft.Kinect.Toolkit; using Microsoft.Kinect.Toolkit.Controls; using Coding4Fun.Kinect.Wpf; namespace KinectStrokeRecovery { /// <summary> /// Interaction logic for SessionMenu.xaml /// </summary> public partial class SessionMenu : Window { private KinectSensorChooser sensorChooser; public SessionMenu() { InitializeComponent(); Loaded += OnLoaded; } private void OnLoaded(object sender, RoutedEventArgs routedEventArgs) { this.sensorChooser = new KinectSensorChooser(); this.sensorChooser.KinectChanged += SensorChooserOnKinectChanged; this.sensorChooserUi.KinectSensorChooser = this.sensorChooser; this.sensorChooser.Start(); //fill scroll content /* for (int i = 1; i < 20; i++) { var button = new KinectCircleButton { Content = i, Height = 200 }; int i1 = i; button.Click += (o, args) => MessageBox.Show("You clicked button #" + i1); scrollContent.Children.Add(button); }*/ } private void SensorChooserOnKinectChanged(object sender, KinectChangedEventArgs args) { bool error = false; if (args.OldSensor != null) { try { args.OldSensor.DepthStream.Range = DepthRange.Default; args.OldSensor.SkeletonStream.EnableTrackingInNearRange = false; args.OldSensor.DepthStream.Disable(); args.OldSensor.SkeletonStream.Disable(); } catch (InvalidOperationException) { // KinectSensor might enter an invalid state while enabling/disabling streams or stream features. // E.g.: sensor might be abruptly unplugged. error = true; } } if (args.NewSensor != null) { try { args.NewSensor.DepthStream.Enable(DepthImageFormat.Resolution640x480Fps30); args.NewSensor.SkeletonStream.Enable(); /* try { args.NewSensor.DepthStream.Range = DepthRange.Near; args.NewSensor.SkeletonStream.EnableTrackingInNearRange = true; args.NewSensor.SkeletonStream.TrackingMode = SkeletonTrackingMode.Seated; } catch (InvalidOperationException) { // Non Kinect for Windows devices do not support Near mode, so reset back to default mode. args.NewSensor.DepthStream.Range = DepthRange.Default; args.NewSensor.SkeletonStream.EnableTrackingInNearRange = false; error = true; }*/ } catch (InvalidOperationException) { error = true; // KinectSensor might enter an invalid state while enabling/disabling streams or stream features. // E.g.: sensor might be abruptly unplugged. } } if (!error) kinectRegion.KinectSensor = args.NewSensor; } private void TrackButtonOnClick(object sender, RoutedEventArgs e) { sensorChooser.Stop(); Exercise ex = new Exercise(); this.Close(); ex.Show(); } } }

SessionMenu.Xaml.cs

首先我在代码上面运行,当我点击TrackButtonClick()事件触发新页面打开但它不起作用时,我的意思是不跟踪骨架。  

Exercise.Xaml.cs

First i am running above code, when i clicked on TrackButtonClick() event fire new page open but its not working , i mean not tracking the skeleton.  
Exercise.Xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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.Shapes;
using Microsoft.Kinect;
using Microsoft.Kinect.Toolkit;
using Microsoft.Kinect.Toolkit.Controls;
using Coding4Fun.Kinect.Wpf;

namespace KinectStrokeRecovery
{
    /// <summary>
    /// Interaction logic for Exercise.xaml
    /// </summary>
    public partial class Exercise : Window
    {
       KinectSensor sensor = KinectSensor.KinectSensors[0];
        
        Skeleton[] skeletons;
        public Exercise()
        {
            InitializeComponent();
            
            sensor.SkeletonStream.Enable();
            sensor.SkeletonFrameReady += TrackSkeleton;
            sensor.Start();
        }
        void  TrackSkeleton(object sender, SkeletonFrameReadyEventArgs e) 
        {
            bool receivedData = false;

            using (SkeletonFrame SFrame = e.OpenSkeletonFrame())
            {
                if (SFrame == null)
                {
                    
                }
                else
                {

                    skeletons = new Skeleton[SFrame.SkeletonArrayLength];
                    SFrame.CopySkeletonDataTo(skeletons);
                    receivedData = true;
                }
            }

            if (receivedData)
            {
                // sub query expression .
                Skeleton currentSkeleton = (from skl in skeletons
                                            where skl.TrackingState == SkeletonTrackingState.Tracked
                                            select skl).FirstOrDefault();

                if (currentSkeleton != null)
                {
                    SetEllipsePosition(head, currentSkeleton.Joints[JointType.Head]);
                    SetEllipsePosition(leftHand, currentSkeleton.Joints[JointType.HandLeft]);
                    SetEllipsePosition(rightHand, currentSkeleton.Joints[JointType.HandRight]);
                    SetEllipsePosition(ElboLeft, currentSkeleton.Joints[JointType.ElbowLeft]);
                    SetEllipsePosition(ElboRight, currentSkeleton.Joints[JointType.ElbowRight]);
                    SetEllipsePosition(WristRight, currentSkeleton.Joints[JointType.WristRight]);
                    SetEllipsePosition(WristLeft, currentSkeleton.Joints[JointType.WristLeft]);
                    SetEllipsePosition(ShoulderRight, currentSkeleton.Joints[JointType.ShoulderRight]);
                    SetEllipsePosition(ShoulderLeft,currentSkeleton.Joints[JointType.ShoulderLeft]);
                    SetEllipsePosition(ShoulderCenter, currentSkeleton.Joints[JointType.ShoulderCenter]);


                
                }
            }
        }


        //This method is used to position the ellipses on the canvas
        //according to correct movements of the tracked joints.


        private void SetEllipsePosition(Ellipse ellipse, Joint joint)
        {
            var scaledJoint = joint.ScaleTo(1920, 1080, .5f, .5f);
            //Microsoft.Kinect.SkeletonPoint vector = new Microsoft.Kinect.SkeletonPoint();
            //vector.X = ScaleVector(640, joint.Position.X);
            //vector.Y = ScaleVector(480, -joint.Position.Y);
            //vector.Z = joint.Position.Z;

            //Joint updatedJoint = new Joint();
            //updatedJoint = joint;
            //updatedJoint.TrackingState = JointTrackingState.Tracked;
            //updatedJoint.Position = vector;

            Canvas.SetLeft(ellipse, scaledJoint.Position.X);
            Canvas.SetTop(ellipse, scaledJoint.Position.Y);
        }
        // Match screen resolution with kinect resolution
       /* private float ScaleVector(int length, float position)
        {
            float value = (((((float)length) / 1f) / 2f) * position) + (length / 2); // Formula to set resolution
            if (value > length)
            {
                return (float)length;
            }
            if (value < 0f)
            {
                return 0f;
            }
            return value;
        }*/

        void MainWindow_Unloaded(object sender, RoutedEventArgs e)
        {
            sensor.Stop();
        }

        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            
            
            
        }


    }
}

推荐答案

您必须将在MainWindow上创建的传感器对象传递到练习
窗口。从那里您可以订阅该活动,您无需重新打开。同样,即使关闭窗口以防止内存泄漏,也需要确保取消订阅。
You have to pass the sensor object you created on MainWindow to your Exercise window. From there you can subscribe to the event, and you don't have to re-open. As well, you need to ensure you have unsubscribed from that even when you shutdown the window to prevent memory leaks.


这篇关于Kinect没有跟踪Skelton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆