@@ -21,7 +21,8 @@ namespace Kinect2Sample
2121 public enum DisplayFrameType
2222 {
2323 Infrared ,
24- Color
24+ Color ,
25+ Depth
2526 }
2627
2728 public sealed partial class MainPage : Page , INotifyPropertyChanged
@@ -80,6 +81,10 @@ public sealed partial class MainPage : Page, INotifyPropertyChanged
8081 private ushort [ ] infraredFrameData = null ;
8182 private byte [ ] infraredPixels = null ;
8283
84+ //Depth Frame
85+ private ushort [ ] depthFrameData = null ;
86+ private byte [ ] depthPixels = null ;
87+
8388 public event PropertyChangedEventHandler PropertyChanged ;
8489 public string StatusText
8590 {
@@ -120,7 +125,7 @@ public MainPage()
120125
121126 SetupCurrentDisplay ( DEFAULT_DISPLAYFRAMETYPE ) ;
122127
123- this . multiSourceFrameReader = this . kinectSensor . OpenMultiSourceFrameReader ( FrameSourceTypes . Infrared | FrameSourceTypes . Color ) ;
128+ this . multiSourceFrameReader = this . kinectSensor . OpenMultiSourceFrameReader ( FrameSourceTypes . Infrared | FrameSourceTypes . Color | FrameSourceTypes . Depth ) ;
124129
125130 this . multiSourceFrameReader . MultiSourceFrameArrived += this . Reader_MultiSourceFrameArrived ;
126131
@@ -157,6 +162,15 @@ private void SetupCurrentDisplay(DisplayFrameType newDisplayFrameType)
157162 this . bitmap = new WriteableBitmap ( colorFrameDescription . Width , colorFrameDescription . Height ) ;
158163 break ;
159164
165+ case DisplayFrameType . Depth :
166+ FrameDescription depthFrameDescription = this . kinectSensor . DepthFrameSource . FrameDescription ;
167+ this . CurrentFrameDescription = depthFrameDescription ;
168+ // allocate space to put the pixels being received and converted
169+ this . depthFrameData = new ushort [ depthFrameDescription . Width * depthFrameDescription . Height ] ;
170+ this . depthPixels = new byte [ depthFrameDescription . Width * depthFrameDescription . Height * BytesPerPixel ] ;
171+ this . bitmap = new WriteableBitmap ( depthFrameDescription . Width , depthFrameDescription . Height ) ;
172+ break ;
173+
160174 default :
161175 break ;
162176 }
@@ -191,11 +205,76 @@ private void Reader_MultiSourceFrameArrived(MultiSourceFrameReader sender, Multi
191205 ShowColorFrame ( colorFrame ) ;
192206 }
193207 break ;
208+ case DisplayFrameType . Depth :
209+ using ( DepthFrame depthFrame =
210+ multiSourceFrame . DepthFrameReference . AcquireFrame ( ) )
211+ {
212+ ShowDepthFrame ( depthFrame ) ;
213+ }
214+ break ;
194215 default :
195216 break ;
196217 }
197218 }
198219
220+ private void ShowDepthFrame ( DepthFrame depthFrame )
221+ {
222+ bool depthFrameProcessed = false ;
223+ ushort minDepth = 0 ;
224+ ushort maxDepth = 0 ;
225+
226+ if ( depthFrame != null )
227+ {
228+ FrameDescription depthFrameDescription = depthFrame . FrameDescription ;
229+
230+ // verify data and write the new infrared frame data to the display bitmap
231+ if ( ( ( depthFrameDescription . Width * depthFrameDescription . Height )
232+ == this . infraredFrameData . Length ) &&
233+ ( depthFrameDescription . Width == this . bitmap . PixelWidth ) &&
234+ ( depthFrameDescription . Height == this . bitmap . PixelHeight ) )
235+ {
236+ // Copy the pixel data from the image to a temporary array
237+ depthFrame . CopyFrameDataToArray ( this . depthFrameData ) ;
238+
239+ minDepth = depthFrame . DepthMinReliableDistance ;
240+ maxDepth = depthFrame . DepthMaxReliableDistance ;
241+ //maxDepth = 8000;
242+
243+ depthFrameProcessed = true ;
244+ }
245+ }
246+
247+ // we got a frame, convert and render
248+ if ( depthFrameProcessed )
249+ {
250+ ConvertDepthDataToPixels ( minDepth , maxDepth ) ;
251+ RenderPixelArray ( this . depthPixels ) ;
252+ }
253+ }
254+
255+ private void ConvertDepthDataToPixels ( ushort minDepth , ushort maxDepth )
256+ {
257+ int colorPixelIndex = 0 ;
258+ // Shape the depth to the range of a byte
259+ int mapDepthToByte = maxDepth / 256 ;
260+
261+ for ( int i = 0 ; i < this . depthFrameData . Length ; ++ i )
262+ {
263+ // Get the depth for this pixel
264+ ushort depth = this . depthFrameData [ i ] ;
265+
266+ // To convert to a byte, we're mapping the depth value to the byte range.
267+ // Values outside the reliable depth range are mapped to 0 (black).
268+ byte intensity = ( byte ) ( depth >= minDepth &&
269+ depth <= maxDepth ? ( depth / mapDepthToByte ) : 0 ) ;
270+
271+ this . depthPixels [ colorPixelIndex ++ ] = intensity ; //Blue
272+ this . depthPixels [ colorPixelIndex ++ ] = intensity ; //Green
273+ this . depthPixels [ colorPixelIndex ++ ] = intensity ; //Red
274+ this . depthPixels [ colorPixelIndex ++ ] = 255 ; //Alpha
275+ }
276+ }
277+
199278 private void ShowColorFrame ( ColorFrame colorFrame )
200279 {
201280 bool colorFrameProcessed = false ;
@@ -236,7 +315,7 @@ private void ShowInfraredFrame(InfraredFrame infraredFrame)
236315 FrameDescription infraredFrameDescription = infraredFrame . FrameDescription ;
237316
238317 // verify data and write the new infrared frame data to the display bitmap
239- if ( ( ( infraredFrameDescription . Width * infraredFrameDescription . Height )
318+ if ( ( ( infraredFrameDescription . Width * infraredFrameDescription . Height )
240319 == this . infraredFrameData . Length ) &&
241320 ( infraredFrameDescription . Width == this . bitmap . PixelWidth ) &&
242321 ( infraredFrameDescription . Height == this . bitmap . PixelHeight ) )
@@ -251,8 +330,8 @@ private void ShowInfraredFrame(InfraredFrame infraredFrame)
251330 // we got a frame, convert and render
252331 if ( infraredFrameProcessed )
253332 {
254- ConvertInfraredDataToPixels ( ) ;
255- RenderPixelArray ( this . infraredPixels ) ;
333+ this . ConvertInfraredDataToPixels ( ) ;
334+ this . RenderPixelArray ( this . infraredPixels ) ;
256335 }
257336 }
258337
@@ -303,5 +382,10 @@ private void ColorButton_Click(object sender, RoutedEventArgs e)
303382 SetupCurrentDisplay ( DisplayFrameType . Color ) ;
304383 }
305384
385+ private void DepthButton_Click ( object sender , RoutedEventArgs e )
386+ {
387+ SetupCurrentDisplay ( DisplayFrameType . Depth ) ;
388+ }
389+
306390 }
307391}
0 commit comments