C# Kinect错误启用流

C# Kinect错误启用流,c#,kinect,kinect-sdk,C#,Kinect,Kinect Sdk,这是我第一次尝试制作一个使用Kinect的程序,我不知道为什么总是出现null错误。也许更了解KinectSDK的人可以帮忙 public ProjKinect() { InitializeComponent(); updateSensor(0);//set current sensor as 0 since we just started } public void updateSensor(int sensorI) { refreshSensors();//see

这是我第一次尝试制作一个使用Kinect的程序,我不知道为什么总是出现
null
错误。也许更了解KinectSDK的人可以帮忙

public ProjKinect()
{
    InitializeComponent();
    updateSensor(0);//set current sensor as 0 since we just started
}

public void updateSensor(int sensorI)
{
    refreshSensors();//see if any new ones connected
    if (sensorI >= sensors.Length)//if it goes to end, then repeat
    {
        sensorI = 0;
    }
    currentSensorInt = sensorI;
    if (activeSensor != null && activeSensor.IsRunning)
    {
        activeSensor.Stop();//stop so we can cahnge
    }
    MessageBox.Show(sensors.Length + " Kinects Found");
    activeSensor = KinectSensor.KinectSensors[currentSensorInt];
    activeSensor.ColorStream.Enable(ColorImageFormat.RgbResolution640x480Fps30); //ERROR IS RIGHT HERE
    activeSensor.DepthStream.Enable();
    activeSensor.SkeletonStream.Enable();
    activeSensor.SkeletonFrameReady += runtime_SkeletonFrameReady;
    activeSensor.DepthFrameReady += runtime_DepthFrameReady;
    activeSensor.ColorFrameReady += runtime_ImageFrameReady;
    activeSensor.Start();//start the newly enabled one
}
public void refreshSensors()
{
    sensors = KinectSensor.KinectSensors.ToArray();
}
错误:

对象引用未设置为对象的实例。
顺便说一句,它说我连接了1个Kinect,所以我知道它至少能识别出我有东西要连接。如果我只说
0
而不是
currentSensorInt
,它也不起作用。如果注释掉
ColorStream.Enable
,则
DepthStream.Enable也会出现错误。所以我猜我只是在创建传感器时做错了什么

希望是小东西。
提前感谢:)

我没有看到任何明显的错误,但我以前也没有看到传感器以这种方式采集和设置。你看过这些例子了吗?关于如何连接到Kinect,有多个示例,其中一些是简单的蛮力连接,而另一些则非常健壮

例如,这是幻灯片手势WPF示例中连接代码的精简版本:

public partial class MainWindow : Window
{
    /// <summary>
    /// Active Kinect sensor
    /// </summary>
    private KinectSensor sensor;

