Kinect彩色和红外图像数据的处理
文章目录
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Kinect;
namespace KinectColorViewer
{
///
/// MainWindow.xaml 的交互逻辑
///
public partial class MainWindow : Window
{
KinectSensor kinectSensor;
private byte[] pixelData;
public MainWindow()
{
InitializeComponent();
}
//在Loaded事件的处理函数中添加KinectSensor对象的初始化代码
private void Grid_Loaded(object sender, RoutedEventArgs e)
{
kinectSensor = (from sensor in KinectSensor.KinectSensors
where sensor.Status == KinectStatus.Connected
select sensor).FirstOrDefault();
kinectSensor.ColorStream.Enable(ColorImageFormat.RgbResolution640x480Fps30);
kinectSensor.Start();//启动Kinect设备
kinectSensor.ColorFrameReady += kinectSensor_ColorFrameReady;
}
//获取视频数据,并将获取到的数据显示出来
void kinectSensor_ColorFrameReady(object sender, ColorImageFrameReadyEventArgs e)
{
using (ColorImageFrame imageFrame = e.OpenColorImageFrame())
{
if(imageFrame != null)
{
this.pixelData = new byte[imageFrame.PixelDataLength];
imageFrame.CopyPixelDataTo(this.pixelData);
this.ColorImage.Source = BitmapSource.Create(imageFrame.Width,
imageFrame.Height, 96, 96, PixelFormats.Bgr32, null, pixelData,
imageFrame.Width * imageFrame.BytesPerPixel);
}
}
}
private void Grid_Unloaded(object sender, RoutedEventArgs e)
{
kinectSensor.Stop();//关闭Kinect设备
}
}
}
ColorImageFormat 的枚举项:
// 摘要:
// Image formats for the Color Image Stream. It represents Image Data format,
// Framerate, and Resolution.
public enum ColorImageFormat
{
// 摘要:
// The image format is undefined.
Undefined = 0,
//
// 摘要:
// RGB data (32 bits per pixel, layout corresponding to PixelFormats.Bgr32).
// Resolution of 640 by 480 at 30 Frames per second.
RgbResolution640x480Fps30 = 1,
//
// 摘要:
// RGB data (32 bits per pixel, layout corresponding to PixelFormats.Bgr32).
// Resolution of 1280 by 960 at 12 Frames per second.
RgbResolution1280x960Fps12 = 2,
//
// 摘要:
// The data is collected from the Kinect as YUV, but it is translated to: RGB
// data (32 bits per pixel, layout corresponding to PixelFormats.Bgr32). Resolution
// of 640 by 480 at 15 Frames per second.
YuvResolution640x480Fps15 = 3,
//
// 摘要:
// YUV data (32 bits per pixel, layout corresponding to D3DFMT_LIN_UYVY). Resolution
// of 640 by 480 at 15 Frames per second.
RawYuvResolution640x480Fps15 = 4,
//
// 摘要:
// Infrared data (16 bits per pxel) Resolution of 640 by 480 at 30 Frames per
// second.
InfraredResolution640x480Fps30 = 5,
//
// 摘要:
// Bayer data (8 bits per pixel, layout in alternating pixels of red, green
// and blue). Resolution of 640 by 480 at 30 Frames per second.
RawBayerResolution640x480Fps30 = 6,
//
// 摘要:
// Bayer data (8 bits per pixel, layout in alternating pixels of red, green
// and blue). Resolution of 1280 by 960 at 12 Frames per second.
RawBayerResolution1280x960Fps12 = 7,
}
我只想说:操蛋!上次安装的Kinect For Window SDK 的运行环境出错。。。害得我以为自己的程序出错纠结了两天!!!最后卸载重新下载安装完果然没错。接下来就简单了。。。
评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论