Windows 7 Working with Depth Data


Extraordinary Robot
News Feed
This video covers the basics of using depth data from Kinect. You may find it easier to follow along by downloading the Kinect for Windows SDK Quickstarts samples and slides.
  • [00:43] Depth data overview
  • [04:56] Initializing the Kinect Runtime
  • [05:46] Using the depth data to create an image
  • [12:13] Using the PlayerIndex
[h=3]Setup[/h]The steps below assume you have setup your development environment as explained in the "Setting Up Your Development Environment" video.
[h=1]Task Setup the Depth Camera event[/h][h=3]Designing the UI[/h]Add an image with Width=320 and Height=240 as shown below:


[h=3]Setup the Depth Camera Event[/h]Create an instance of the Kinect Runtime outside of the Window_Loaded event. Then, initialize the Runtime to use DepthAndPlayerIndex and UseSkeletalTracking. Finally register for DepthFrameReady event and open the Depth stream as shown below.


//Kinect RuntimeRuntime nui = new Runtime(); private void Window_Loaded(object sender, RoutedEventArgs e){ //UseDepthAndPlayerIndex and UseSkeletalTracking nui.Initialize(RuntimeOptions.UseDepthAndPlayerIndex | RuntimeOptions.UseSkeletalTracking); //register for event nui.DepthFrameReady += new EventHandler(nui_DepthFrameReady); //DepthAndPlayerIndex ImageType nui.DepthStream.Open(ImageStreamType.Depth, 2, ImageResolution.Resolution320x240, ImageType.DepthAndPlayerIndex); }void nui_DepthFrameReady(object sender, ImageFrameReadyEventArgs e){}
Visual Basic

Private nui As New RuntimePrivate Sub Window_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs) 'UseDepthAndPlayerIndex and UseSkeletalTracking nui.Initialize(RuntimeOptions.UseDepthAndPlayerIndex Or RuntimeOptions.UseSkeletalTracking) 'register for event AddHandler nui.DepthFrameReady, AddressOf nui_DepthFrameReady 'DepthAndPlayerIndex ImageType nui.DepthStream.Open(ImageStreamType.Depth, 2, ImageResolution.Resolution320x240, ImageType.DepthAndPlayerIndex)End SubPrivate Sub nui_DepthFrameReady(ByVal sender As Object, ByVal e As ImageFrameReadyEventArgs)End Sub[h=3]Understanding the PlanarImage byte[] Array[/h]The DepthFrameReady event returns an ImageFrame class that contains a PlanarImage. That PlanarImage contains a byte[] array which contains the distance of each pixel. The array:
  • Starts at top left of image
  • Moves left to right, then top to bottom
  • Represents distance for pixel
[h=3]Calculating Distance[/h]Because there are 2 bytes per pixel (16 bits) representing the distance, you will need to use bitshift operators to get the distance for a particular pixel. The exact bitshifting depends on the ImageType you use
Depth Image Type
Bitshift second byte left by 8
Distance (0,0) = (int)(Bits[0] | Bits[1] > 3 | Bits[1] > 3 | secondFrame 2000 Then 'we are the farthest colorFrame(index + BlueIndex) = 0 colorFrame(index + GreenIndex) = 0 colorFrame(index + RedIndex) = 255 End If '//Color a player If GetPlayerIndex(depthData(depthIndex)) > 0 Then 'we are the farthest colorFrame(index + BlueIndex) = 0 colorFrame(index + GreenIndex) = 255 colorFrame(index + RedIndex) = 255 End If 'jump two bytes at a time depthIndex += 2 Next x Next y Return colorFrameEnd Function


This website is not affiliated, owned, or endorsed by Microsoft Corporation. It is a member of the Microsoft Partner Program.