0

我有一个程序可以检查 Kinect 是否连接到计算机。但是,我真的不知道我是否需要调用一个方法(我会假设如此)以及在哪里?我附上了从介绍 Kinect 书籍中获得的代码。谢谢!

using System;
using System.Windows;
using System.Windows.Controls;
using Microsoft.Kinect;

namespace test
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

        }

    KinectSensor kinectSensor;
    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        try
        {
            KinectSensor.KinectSensors.StatusChanged += Kinects_StatusChanged;
            foreach (KinectSensor kinect in KinectSensor.KinectSensors)
            {
                if (kinect.Status == KinectStatus.Connected)
                {
                    kinectSensor = kinect;
                    MessageBox.Show("Connected");
                    break;
                }
            }
            if (KinectSensor.KinectSensors.Count == 0)
                MessageBox.Show("No Kinect Found");
            else
                Initialize();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

    void Kinects_StatusChanged(object sender, StatusChangedEventArgs e)
    {
        switch (e.Status)
        {
            case KinectStatus.Connected:
                if (kinectSensor == null)
                {
                    kinectSensor = e.Sensor;
                    Initialize();
                }
                break;
            case KinectStatus.Disconnected:
                if (kinectSensor == e.Sensor)
                {
                    Clean();
                    MessageBox.Show("Kinect was disconnected");
                }
                break;
            case KinectStatus.NotReady:
                break;
            case KinectStatus.NotPowered:
                if (kinectSensor == e.Sensor)
                {
                    Clean();
                    MessageBox.Show("Kinect is not powered anymore.");
                }
                break;
            default:
                MessageBox.Show("Unhandled Status: " + e.Status);
                break;
        }
    }

    private void Initialize()
    {
        if (kinectSensor == null)
            return;
        kinectSensor.Start();
    }

    private void Clean()
    {
        if (kinectSensor != null)
        {
            kinectSensor.Stop();
            kinectSensor = null;
        }
    }

}

}

4

1 回答 1

1

下载Kinect for Windows 开发者工具包。那里有多个关于如何做多种事情的示例,它们将帮助您入门并帮助您了解如何与 Kinect 交谈。

连接到 Kinect 后,您需要对其进行设置,然后订阅事件回调。你最终会得到一个看起来像这样的函数:

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
    sensor.SkeletonFrameReady += OnSkeletonFrameReady;
    kinectSensorManager.SkeletonStreamEnabled = true;

    // initialize the gesture recognizer
    _gestureController = new GestureController();
    _gestureController.GestureRecognized += OnGestureRecognized;

    kinectSensorManager.KinectSensorEnabled = true;

    if (!kinectSensorManager.KinectSensorAppConflict)
    {
      // additional init
    }
}

这是我的通用设置函数,它基于 Developer Toolkit 中的示例。您将无法仅将其插入您的代码中,它会起作用。查看工具包中的示例将使您了解发生这种情况的位置以及如何最好地管理它。

这个KinectExplorer例子是一个很好的整体项目。它还将让您清楚地了解上述功能的工作原理(它具有相同的功能)。

于 2012-12-04T16:10:00.593 回答