- Joined
- Jun 27, 2006
- Messages
- 23,048
- Thread Author
- #1
This video covers the basics of using depth data from Kinect. You may find it easier to follow along by downloading the Link Removed.
[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:
XAML
Link Removed
[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.
C#
//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:
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 Link Removed
Link Removed
- [Link Removed] Depth data overview
- [Link Removed] Initializing the Kinect Runtime
- [Link Removed] Using the depth data to create an image
- [Link Removed] Using the PlayerIndex
[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:
XAML
Link Removed
[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.
C#
//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
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 Link Removed
Link Removed