Kinect开发环境 📅 2026/7/4 22:12:13 今天我们主要是操作RGB Camera和Depth Sensor首先我们要完成Kinect开发环境的配置第一步创建WPF工程打开Visual Studio 2010创建一个WPF工程名叫KinectWpfDemo当然由于Kinect SDK中包含基于.NET的程序集除了WPF外我们使用.NET WinForm或XNA框架都可以目前还没有人在Silverlight平台上实验成功。第二步添加Kinect程序集的引用在Solution Explorer中右键单击KinectWpfDemo在右键菜单中选择“Add Reference…”。在弹出的对话框中我们在.NET标签页里选择“Microsoft.Research.Kinect”程序集。如下图所示第三步添加Coding4Fun Kinect Toolkit这是一个可选项不过为了之后的编程方便建议大家添加一个。Coding4Fun Kinect Toolkit的下载地址http://c4fkinect.codeplex.com/解压缩后一共有五个文件针对WinForm、WPF平台还有一个Microsoft.Expression.Drawing.dll。我们通过Add Reference将Coding4Fun.Kinect.Wpf.dll添加进来。获取RGB Camera数据第四步添加控件双击打开MainWindow.xaml在设计器中添加两个Image控件一个用于显示RGB图像另一个用于显示Depth信息。第五步引用命名空间打开MainWindow.xaml.cs文件在文件头部添加对于Kinect对象的引用using Microsoft.Research.Kinect.Nui; using Microsoft.Research.Kinect.Audio; using Coding4Fun.Kinect.Wpf;回到MainWindow.xaml的设计器中在属性窗口中选择Event找到Loaded和Closed两个方法分别双击添加两个事件的处理函数在MainWindow.xaml.cs文件的MainWindow类中声明Runtime的变量Runtime nui;然后在Loaded事件的处理函数中添加Runtime初始化的代码private void Window_Loaded(object sender, RoutedEventArgs e) { nui new Runtime(); nui.Initialize(RuntimeOptions.UseColor| RuntimeOptions.UseDepth | RuntimeOptions.UseDepthAndPlayerIndex | RuntimeOptions.UseSkeletalTracking); }接下来是Closed事件中关闭Runtime的代码private void Window_Closed(object sender, EventArgs e) { nui.Uninitialize(); }Runtime对象是Kinect SDK中最主要的一个类所有针对Kinect的操作都由Runtime类进行了封装。Runtime的构造函数没有接受任何参数但有一个显式的初始化函数Initialize接受RuntimeOptions参数指定调用Kinect的哪些功能。其中RuntimeOptions.UseColor表示使用RGB Camera而RuntimeOptions.UseDepth则表示使用Depth传感器。初始化工作完成之后我们要通过RGB Camera来获取实时的图像数据了。我们首先要声明一个事件处理方法来接收视频数据的信息nui.VideoFrameReady new EventHandlerImageFrameReadyEventArgs(nui_VideoFrameReady);然后是事件处理函数void nui_VideoFrameReady(object sender, ImageFrameReadyEventArgs e) { PlanarImage imageData e.ImageFrame.Image; image1.Source BitmapSource.Create(imageData.Width, imageData.Height, 96, 96, PixelFormats.Bgr32, null, imageData.Bits, imageData.Width * imageData.BytesPerPixel); //image1.Source e.ImageFrame.ToBitmapSource(); }提示Getting Started上提供的Sample Code有误需要将最后一个参数中的data.Width改为imageData.Width才可以正常运行。VideoFrameReady事件会传递一个ImageFrameReadyEventArgs参数给事件处理函数其中的ImageFrame会包含关于图片的各种信息比如Type变量指定了图像是来自RGB还是DepthResolution变量指定了分辨率而Image中以byte[]数组的方式保存了图像的真实数据。然后的工作就是根据PlanarImage中包括的数据来创建一个Bitmap对象然后将其传递给Image控件显示到WPF程序的界面上。最后我们还要在构造函数里打开视频流来获取视频数据nui.VideoStream.Open(ImageStreamType.Video, 2, ImageResolution.Resolution640x480, ImageType.Color);第一个参数是ImageStreamType用来指定打开的设备流类型第二个参数是PoolSize指定缓冲区的数量至少为2保证一个Buffer进行绘制另一个Buffer进行数据填充第三个参数指定Camera的分辨率第四个参数则是获取的图片类型。显示效果如下图所示上面的示例代码没有使用Coding4Fun的Helper类如果使用的话则代码如下void nui_VideoFrameReady(object sender, ImageFrameReadyEventArgs e) { image1.Source e.ImageFrame.ToBitmapSource(); e.ImageFrame.ToBitmapSource().Save(catpure.jpg, ImageFormat.Jpeg); }Helper类使用了C#的Extension Methods为ImageFrame增加了一些转换方法。我们还可以将图像保存为文件考虑到文件系统存储的效率文件建议大家不用每张都存。获取Depth信息接下来我们要获取Depth信息了过程与RGB Camera类似。首先要确保Runtime对象被初始化时已经添加了RuntimeOptions.UseDepth的属性否则设备无法正常打开。然后添加获取Depth数据的事件处理并打开Depth的数据流这次的分辨率是320x240nui.DepthFrameReady new EventHandlerImageFrameReadyEventArgs(nui_DepthFrameReady); nui.DepthStream.Open(ImageStreamType.Depth, 2, ImageResolution.Resolution320x240, ImageType.Depth);下面是事件处理函数在另外一个Image函数里显示Depth图像void nui_DepthFrameReady(object sender, ImageFrameReadyEventArgs e) { image2.Source e.ImageFrame.ToBitmapSource(); }偷懒所以使用了Coding4Fun的Helper类。程序运行的效果如下