2

我是 Kinect 和 VS 开发的新手,我使用的是Windows Kinect Quickstart Series,但不幸的是,它适用于 Kinect SDK v1.00。

我被 KinectColorView 的使用卡住了,我无法继续(我已经用谷歌搜索了 2 天了)。我还发现了这个非常相似的问题:在 StackOverflow 上的 SDK1.5 中使用 KinectColorViewer但无法解决我的问题。我知道我很接近,但我需要帮助。

问题是:我无法让我的 KinectColorViewer 工作。

我将它添加到 XAML 设计器,但似乎我无法将它绑定到 SensorManager。这是我的 XAML 代码:

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:WpfViewers="clr-namespace:Microsoft.Samples.Kinect.WpfViewers;assembly=Microsoft.Samples.Kinect.WpfViewers" xmlns:Toolkit="clr-namespace:Microsoft.Kinect.Toolkit;assembly=Microsoft.Kinect.Toolkit" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="KinectSetupDev.MainWindow"
    Title="MainWindow" Height="768" Width="1024" Loaded="Window_Loaded_1" Closed="Window_Closed_1">

    <Grid HorizontalAlignment="Left" Height="736" VerticalAlignment="Top" Width="1012" Margin="2,2,0,0">
         <TextBlock x:Name="tbMessages" HorizontalAlignment="Left" Height="60" Margin="10,662,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="992"/>
         <WpfViewers:KinectColorViewer x:Name="KinectColorViewer" KinectSensorManager="{Binding KinectSensorManager}" HorizontalAlignment="Left" Height="240" Margin="608,27,0,0" VerticalAlignment="Top" Width="320" BorderThickness="1" BorderBrush="Black"/>
    </Grid>
</Window>

没有关于 XAML 代码的报告,除了我必须添加块

"KinectSensorManager="{Binding KinectSensorManager}"

在 KinectColorViewer 项目中通过编写它,因为我无法通过 XAML 设计器的 GUI 找到解决方法(所以我对它的可行性有些怀疑)。

