[KinectWPF程序]单帧彩色图像获取&使用WriteableBitmap对象
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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;
//添加两个Image控件—ShotImage和ShowImage,一个Button控件
namespace WpfApplication1
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
KinectSensor kinectSensor;
private WriteableBitmap colorImageBitMap;
private Int32Rect colorImageBitmapRect;
private int colorImageStride;
byte[] ColorPixelData;
ColorImageFrame colorFrame;
//初始化Kinect直接调用InitKinect()即可;注册的同步事件为AllFramesReadyEventArgs
/// <summary>
/// 启动Kinect设备,初始化选项,并注册AllFrameReady同步事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void InitKinect()
{
if (KinectSensor.KinectSensors.Count > 0)
{
kinectSensor = (from sensor in KinectSensor.KinectSensors
where sensor.Status == KinectStatus.Connected
select sensor).FirstOrDefault();
kinectSensor.ColorStream.Enable(ColorImageFormat.RgbResolution640x480Fps30);
kinectSensor.DepthStream.Enable(DepthImageFormat.Resolution640x480Fps30);
kinectSensor.SkeletonStream.Enable();
//创建WriteableBitmap对象,准备接收像素数据
ColorImageStream colorStream = kinectSensor.ColorStream;
this.colorImageBitMap = new WriteableBitmap(colorStream.FrameWidth, colorStream.FrameHeight,
96, 96, PixelFormats.Bgr32, null);
this.colorImageBitmapRect = new Int32Rect(0, 0, colorStream.FrameWidth, colorStream.FrameHeight);
this.colorImageStride = colorStream.FrameWidth * colorStream.FrameBytesPerPixel;
ShowImage.Source = this.colorImageBitMap;
kinectSensor.Start();
kinectSensor.AllFramesReady += new EventHandler<AllFramesReadyEventArgs>(kinectSensor_AllFramesReady);
}
else
{
MessageBox.Show("没有检测到Kinect设备");
}
}
public MainWindow()
{
InitializeComponent();
}
void kinectSensor_AllFramesReady(object sender, AllFramesReadyEventArgs e)
{
using (colorFrame = e.OpenColorImageFrame())
{
if (colorFrame != null)
{
//建立存储像素的矩阵,一维矩阵
//colorFrame.PixelDataLength每一帧的数据长度
ColorPixelData = new byte[colorFrame.PixelDataLength];
//将Kicent获取的彩色像素拷贝存储像素的矩阵中
colorFrame.CopyPixelDataTo(ColorPixelData);
//关闭每个像素点的蓝色和绿色通道。
//for循环遍历每个像素,使得i的起始位置重视该像素的第一个字节。
//由于数据的格式是Bgr32,即RGB32位(一个像素共占4个字节,每个字节8位),
//所以第一个字节是蓝色通道,第二个是绿色,第三个是红色。
//循环体内,将第一个和第二个通道设置为0.所以输出的代码中只用红色通道的信息。这是最基本的图像处理。
for (int i = 0; i < ColorPixelData.Length; i += colorFrame.BytesPerPixel)
{
ColorPixelData[i] = 0x25;//蓝色
ColorPixelData[i + 1] = 0x00;//绿色
}
//调用WriteableBitmap对象的WritePixels方法来更新图像
this.colorImageBitMap.WritePixels(this.colorImageBitmapRect, ColorPixelData, this.colorImageStride, 0);
}
}
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
this.ShotImage.Source = BitmapSource.Create(colorFrame.Width, colorFrame.Height, 96, 96,
PixelFormats.Bgr32, null, ColorPixelData, colorFrame.Width * colorFrame.BytesPerPixel);
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
InitKinect();
}
private void Window_Closed(object sender, EventArgs e)
{
kinectSensor.Stop();
}
}
}