Click here to Skip to main content
15,885,365 members
Articles / Desktop Programming / WPF

Kinect – Getting Started – Become The Incredible Hulk

Rate me:
Please Sign up or sign in to vote.
4.90/5 (89 votes)
18 Jun 2011Ms-PL6 min read 299.6K   16K   163  
Getting Started with Kinect - Create Project, Control the Camera Angle and use Skeleton Tracking
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.Markup;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Research.Kinect.Nui;

namespace KinectGettingStarted
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        Runtime _kinectNui;

        const int RED_IDX = 2;
        const int GREEN_IDX = 1;
        const int BLUE_IDX = 0;
        byte[] depthFrame32 = new byte[320 * 240 * 4];

        int totalFrames = 0;
        int lastFrames = 0;
        DateTime lastTime = DateTime.MaxValue;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void InitializeNui()
        {
            try
            {
                //•	Declares _kinectNui as a Runtime object, which represents the Kinect sensor instance.
                _kinectNui = new Runtime();

                //Open the video and depth streams, and sets up the event handlers that the runtime calls when a video, depth, or skeleton frame is ready
                //An application must initialize the Kinect sensor by calling Runtime.Initialize before calling any other methods on the Runtime object. 
                _kinectNui.Initialize(RuntimeOptions.UseDepthAndPlayerIndex |
                                RuntimeOptions.UseSkeletalTracking | RuntimeOptions.UseColor);

                //To stream color images:
                //  •	The options must include UseColor.
                //  •	Valid image resolutions are Resolution1280x1024 and Resolution640x480.
                //  •	Valid image types are Color, ColorYUV, and ColorYUVRaw.
                _kinectNui.VideoStream.Open(ImageStreamType.Video, 2,
                                        ImageResolution.Resolution640x480, ImageType.ColorYuv);

                //To stream depth and player index data:
                //  •	The options must include UseDepthAndPlayerIndex.
                //  •	Valid resolutions for depth and player index data are Resolution320x240 and Resolution80x60.
                //  •	The only valid image type is DepthAndPlayerIndex.
                _kinectNui.DepthStream.Open(ImageStreamType.Depth, 2, ImageResolution.Resolution320x240, ImageType.DepthAndPlayerIndex);

                lastTime = DateTime.Now;

                _kinectNui.VideoFrameReady += new EventHandler<ImageFrameReadyEventArgs>(NuiVideoFrameReady);
                _kinectNui.DepthFrameReady += new EventHandler<ImageFrameReadyEventArgs>(nui_DepthFrameReady);
            }
            catch (InvalidOperationException ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        void nui_DepthFrameReady(object sender, ImageFrameReadyEventArgs e)
        {
            var Image = e.ImageFrame.Image;
            var convertedDepthFrame = convertDepthFrame(Image.Bits);

            depth.Source = BitmapSource.Create(
                Image.Width, Image.Height, 96, 96, PixelFormats.Bgr32, null, convertedDepthFrame, Image.Width * 4);

            CalculateFps();
        }

        // Converts a 16-bit grayscale depth frame which includes player indexes into a 32-bit frame
        // that displays different players in different colors
        byte[] convertDepthFrame(byte[] depthFrame16)
        {
            for (int i16 = 0, i32 = 0; i16 < depthFrame16.Length && i32 < depthFrame32.Length; i16 += 2, i32 += 4)
            {
                int player = depthFrame16[i16] & 0x07;
                int realDepth = (depthFrame16[i16 + 1] << 5) | (depthFrame16[i16] >> 3);
                // transform 13-bit depth information into an 8-bit intensity appropriate
                // for display (we disregard information in most significant bit)
                byte intensity = (byte)(255 - (255 * realDepth / 0x0fff));

                depthFrame32[i32 + RED_IDX] = intensity;
                depthFrame32[i32 + BLUE_IDX] = intensity;
                depthFrame32[i32 + GREEN_IDX] = intensity;
            }
            return depthFrame32;
        }

        private void WindowLoaded(object sender, RoutedEventArgs e)
        {
            InitializeNui();
        }

        void CalculateFps()
        {
            ++totalFrames;

            var cur = DateTime.Now;
            if (cur.Subtract(lastTime) > TimeSpan.FromSeconds(1))
            {
                int frameDiff = totalFrames - lastFrames;
                lastFrames = totalFrames;
                lastTime = cur;
                frameRate.Text = frameDiff.ToString() + " fps";
            }
        }

        void NuiVideoFrameReady(object sender, ImageFrameReadyEventArgs e)
        {
            PlanarImage Image = e.ImageFrame.Image;

            image.Source = BitmapSource.Create(
                Image.Width, Image.Height, 96, 96, PixelFormats.Bgr32, null,
                Image.Bits, Image.Width * Image.BytesPerPixel);

            imageCmyk32.Source = BitmapSource.Create(
                Image.Width, Image.Height, 96, 96, PixelFormats.Cmyk32, null,
                Image.Bits, Image.Width * Image.BytesPerPixel);
        }

        private void WindowClosed(object sender, EventArgs e)
        {
            _kinectNui.Uninitialize();
        }

        private void BtnStopClick(object sender, RoutedEventArgs e)
        {
            _kinectNui.Uninitialize();
            btnStop.IsEnabled = false;
            btnStart.IsEnabled = true;
        }

        private void BtnStartClick(object sender, RoutedEventArgs e)
        {
            InitializeNui();
            btnStop.IsEnabled = true;
            btnStart.IsEnabled = false;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


Written By
Architect Sela
Israel Israel
Shai Raiten is VS ALM MVP, currently working for Sela Group as a ALM senior consultant and trainer specializes in Microsoft technologies especially Team System and .NET technology. He is currently consulting in various enterprises in Israel, planning and analysis Load and performance problems using Team System, building Team System customizations and adjusts ALM processes for enterprises. Shai is known as one of the top Team System experts in Israel. He conducts lectures and workshops for developers\QA and enterprises who want to specialize in Team System.

My Blog: http://blogs.microsoft.co.il/blogs/shair/

Comments and Discussions