深度數據是利用紅外發射攝像頭髮射紅外線,用紅外接收攝像頭接收紅外線而造成的圖像,在利用發射接收差來計算深度距離,從抓取的圖像中咱們能夠看出,紅外圖像是有遠近梯度的。數組
後臺代碼以下:ide
- //Kinect對象
- KinectSensor kinectsensor = null;
- private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
- {
- if (kinectsensor.Status == KinectStatus.Connected)
- {
- kinectsensor.Stop();//中止Kinect
- }
- }
- private void Window_Loaded(object sender, RoutedEventArgs e)
- {
- foreach (KinectSensor ks in KinectSensor.KinectSensors)
- {
- if (ks.Status == KinectStatus.Connected)
- {
- kinectsensor = ks;
- //設置深度數據流採集參數
- kinectsensor.DepthStream.Enable(DepthImageFormat.Resolution640x480Fps30);
- //訂閱深度數據流採集事件
- kinectsensor.DepthFrameReady += kinectsensor_DepthFrameReady;
- kinectsensor.Start();//啓動Kinect
- this.Title = "Kinect開始工做……";
- return;
- }
- }
- }
- private DepthImagePixel[] depthPixels;//定義字節數組
- private WriteableBitmap DepthBitmap;//定義位圖
- void kinectsensor_DepthFrameReady(object sender, DepthImageFrameReadyEventArgs e)
- {
- using (DepthImageFrame depframe = e.OpenDepthImageFrame())
- {
- if (depframe != null)
- {
- //初始化字節數組
- depthPixels = new DepthImagePixel[kinectsensor.DepthStream.FramePixelDataLength];
- //把深度數據複製到數組中
- depframe.CopyDepthImagePixelDataTo(depthPixels);
- //實例化位圖對象
- this.DepthBitmap = new WriteableBitmap(kinectsensor.DepthStream.FrameWidth, kinectsensor.DepthStream.FrameHeight, 96.0, 96.0, PixelFormats.Bgr32, null);
- //用字節數組數據繪位圖
- this.DepthBitmap.WritePixels(new Int32Rect(0, 0, DepthBitmap.PixelWidth, DepthBitmap.PixelHeight), depthPixels, DepthBitmap.PixelWidth * sizeof(int), 0);
- //顯示位圖到圖片控件上
- img.Source = DepthBitmap;
- }
- }
- }
效果以下圖:this