深度数据是利用红外发射摄像头发射红外线,用红外接收摄像头接收红外线而形成的图像,在利用发射接收差来计算深度距离,从抓取的图像中我们可以看出,红外图像是有远近梯度的。
新建一个WPF项目,在窗体中添加一个Image控件,Name为img。
后台代码如下:
- //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;
- }
- }
- }
效果如下图:
本文转自桂素伟51CTO博客,原文链接: http://blog.51cto.com/axzxs/1184502,如需转载请自行联系原作者