在SDK1.5中使用KinectColorViewer

时间:2012-09-26 20:28:50

标签: kinect kinect-sdk

我正在尝试在使用Kinect for windows(sdk 1.5)的项目中使用KinectColorViewer。在kinect资源管理器示例中,KinectColorViewer组件具有绑定的KinectSensorManager组件。在xaml文件中,我们有:

<kt:KinectColorViewer x:Name="ColorViewer" KinectSensorManager="{Binding KinectSensorManager}" CollectFrameRate="True" RetainImageOnSensorChange="True" />

在其他项目中重现相同的概念我遇到了很多麻烦。我使用过Microsoft.Kinect.Toolkit的KinectSensorChooser,KinectSensorChooserUI和Mirosoft.Sampels.Kinect.wpfviewers KinectColorViewer。试图将colorViewer的KinectSensorManager绑定到UI元素并遵循。

<my:KinectColorViewer Width="200" Height="200" HorizontalAlignment="Left" Margin="198,12,0,0" Name="kinectColorViewer1" VerticalAlignment="Top" KinectSensorManager="{Binding ElementName=kinectSensorChooserUI1, Path=KinectSensorChooser.Kinect}" />
两次尝试都是不成功的。有没有人使用新的SDK使用ColorViwer,DepthViwer和SkeletonViewer?确定我们会很棒......

为了绑定KinectSensorManager,我在后端添加了以下代码:

public partial class MainWindow : Window
{
  public KinectSensorManager KinectSensorManager { get; set;}
  private void WindowLoaded(object sender, RoutedEventArgs e)
{
  KinectSensorManager = new KinectSensorManager();

  KinectSensorManager.KinectSensorChanged += new EventHandler<KinectSensorManagerEventArgs<KinectSensor>>(KinectSensorManager_KinectSensorChanged);

        // Look through all sensors and start the first connected one.
        // This requires that a Kinect is connected at the time of app startup.
        foreach (var potentialSensor in KinectSensor.KinectSensors)
        {
            if (potentialSensor.Status == KinectStatus.Connected)
            {
                this.sensor = potentialSensor;
                break;
            }
        }

        if (null != this.sensor)
        {
            // Turn on the skeleton stream to receive skeleton frames

            this.sensor.SkeletonStream.Enable();
            this.sensor.DepthStream.Enable();
            this.sensor.ColorStream.Enable(ColorImageFormat.RgbResolution640x480Fps30);
            this.sensor.AllFramesReady += new System.EventHandler<AllFramesReadyEventArgs>(sensor_AllFramesReady);
            this.sensor.SkeletonFrameReady += this.SensorSkeletonFrameReady;

            // Start the sensor!
            try
            {
                this.sensor.Start();
            }
            catch (IOException)
            {
                this.sensor = null;
            }

            var kinectSensorBinding = new Binding("KinectSensor") { Source = this.sensor };
            BindingOperations.SetBinding(this.KinectSensorManager, KinectSensorManager.KinectSensorProperty, kinectSensorBinding);

        }

        if (null == this.sensor)
        {
            this.statusBarText.Text = Properties.Resources.NoKinectReady;
        }

    }

和Xaml文件:

<kt:KinectColorViewer Width="191" Height="83" Grid.Column="3" KinectSensorManager="{Binding KinectSensorManager}" HorizontalAlignment="Left" Margin="17,236,0,0" Name="kinectColorViewer1" VerticalAlignment="Top" />

但我仍然没有得到任何颜色的流。

1 个答案:

答案 0 :(得分:4)

您的绑定声明不正确。 “KinectSensorChooser.Kinect”是对KinectSensorChooser选择的硬件(即Kinect)的引用 - 它不是KinectSensorManager。

您应该以与示例相同的方式绑定到KinectSensorManager。你有什么特别的尝试,会导致你以不同的方式绑定它吗?

你的主要课程应该是这样的:

// automatically finds a Kinect for you
private readonly KinectSensorChooser sensorChooser = new KinectSensorChooser();

// the bindable sensor property
public KinectSensorManager KinectSensorManager { get; private set; }

public MainViewModel()
{
    if (IsInDesignMode) {
        // load design mode only content
    }
    else
    {
        // initialize the Kinect sensor manager
        KinectSensorManager = new KinectSensorManager();
        KinectSensorManager.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.KinectSensorManager,
            KinectSensorManager.KinectSensorProperty,
            kinectSensorBinding);
    }
}

#region Kinect Discovery & Setup

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

    if (null != args.NewValue)
        InitializeKinectServices(KinectSensorManager, 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.

    // configure the color stream
    kinectSensorManager.ColorFormat = 
        ColorImageFormat.RgbResolution640x480Fps30;
    kinectSensorManager.ColorStreamEnabled = true;

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

    kinectSensorManager.TransformSmoothParameters = 
        new TransformSmoothParameters
        {
            Smoothing = 0.5f,
            Correction = 0.5f,
            Prediction = 0.5f,
            JitterRadius = 0.05f,
            MaxDeviationRadius = 0.04f
        };

    // configure the skeleton stream
    sensor.SkeletonFrameReady += OnSkeletonFrameReady;
    kinectSensorManager.SkeletonStreamEnabled = true;
}

// Kinect enabled apps should uninitialize all Kinect services that were initialized in InitializeKinectServices() here.
private void UninitializeKinectServices(KinectSensor sensor)
{
    sensor.SkeletonFrameReady -= this.OnSkeletonFrameReady;
}

#endregion Kinect Discovery & Setup

然后,您可以从View中绑定到管理器,如示例所示。

您还需要通过将以下内容放入构造函数中来设置DataContext:     DataContext = this;