kinect_深度图像的测试
这几天试了下Kinect的深度图像的例子,测量图像像素的深度数据,Kinect处理的深度数据距离大概是0到8000mm,通过depthframe视频流,来获取深度数据。深度距离就是从摄像头到图像的各个像素点的距离。因为实验时候,像素格式为Gray16视觉效果不是很好,故采用了BGRA32的格式。
(1)实验目的:
1.如何调用depthframe视频流来处理像素。
2.什么叫做图像像素的深度。
3.对深度数据进行着色处理。
(2)实验准备
1.图像像素格式
像素格式描述了像素数据存储所用的格式。
Byte formats:
在这种格式下每个通道对应一个byte,通道在内存里的组织方式和格式名称定义的相同。例如PF_BYTE_RGBA格式的像素包含了四个byte,一个对应红色,一个绿色,一个蓝色,以及一个alpha通道。
其他像素格式我也就不意义细说了,只是数据类型变了。
2.Kinect中的深度图像像素。
ushort型深度数据的高13位代表像素与摄像头的距离,后三个代表玩家索引。故能测量的范围为0-2的13次方mm。
3.下面对这距离不同的区域进行着色处理。具体代码如下:
namespace Kinect人机交互深度图像的获取
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
private KinectSensor depth_kinect = null;//体感仪设备
private DepthFrameReader kinect_depthreader = null;//深度图像帧的读取器;
private FrameDescription kinect_description = null;//帧的描述,所有帧的描述都用这个Framedescription
private WriteableBitmap kinect_bitmap = null;//位图
private ushort[] kinect_depthdata;//储存深度图像的数组,深度图像的每个像素都用16位来表示。
private byte[] kinect_depthbrgdata;//转换成bgr格式.
public MainWindow()
{
InitializeComponent();
this.depth_kinect = KinectSensor.GetDefault();//取得默认的体感仪设备。
this.kinect_depthreader = this.depth_kinect.DepthFrameSource.OpenReader();//初始化深度图像的变量
this.kinect_description = this.depth_kinect.DepthFrameSource.FrameDescription;//获取深度图像的高度宽度.
this.kinect_depthbrgdata = new byte[this.kinect_description.LengthInPixels*4];//转换为bgra格式。
this.kinect_depthdata = new ushort[this.kinect_description.LengthInPixels];
this.kinect_bitmap = new WriteableBitmap(this.kinect_description.Width,this.kinect_description.Height,
96.0,96.0,PixelFormats.Bgra32,null);//位图的没初始化
depthimage.Source = this.kinect_bitmap;//将位图放在图像的控件上
this.kinect_depthreader.FrameArrived += kinect_depthreader_arriveed;//注册事件
this.depth_kinect.Open();
private void kinect_depthreader_arriveed(object sender, DepthFrameArrivedEventArgs e)//事件处理函数
{
using (DepthFrame msf = e.FrameReference.AcquireFrame()) {//捕获一帧图像
if (msf != null) {
msf.CopyFrameDataToArray(this.kinect_depthdata);//拷贝数据到ushort数组
for (int i = 0; i < this.kinect_depthdata.Length; i++) {
ushort realdepth = this.kinect_depthdata[i];
if (realdepth < 500)//判断距离赋予颜色
{
this.kinect_depthbrgdata[i * 4] = 0;
this.kinect_depthbrgdata[i * 4 + 1] = 0;
this.kinect_depthbrgdata[i * 4 + 2] = 0;
this.kinect_depthbrgdata[i * 4 + 3] = 255;
}
else if (realdepth >= 500 && realdepth < 1500)//接下来是类似的颜色复制,我就不啰嗦了
{
this.kinect_depthbrgdata[i * 4] = 0;
this.kinect_depthbrgdata[i * 4 + 1] = 255;
this.kinect_depthbrgdata[i * 4 + 2] = 255;
this.kinect_depthbrgdata[i * 4 + 3] = 255;
}
else if(realdepth>=1500&&realdepth<2500){
this.kinect_depthbrgdata[i * 4] = 160;
this.kinect_depthbrgdata[i * 4 + 1] = 32;
this.kinect_depthbrgdata[i * 4 + 2] = 240;
this.kinect_depthbrgdata[i * 4 + 3] = 255;
}
else if (realdepth >= 2500 && realdepth < 3500) {
this.kinect_depthbrgdata[i * 4] = 255;
this.kinect_depthbrgdata[i * 4 + 1] = 255;
this.kinect_depthbrgdata[i * 4 + 2] = 0;
this.kinect_depthbrgdata[i * 4 + 3] = 255;
}
else if (realdepth >= 3500&&realdepth<4500) {
this.kinect_depthbrgdata[i * 4] = 192;
this.kinect_depthbrgdata[i * 4 + 1] = 192;
this.kinect_depthbrgdata[i * 4 + 2] = 192;
this.kinect_depthbrgdata[i * 4 + 3] = 255;
}
else if (realdepth >= 4500 && realdepth < 5500)
{
this.kinect_depthbrgdata[i * 4] = 192;
this.kinect_depthbrgdata[i * 4 + 1] = 0;
this.kinect_depthbrgdata[i * 4 + 2] = 192;
this.kinect_depthbrgdata[i * 4 + 3] = 255;
}
else if (realdepth >= 5500 && realdepth < 6500)
{
this.kinect_depthbrgdata[i * 4] = 192;
this.kinect_depthbrgdata[i * 4 + 1] = 40;
this.kinect_depthbrgdata[i * 4 + 2] = 192;
this.kinect_depthbrgdata[i * 4 + 3] = 255;
}
else if (realdepth >= 6500 && realdepth < 7500)
{
this.kinect_depthbrgdata[i * 4] = 192;
this.kinect_depthbrgdata[i * 4 + 1] = 192;
this.kinect_depthbrgdata[i * 4 + 2] = 40;
this.kinect_depthbrgdata[i * 4 + 3] = 255;
}
}
this.kinect_bitmap.WritePixels(new Int32Rect(0,0,512,350),this.kinect_depthbrgdata,512*4,0); //把更新后的数据更新到位图,即可
}
}
}
private void Window_Closed(object sender, EventArgs e)//关闭Kinect
{
if (this.depth_kinect != null) {
this.kinect_depthreader.Dispose();
this.kinect_depthreader = null;
}
if (this.depth_kinect != null) {
this.depth_kinect.Close();
this.depth_kinect = null;
}
}
}
}
运行时的截图如下:到摄像头不同距离的深度点的颜色不同。