3

I am basically trying to take input as RGB Stream from Kinect for Windows using the same code as given in the SDK (ColorBasics Example).

The code of the Example in SDK is as follow

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

And the code in my application is as follows

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

But I am getting the Error at "IOException" in my code and the Image "ColorImage" is not being detected even though I named the image in my xaml file as the same.

0

3 Answers 3

3

I was able to compile and execute your code above with the expected behaviour - the only change was uncommenting the InitializeComponent(); and adding <Image x:Name="ColorImage"/> to the MainWindow.xaml.

What exactly does the IOException say? Can you double check that you Kinect sensor is connected properly via at least a USB 2.0 bus - 1.1 has insufficient bandwidth? Are you also sure that the sensor has sufficient power as you will need the kinect power supply cable. You could always check to see if the SDK is installed properly, have a look at this post.

With regards to it not finding your ColorImage rebuilding the solution should resolve the issue, assuming there is not a spelling mistake on the x:Name of the Image in XAML.

0

You messed up when you commented out the try ... catch statement, you left sensor = null; in there, after you started it, meaning you had no sensor. You should have commented that out.

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

That line will ultimately destroy any Kinect program you ever write. Hope this helps!

0

you left out a reference. you need to include:

using System.IO;

at the very top. Hope This helps!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.