这是我的代码隐藏(我几乎完全复制了它的问题:Using KinectColorViewer in SDK1.5 so I can't understand why it's not working!!

namespace KinectSetupDev
{
  public partial class MainWindow : Window
  {
    // Automatically finds a Kinect for you.
    private readonly KinectSensorChooser sensorChooser = new KinectSensorChooser();

    public KinectSensorManager KinectSensorManager01 { get; private set; }

    public MainWindow()
    {
        InitializeComponent();
        DataContext = this;

    }

    private void Window_Loaded_1(object sender, RoutedEventArgs e)
    {
        // Initialize the Kinect sensor manager.
        KinectSensorManager01 = new KinectSensorManager();
        KinectSensorManager01.KinectSensorChanged += this.KinectSensorChanged;

        // Locate an available sensor.
        sensorChooser.Start();

        // Bind chooser's sensor value to the local sensor manager.
        var kinectSensorBinding = new Binding("Kinect") { Source = this.sensorChooser };
        BindingOperations.SetBinding(this.KinectSensorManager01, KinectSensorManager.KinectSensorProperty, kinectSensorBinding);
    }

    private void Window_Closed_1(object sender, EventArgs e)
    {
        UninitializeKinectServices(KinectSensorManager01.KinectSensor);
    }

    #region Kinect Discovery & Setup

    private void KinectSensorChanged(object sender, KinectSensorManagerEventArgs<KinectSensor> args)
    {
        if (null != args.OldValue)  
            UninitializeKinectServices(args.OldValue);

        if (null != args.NewValue)
            InitializeKinectServices(KinectSensorManager01, args.NewValue);
    }

    // Kinect enabled apps should customize which Kinect services it initializes here.
    private void InitializeKinectServices(KinectSensorManager kinectSensorManager, KinectSensor sensor)
    {
        // Application should enable all streams first.
        kinectSensorManager.ColorStreamEnabled = true;
        kinectSensorManager.DepthStreamEnabled = true;
        kinectSensorManager.SkeletonStreamEnabled = true;

        // Start the sensor!
        try
        {
            sensor.Start();
            sensorChooser.Kinect.ElevationAngle = 10;
            tbMessages.Text = "Kinect Started";
        }
        catch (System.IO.IOException)
        {
            sensor = null;
            tbMessages.Text = "Kinect Not Started" + "\r";
        }
    }

    // Kinect enabled apps should uninitialize all Kinect services that were initialized in InitializeKinectServices() here.
    private void UninitializeKinectServices(KinectSensor sensor)
    {
        if (sensor != null)
        {

            if (sensor.IsRunning)
            {
                sensorChooser.Kinect.ElevationAngle = 0;
                sensor.Stop();
                sensor.AudioSource.Stop();

            }
        }
    }

    #endregion Kinect Discovery & Setup

  }
}

我试图在构造函数中添加该行 DataContext = this; ,但它不起作用。

应用程序运行,我的 Kinect 启动,但 KinectColorViewer 不显示任何内容。

此外,谁能说出和之间的 sensor.SkeletonStream.Enable(); 区别 kinectSensorManager.SkeletonStreamEnabled = true; ??

4

2 回答 2

3

在您的 XAML 中,您绑定到KinectSensorManager,它不存在。您在代码中调用它KinectSensorManager01,因此您需要在 XAML 中正确反映它。

这可能会解决它。但我看到的另一件事是,您至少以 3 种不同的方式访问传感器。这涉及到你的第二个问题。

你用sensor.SkeletonStream.Enabled()and做同样的事情kinectSensorManager.SkeletonStreamEnabled = true,你只是用不同的方式去做。这KinectSensorManager是一个包裹在 Kinect 硬件上的数据模型,它为你管理许多平凡的小事情。如果您要使用它(我个人一直都这样做),那么您应该始终使用它来访问硬件。这将确保您的参考资料永远不会不同步。

因此,在您的InitializeKinectServer函数中,您不想使用sensor.Start()and sensorChooser.Kinect.ElevationAngle。您想使用相应的KinectSensorManager调用。

这是一个快速而肮脏的应用程序,仅显示 KinectColorChooser:

主窗口.xaml

<Window x:Class="SimpleKinectStart.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:viewer="clr-namespace:Microsoft.Samples.Kinect.WpfViewers;assembly=Microsoft.Samples.Kinect.WpfViewers"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <viewer:KinectColorViewer KinectSensorManager="{Binding KinectSensorManager}" />
    </Grid>
</Window>

主窗口.xaml.cs

using System.ComponentModel;
using System.Windows;
using System.Windows.Data;
using Microsoft.Kinect;
using Microsoft.Kinect.Toolkit;
using Microsoft.Samples.Kinect.WpfViewers;

namespace SimpleKinectStart
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private readonly KinectSensorChooser _sensorChooser = new KinectSensorChooser();

        public MainWindow()
        {
            InitializeComponent();

            DataContext = this;

            if (DesignerProperties.GetIsInDesignMode(this))
            {
                // do something special, only for design mode
            }
            else
            {
                KinectSensorManager = new KinectSensorManager();
                KinectSensorManager.KinectSensorChanged += OnKinectSensorChanged;

                _sensorChooser.Start();

                if (_sensorChooser.Kinect == null)
                {
                    MessageBox.Show("Unable to detect an available Kinect Sensor");
                    Application.Current.Shutdown();
                }

                // Bind the KinectSensor from the sensorChooser to the KinectSensor on the KinectSensorManager
                var kinectSensorBinding = new Binding("Kinect") { Source = _sensorChooser };
                BindingOperations.SetBinding(this.KinectSensorManager, KinectSensorManager.KinectSensorProperty, kinectSensorBinding);
            }
        }

        #region Kinect Discovery & Setup

        private void OnKinectSensorChanged(object sender, KinectSensorManagerEventArgs<KinectSensor> args)
        {
            if (null != args.OldValue)
                UninitializeKinectServices(args.OldValue);

            if (null != args.NewValue)
                InitializeKinectServices(KinectSensorManager, args.NewValue);
        }

        /// <summary>
        /// Initialize Kinect based services.
        /// </summary>
        /// <param name="kinectSensorManager"></param>
        /// <param name="sensor"></param>
        private void InitializeKinectServices(KinectSensorManager kinectSensorManager, KinectSensor sensor)
        {
            // configure the color stream
            kinectSensorManager.ColorFormat = ColorImageFormat.RgbResolution640x480Fps30;
            kinectSensorManager.ColorStreamEnabled = true;

            // configure the depth stream
            kinectSensorManager.DepthStreamEnabled = true;

            kinectSensorManager.TransformSmoothParameters =
                new TransformSmoothParameters
                {
                    // as the smoothing value is increased responsiveness to the raw data
                    // decreases; therefore, increased smoothing leads to increased latency.
                    Smoothing = 0.5f,
                    // higher value corrects toward the raw data more quickly,
                    // a lower value corrects more slowly and appears smoother.
                    Correction = 0.5f,
                    // number of frames to predict into the future.
                    Prediction = 0.5f,
                    // determines how aggressively to remove jitter from the raw data.
                    JitterRadius = 0.05f,
                    // maximum radius (in meters) that filtered positions can deviate from raw data.
                    MaxDeviationRadius = 0.04f
                };

            // configure the skeleton stream
            kinectSensorManager.SkeletonStreamEnabled = true;

            // enable the sensor
            kinectSensorManager.KinectSensorEnabled = true;
        }

        /// <summary>
        /// Uninitialize all Kinect services that were initialized in InitializeKinectServices.
        /// </summary>
        /// <param name="sensor"></param>
        private void UninitializeKinectServices(KinectSensor sensor)
        {
            // do what needs to be done
        }

        #endregion Kinect Discovery & Setup

        #region Properties

        public KinectSensorManager KinectSensorManager { get; private set; }

        #endregion Properties
    }
}

使用时,KinectSensorManager您总是希望尽可能使用传感器管理器提供的包装器。因此,您可能希望kinectSensorManager.ElevationAngle在 init 函数或KinectSensorManager.ElevationAngle其他地方使用(注意本地调用与全局调用之间的区别)。这允许传感器管理器尝试并按照您的要求进行操作,并在需要时优雅地失败。

在该UninitializeKinectServices功能中,您只会直接处理传感器。当您点击此功能时,KinectSensorChooser要么选择了新的 Kinect,要么丢失了它并将传感器设置为null。您正在处理此函数中的传感器值,清理曾经链接到它的服务。您所做的任何参考都KinectSensorManager将是新传感器或null参考。

于 2012-11-06T19:27:37.383 回答
0

我手动添加了 Path 属性,它对我有用:

<WpfViewers:KinectColorViewer HorizontalAlignment="Left" Height="221" Margin="75,84,0,0" VerticalAlignment="Top" Width="372" Kinect="{Binding ElementName=kinectSensorChooser1, Mode=OneWay,Path=Kinect}"/>
于 2012-11-11T18:54:32.720 回答