我是Kinect和VS Development的新手,我使用的是Windows Kinect Quickstart Series,但不幸的是,它是针对Kinect SDK v1.00的。
我对KinectColorView的使用感到困惑,我无法继续(我已经Googlin呆了2天了)。我也发现了一个非常相似的问题:Using KinectColorViewer in SDK1.5在StackOverflow上,但无法解决我的问题。我知道我已经接近了,但是我需要帮助。
问题是:我无法使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,所以我不明白为什么它不起作用!)
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;
?? 最佳答案
在您的XAML中,您绑定到不存在的KinectSensorManager
。您在代码中将其称为KinectSensorManager01
,因此需要在XAML中正确反映出来。
那可能会解决它。但是我看到的另一件事是,您以至少3种不同的方式访问传感器。这是您的第二个问题。
您使用sensor.SkeletonStream.Enabled()
和kinectSensorManager.SkeletonStreamEnabled = true
做相同的事情,只是以不同的方式进行。 KinectSensorManager
是包裹在Kinect硬件周围的数据模型,它可以为您管理许多平凡的小事情。如果要使用它(我一直都在使用),则应始终使用它来访问硬件。这将确保您的引用不会永不同步。
因此,在您的InitializeKinectServer
函数中,您不想使用sensor.Start()
和sensorChooser.Kinect.ElevationAngle
。您要使用相应的KinectSensorManager
调用。
这是一个快速肮脏的应用程序,仅显示KinectColorChooser:
MainWindow.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>
MainWindow.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
时,您总是尽可能使用传感器管理器提供的包装纸。因此,您可能想在init函数中使用kinectSensorManager.ElevationAngle
,或在其他地方使用KinectSensorManager.ElevationAngle
(请注意本地调用与全局调用之间的区别)。这使传感器管理器可以尝试执行您要求的操作,并在需要时适当地失败。在
UninitializeKinectServices
功能中,您将只能直接处理传感器。按下此功能时,KinectSensorChooser
选择了新的Kinect或丢失了它,并将传感器设置为null
。您正在使用此功能处理旧的传感器值,从而清理了曾经与之链接的服务。您对KinectSensorManager
所做的任何引用都将是新传感器或null
引用。