Kinect for Windows输入流

3

我基本上正在尝试使用SDK中提供的代码(ColorBasics示例)从Kinect for Windows获取RGB流作为输入。

SDK示例的代码如下:

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

    /// <summary>
    /// Bitmap that will hold color information
    /// </summary>
    private WriteableBitmap colorBitmap;

    /// <summary>
    /// Intermediate storage for the color data received from the camera
    /// </summary>
    private byte[] colorPixels;

    /// <summary>
    /// Initializes a new instance of the MainWindow class.
    /// </summary>
    public MainWindow()
    {
       //InitializeComponent();
    }

    /// <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.RgbResolution640x480Fps30);

            // Allocate space to put the pixels we'll receive
            this.colorPixels = new byte[this.sensor.ColorStream.FramePixelDataLength];

            // This is the bitmap we'll display on-screen
            this.colorBitmap = new WriteableBitmap(this.sensor.ColorStream.FrameWidth, this.sensor.ColorStream.FrameHeight, 96.0, 96.0, PixelFormats.Bgr32, null);

            // Set the image we display to point to the bitmap where we'll put the image data
            this.Image.Source = this.colorBitmap;

            // 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;
            }
        }

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

    /// <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();
        }
    }

    /// <summary>
    /// Event handler for Kinect sensor's ColorFrameReady event
    /// </summary>
    /// <param name="sender">object sending the event</param>
    /// <param name="e">event arguments</param>
    private void SensorColorFrameReady(object sender, ColorImageFrameReadyEventArgs e)
    {
        using (ColorImageFrame colorFrame = e.OpenColorImageFrame())
        {
            if (colorFrame != null)
            {
                // Copy the pixel data from the image to a temporary array
                colorFrame.CopyPixelDataTo(this.colorPixels);

                // Write the pixel data into our bitmap
                this.colorBitmap.WritePixels(
                    new Int32Rect(0, 0, this.colorBitmap.PixelWidth, this.colorBitmap.PixelHeight),
                    this.colorPixels,
                    this.colorBitmap.PixelWidth * sizeof(int),
                    0);
            }
        }
    }

    /// <summary>
    /// Handles the user clicking on the screenshot button
    /// </summary>
    /// <param name="sender">object sending the event</param>
    /// <param name="e">event arguments</param>
    private void ButtonScreenshotClick(object sender, RoutedEventArgs e)
    {
        if (null == this.sensor)
        {
            this.statusBarText.Text = Properties.Resources.ConnectDeviceFirst;
            return;
        }

        // create a png bitmap encoder which knows how to save a .png file
        BitmapEncoder encoder = new PngBitmapEncoder();

        // create frame from the writable bitmap and add to encoder
        encoder.Frames.Add(BitmapFrame.Create(this.colorBitmap));

        string time = System.DateTime.Now.ToString("hh'-'mm'-'ss", CultureInfo.CurrentUICulture.DateTimeFormat);

        string myPhotos = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);

        string path = Path.Combine(myPhotos, "KinectSnapshot-" + time + ".png");

        // write the new file to disk
        try
        {
            using (FileStream fs = new FileStream(path, FileMode.Create))
            {
                encoder.Save(fs);
            }

            this.statusBarText.Text = string.Format("{0} {1}", Properties.Resources.ScreenshotWriteSuccess, path);
        }
        catch (IOException)
        {
            this.statusBarText.Text = string.Format("{0} {1}", Properties.Resources.ScreenshotWriteFailed, path);
        }
    }
}
}

我的应用程序代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Kinect;

 namespace VideoKinect
 {
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    private KinectSensor sensor;

    private WriteableBitmap colorBitmap;

    private byte[] colorPixels;

    public MainWindow()
    {
        //InitializeComponent();
    }

    private void WindowLoaded(object sender, RoutedEventArgs e)
    {
        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.RgbResolution640x480Fps30);

            // Allocate space to put the pixels we'll receive
            this.colorPixels = new byte[this.sensor.ColorStream.FramePixelDataLength];

            // This is the bitmap we'll display on-screen
            this.colorBitmap = new WriteableBitmap(this.sensor.ColorStream.FrameWidth, this.sensor.ColorStream.FrameHeight, 96.0, 96.0, PixelFormats.Bgr32, null);

            // Set the image we display to point to the bitmap where we'll put the image data
            this.ColorImage.Source = this.colorBitmap;

            // 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;
           // }
        }

        if (null == this.sensor)
        {
            MessageBox.Show("No Kinect Available");
        }


    }

    private void SensorColorFrameReady(object sender, ColorImageFrameReadyEventArgs e)
    {
        using (ColorImageFrame colorFrame = e.OpenColorImageFrame())
        {
            if (colorFrame != null)
            {
                // Copy the pixel data from the image to a temporary array
                colorFrame.CopyPixelDataTo(this.colorPixels);

                // Write the pixel data into our bitmap
                this.colorBitmap.WritePixels(
                    new Int32Rect(0, 0, this.colorBitmap.PixelWidth, this.colorBitmap.PixelHeight),
                    this.colorPixels,
                    this.colorBitmap.PixelWidth * sizeof(int),
                    0);
            }
        }
    }
}
}

但是在我的代码中,当出现“IOException”错误时,我的图像“ColorImage”不能被检测到,即使我在xaml文件中将图像命名为相同的名称。

3个回答

3
我能够编译和执行您上面的代码并获得预期的行为 - 唯一的更改是取消注释InitializeComponent();并将<Image x:Name="ColorImage"/>添加到MainWindow.xaml中。 IOException具体指什么?您可以再次检查您的Kinect传感器是否通过至少USB 2.0总线连接-1.1带宽不足连接正确吗?您确定传感器有足够的电源,因为您需要使用kinect电源线缆。您始终可以检查SDK是否已正确安装,请参阅此帖子
关于无法找到您的ColorImage,重新构建解决方案应该会解决该问题,假设在XAML中的图像的x:Name没有拼写错误。

0

当你注释掉try ... catch语句时,你犯了错误,你把sensor = null;留在那里了,在你启动它之后,这意味着你没有传感器。你应该注释掉它。

       // What it should be:
       // try
       // {
            this.sensor.Start();
       // }
       // catch (IOException)
       // {
       //   this.sensor = null; <- What it should be
       // }

       // What it is:
       // try
       // {
            this.sensor.Start();
       // }
       // catch (IOException)
       // {
            this.sensor = null; //turns sensor null, then SensorColorFrame
                                // never gets called, etc.
       // }

那行代码最终会摧毁你编写的任何Kinect程序。希望这可以帮到你!


0

你漏掉了一个引用。你需要在最上面包含:

using System.IO;

希望这可以帮到你!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接