Click here to Skip to main content
12,447,778 members (71,204 online)
Click here to Skip to main content

Stats

33.3K views
934 downloads
27 bookmarked
Posted

Getting most of the Kinect SDK in C# - Part 1 of ?: Initialization

, 6 Jan 2012 Ms-PL
A series about experimenting with Kinect for Windows SDK.
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.Research.Kinect.Nui;

namespace JK.KinectExperiments.ImageStreamTest
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        Runtime CurrentRuntime;
        InteropBitmapHelper imageHelper;
        
        PlanarImageHelper pih = null;
        Byte[] bg = new Byte[1280 * 1024 * 4];
        
        
        public MainWindow()
        {
            InitializeComponent();
            CurrentRuntime = Runtime.Kinects[0];
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            CurrentRuntime.Initialize(
                RuntimeOptions.UseDepthAndPlayerIndex

                                        | RuntimeOptions.UseSkeletalTracking
                                        | RuntimeOptions.UseColor
                                        );

            CurrentRuntime.VideoStream.Open(ImageStreamType.Video, 2, ImageResolution.Resolution640x480, ImageType.Color);
            CurrentRuntime.DepthStream.Open(ImageStreamType.Depth, 2, ImageResolution.Resolution320x240, ImageType.DepthAndPlayerIndex);


            CurrentRuntime.VideoFrameReady += new EventHandler<ImageFrameReadyEventArgs>(CurrentRuntime_VideoFrameReady);
            CurrentRuntime.DepthFrameReady += new EventHandler<ImageFrameReadyEventArgs>(CurrentRuntime_DepthFrameReady);

        }


        


        void CurrentRuntime_DepthFrameReady(object sender, ImageFrameReadyEventArgs e)
        {
        
            pih = new PlanarImageHelper(e.ImageFrame.Image);
        }


        
        void CurrentRuntime_VideoFrameReady(object sender, ImageFrameReadyEventArgs e)
        {

            PlanarImage planarImage = e.ImageFrame.Image;


            double scale = (double)pih.Image.Width / (double)planarImage.Width;
        
            for (int y = 0; y < planarImage.Height; y++)
                for (int x = 0; x < planarImage.Width; x++)
                {
                    int baseindex = y * planarImage.Width * planarImage.BytesPerPixel + x * planarImage.BytesPerPixel;
            
                    switch (((ComboBoxItem)cbEffect.SelectedItem).Content.ToString())
                    {
                        case "Depth":
                            double mi = slider1.Value;
                            double mx = slider2.Value;
                            int cr = pih.GetDepthAt((int)Math.Floor(x * scale), (int)Math.Floor(y * scale), true);
                            if (cr > slider1.Maximum)
                            {
                                slider1.Maximum = cr;
                                slider2.Maximum = cr;
                            }
                            if (cr > mx || cr < mi)
                            {
                                planarImage.Bits[baseindex + 0] = 255;
                                planarImage.Bits[baseindex + 1] = 255;
                                planarImage.Bits[baseindex + 2] = 255;
                                planarImage.Bits[baseindex + 3] = 255;
                            }
                            break;

                        case "Player":

                            int pi = pih.GetPlayerAt((int)Math.Floor(x * scale), (int)Math.Floor(y * scale));
                            if (pi == 0)
                            {
                                planarImage.Bits[baseindex + 0] = 255;
                                planarImage.Bits[baseindex + 1] = 255;
                                planarImage.Bits[baseindex + 2] = 255;
                                planarImage.Bits[baseindex + 3] = 0;
                            }
                            break;

                        case "Background":

                            bool isbg = pih.GetPlayerAt((int)Math.Floor(x * scale), (int)Math.Floor(y * scale)) == 0;
                            for (int i = 0; i < 4; i++)
                            {
                                if (isbg)
                                {
                                    bg[baseindex + i] = planarImage.Bits[baseindex + i];

                                }
                                else
                                {
                                    planarImage.Bits[baseindex + i] = bg[baseindex + i];

                                }
                            }
                            break;

                        default: break;
                    }


                }


            if (imageHelper == null)
            {
                imageHelper = new InteropBitmapHelper(planarImage.Width, planarImage.Height, planarImage.Bits);
                image1.Source = imageHelper.InteropBitmap;
            }
            else
            {
                imageHelper.UpdateBits(planarImage.Bits);
            }
         }

       

        private void bUp_Click(object sender, RoutedEventArgs e)
        {
            CurrentRuntime.NuiCamera.ElevationAngle += 5;
        }

        private void bDown_Click(object sender, RoutedEventArgs e)
        {
            CurrentRuntime.NuiCamera.ElevationAngle -= 5;
        }

        private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        private void cbEffect_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            switch (((ComboBoxItem)cbEffect.SelectedItem).Content.ToString())
            {
                case "Depth":
                    slider1.IsEnabled = true;
                    slider2.IsEnabled = true;
                    break;

                
                default: 
                    
                    slider1.IsEnabled = false;
                    slider2.IsEnabled = false;
                    break;
            }
        }


       
       
       
    }
}

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)

Share

About the Author

Jarek Kruza
Flextronics
Poland Poland
Programming since 10 years old with first commercial app sold in age of 16.

In past got Bachelor's degree in Computer Sciences and worked as Linux administrator and software developer.

Currently slightly over 30 and working as IT Project Manager for Flextronics.

Still coding for fun and/or money.

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160811.3 | Last Updated 6 Jan 2012
Article Copyright 2011 by Jarek Kruza
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid