本文共 1563 字,大约阅读时间需要 5 分钟。
深度数据是利用红外发射摄像头发射红外线,用红外接收摄像头接收红外线而形成的图像,在利用发射接收差来计算深度距离,从抓取的图像中我们可以看出,红外图像是有远近梯度的。
新建一个WPF项目,在窗体中添加一个Image控件,Name为img。
后台代码如下:
-
- KinectSensor kinectsensor = null;
- private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
- {
- if (kinectsensor.Status == KinectStatus.Connected)
- {
- kinectsensor.Stop();
- }
- }
- 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();
- 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;
- }
- }
- }
效果如下图:
转载地址:http://zwhzx.baihongyu.com/