@@ -21,7 +21,8 @@ namespace Kinect2Sample
21
21
public enum DisplayFrameType
22
22
{
23
23
Infrared ,
24
- Color
24
+ Color ,
25
+ Depth
25
26
}
26
27
27
28
public sealed partial class MainPage : Page , INotifyPropertyChanged
@@ -80,6 +81,10 @@ public sealed partial class MainPage : Page, INotifyPropertyChanged
80
81
private ushort [ ] infraredFrameData = null ;
81
82
private byte [ ] infraredPixels = null ;
82
83
84
+ //Depth Frame
85
+ private ushort [ ] depthFrameData = null ;
86
+ private byte [ ] depthPixels = null ;
87
+
83
88
public event PropertyChangedEventHandler PropertyChanged ;
84
89
public string StatusText
85
90
{
@@ -120,7 +125,7 @@ public MainPage()
120
125
121
126
SetupCurrentDisplay ( DEFAULT_DISPLAYFRAMETYPE ) ;
122
127
123
- this . multiSourceFrameReader = this . kinectSensor . OpenMultiSourceFrameReader ( FrameSourceTypes . Infrared | FrameSourceTypes . Color ) ;
128
+ this . multiSourceFrameReader = this . kinectSensor . OpenMultiSourceFrameReader ( FrameSourceTypes . Infrared | FrameSourceTypes . Color | FrameSourceTypes . Depth ) ;
124
129
125
130
this . multiSourceFrameReader . MultiSourceFrameArrived += this . Reader_MultiSourceFrameArrived ;
126
131
@@ -157,6 +162,15 @@ private void SetupCurrentDisplay(DisplayFrameType newDisplayFrameType)
157
162
this . bitmap = new WriteableBitmap ( colorFrameDescription . Width , colorFrameDescription . Height ) ;
158
163
break ;
159
164
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
+
160
174
default :
161
175
break ;
162
176
}
@@ -191,11 +205,76 @@ private void Reader_MultiSourceFrameArrived(MultiSourceFrameReader sender, Multi
191
205
ShowColorFrame ( colorFrame ) ;
192
206
}
193
207
break ;
208
+ case DisplayFrameType . Depth :
209
+ using ( DepthFrame depthFrame =
210
+ multiSourceFrame . DepthFrameReference . AcquireFrame ( ) )
211
+ {
212
+ ShowDepthFrame ( depthFrame ) ;
213
+ }
214
+ break ;
194
215
default :
195
216
break ;
196
217
}
197
218
}
198
219
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
+
199
278
private void ShowColorFrame ( ColorFrame colorFrame )
200
279
{
201
280
bool colorFrameProcessed = false ;
@@ -236,7 +315,7 @@ private void ShowInfraredFrame(InfraredFrame infraredFrame)
236
315
FrameDescription infraredFrameDescription = infraredFrame . FrameDescription ;
237
316
238
317
// 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 )
240
319
== this . infraredFrameData . Length ) &&
241
320
( infraredFrameDescription . Width == this . bitmap . PixelWidth ) &&
242
321
( infraredFrameDescription . Height == this . bitmap . PixelHeight ) )
@@ -251,8 +330,8 @@ private void ShowInfraredFrame(InfraredFrame infraredFrame)
251
330
// we got a frame, convert and render
252
331
if ( infraredFrameProcessed )
253
332
{
254
- ConvertInfraredDataToPixels ( ) ;
255
- RenderPixelArray ( this . infraredPixels ) ;
333
+ this . ConvertInfraredDataToPixels ( ) ;
334
+ this . RenderPixelArray ( this . infraredPixels ) ;
256
335
}
257
336
}
258
337
@@ -303,5 +382,10 @@ private void ColorButton_Click(object sender, RoutedEventArgs e)
303
382
SetupCurrentDisplay ( DisplayFrameType . Color ) ;
304
383
}
305
384
385
+ private void DepthButton_Click ( object sender , RoutedEventArgs e )
386
+ {
387
+ SetupCurrentDisplay ( DisplayFrameType . Depth ) ;
388
+ }
389
+
306
390
}
307
391
}
0 commit comments