    /// <summary>
    /// Execute startup tasks
    /// </summary>
    /// <param name="sender">object sending the event</param>
    /// <param name="e">event arguments</param>
    private void WindowLoaded(object sender, RoutedEventArgs e)
    {
        // Look through all sensors and start the first connected one.
        // This requires that a Kinect is connected at the time of app startup.
        // To make your app robust against plug/unplug, 
        // it is recommended to use KinectSensorChooser provided in Microsoft.Kinect.Toolkit
        foreach (var potentialSensor in KinectSensor.KinectSensors)
        {
            if (potentialSensor.Status == KinectStatus.Connected)
            {
                this.sensor = potentialSensor;
                break;
            }
        }

        if (null != this.sensor)
        {
            // Turn on the color stream to receive color frames
            this.sensor.ColorStream.Enable(ColorImageFormat.InfraredResolution640x480Fps30);

            // Add an event handler to be called whenever there is new color frame data
            this.sensor.ColorFrameReady += this.SensorColorFrameReady;

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

    /// <summary>
    /// Execute shutdown tasks
    /// </summary>
    /// <param name="sender">object sending the event</param>
    /// <param name="e">event arguments</param>
    private void WindowClosing(object sender, System.ComponentModel.CancelEventArgs e)
    {
        if (null != this.sensor)
        {
            this.sensor.Stop();
        }
    }
}
就这样。我有一个传感器,我可以开始使用它了。我如何连接和控制Kinect的较大示例使用工具包中的
KinectSensorManager
类,该类位于
KinectWpfViewers
命名空间中:

public class MainViewModel : ViewModelBase
{
    private readonly KinectSensorChooser _sensorChooser = new KinectSensorChooser();

    /// <summary>
    /// Initializes a new instance of the MainViewModel class.
    /// </summary>
    public MainViewModel(IDataService dataService)
    {
        if (IsInDesignMode)
        {
            // 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
        sensor.SkeletonFrameReady += OnSkeletonFrameReady;
        kinectSensorManager.SkeletonStreamEnabled = true;

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

        kinectSensorManager.KinectSensorEnabled = true;

        if (!kinectSensorManager.KinectSensorAppConflict)
        {
            // set up addition Kinect based services here
            // (e.g., SpeechRecognizer)
        }

        kinectSensorManager.ElevationAngle = Settings.Default.KinectAngle;
    }

    /// <summary>
    /// Uninitialize all Kinect services that were initialized in InitializeKinectServices.
    /// </summary>
    /// <param name="sensor"></param>
    private void UninitializeKinectServices(KinectSensor sensor)
    {
        sensor.SkeletonFrameReady -= this.OnSkeletonFrameReady;
    }

    #endregion Kinect Discovery & Setup

    #region Properties

    public KinectSensorManager KinectSensorManager { get; private set; }

    #endregion Properties
}
public类MainViewModel:ViewModelBase
{
专用只读KinectSensorChooser _sensorChooser=新KinectSensorChooser();
/// 
///初始化MainViewModel类的新实例。
/// 
公共主视图模型(IDataService数据服务)
{
如果(IsInDesignMode)
{
//做一些特别的事情,只针对设计模式
}
其他的
{
KinectSensorManager=新的KinectSensorManager();
KinectSensorManager.KinectSensorChanged+=OnKinectSensorChanged;
_sensorChooser.Start();
如果(_sensorChooser.Kinect==null)
{
MessageBox.Show(“无法检测可用的Kinect传感器”);
Application.Current.Shutdown();
}
//将传感器选择器中的Kinect传感器绑定到Kinect传感器管理器上的Kinect传感器
var kinectSensorBinding=新绑定(“Kinect”){Source=\u sensorChooser};
BindingOperations.SetBinding(this.KinectSensorManager、KinectSensorManager.KinectSensorProperty、kinectSensorBinding);
}
}
#区域Kinect发现和设置
KinectSensorChanged上的私有无效(对象发送方,KinectSensorManagerEventArgs args)
{
if(null!=args.OldValue)
取消初始化KinectServices(args.OldValue);
if(null!=args.NewValue)
初始化KinectServices(KinectSensorManager,args.NewValue);
}
/// 
///初始化基于Kinect的服务。
/// 
/// 
/// 
private void InitializeKinectServices(Kinect传感器管理器Kinect传感器管理器,Kinect传感器)
{
//配置颜色流
kinectSensorManager.ColorFormat=ColorImageFormat.RgbResolution640x480Fps30;
kinectSensorManager.ColorStreamEnabled=true;
//配置深度流
kinectSensorManager.DepthStreamEnabled=true;
kinectSensorManager.TransformSmoothParameters=
新参数
{
//随着平滑值的增加,对原始数据的响应性增强
//减少;因此,增加平滑会导致延迟增加。
平滑度=0.5f,
//值越高,对原始数据的校正速度越快,
//值越小,校正速度越慢,看起来越平滑。
修正值=0.5f,
//要预测未来的帧数。
预测值=0.5f,
//确定从原始数据中删除抖动的力度。
吉特拉迪乌斯=0.05f,
//过滤位置可能偏离原始数据的最大半径(米)。
最大偏差半径=0.04f
};
//配置骨架流
sensor.SkeletonFrameReady+=OnSkeletonFrameReady;
kinectSensorManager.SkeletonStreamEnabled=true;
//初始化手势识别器
_gestureController=新的gestureController();
_gestureController.GestureReceignized+=OnGestureReceignized;
kinectSensorManager.KinectSensorEnabled=true;
如果(!kinectSensorManager.KinectSensorAppConflict)
{
//在此处设置基于Kinect的附加服务
//(例如,语音识别器)
}
kinectSensorManager.ElevationAngle=Settings.Default.KinectAngle;
}
/// 
///取消初始化在InitializeKinect services中初始化的所有Kinect服务。
/// 
/// 
private void取消初始化Kinect服务(Kinect传感器)
{
sensor.SkeletonFrameReady-=this.OnSkeletonFrameReady;
}
#端域Kinect发现与设置
#区域属性
公共KinectSensorManager KinectSensorManager{get;private set;}
#端域属性
}

所有这些额外代码的优点可以在工具包中的
KinectExplorer
示例中看到。简言之,我可以用这段代码管理多个Kinect,拔下一个,程序就会切换到另一个。

谢谢你的回答。但不知何故,它只是在多次拔下和插回kinect后才开始工作。我猜这只是一个奇怪的小故障,因为它现在可以工作了。感谢所有的示例代码。确实帮助我掌握SDK。是的,确实如此。我还不知道为什么。您有时会在
Kinect.dll
中遇到“invalidooperation”异常,这通常是在拔下Kinect并将其重新插入时解决的。保持冷静
public class MainViewModel : ViewModelBase
{
    private readonly KinectSensorChooser _sensorChooser = new KinectSensorChooser();

    /// <summary>
    /// Initializes a new instance of the MainViewModel class.
    /// </summary>
    public MainViewModel(IDataService dataService)
    {
        if (IsInDesignMode)
        {
            // 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
        sensor.SkeletonFrameReady += OnSkeletonFrameReady;
        kinectSensorManager.SkeletonStreamEnabled = true;

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

        kinectSensorManager.KinectSensorEnabled = true;

        if (!kinectSensorManager.KinectSensorAppConflict)
        {
            // set up addition Kinect based services here
            // (e.g., SpeechRecognizer)
        }

        kinectSensorManager.ElevationAngle = Settings.Default.KinectAngle;
    }

    /// <summary>
    /// Uninitialize all Kinect services that were initialized in InitializeKinectServices.
    /// </summary>
    /// <param name="sensor"></param>
    private void UninitializeKinectServices(KinectSensor sensor)
    {
        sensor.SkeletonFrameReady -= this.OnSkeletonFrameReady;
    }

    #endregion Kinect Discovery & Setup

    #region Properties

    public KinectSensorManager KinectSensorManager { get; private set; }

    #endregion Properties
}