您的位置:首页 > 编程语言 > C#

C# kinect v2学习笔记(三) 彩色图像

2016-03-20 09:12 405 查看
        彩色图像和红外、深度图像的处理基本类似,特别是深度图像,代码几乎看不出差别,当然使用的情况不一样啊,有兴趣的可以先去研究官方SDK中的coordinatemapping这个抠图的示例,运用体感器来实现抠图效果,感觉还是蛮良好的!

        作为一个程序员,良好的命名规范还是很重要滴,不过我英文真心不怎么好,也只有那样了,同样,彩色图像也是:1.获取体感设备;2.彩色帧初始化;3.帧描述;4.彩色帧触发事件;5. 创立一个位图,放入位图需要的东西,即将获取的彩色图像放在位图中;6.最后显示出来就好了!

        下面直接看代码:(效果就不展示了,就相当于1920*1080的高清摄像头)

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 彩色图像
{
public partial class MainWindow : Window
{
//体感器设备
private KinectSensor kinectDevice = null;

//彩色帧读取变量
private ColorFrameReader colorFrameReader = null;

//彩色帧描述
private FrameDescription colorFrameDescription = null;

//位图对象
private WriteableBitmap colorBitmap = null;

//存放一帧彩色图像的矩形框
private Int32Rect colorBitmapRect;

//步长,一维彩色像素数组转化为矩形框的步长。
private int colorBitmapStride;

//存放一帧彩色图像像素
private Byte[] colorPixelData;

public MainWindow()
{
InitializeComponent();

//获取默认的连接的体感器
this.kinectDevice = KinectSensor.GetDefault();

//彩色帧读取变量初始化
this.colorFrameReader = this.kinectDevice.ColorFrameSource.OpenReader();

//彩色帧描述,宽度和高度,注意括号内的入参,代表这一帧彩色图像像素的格式
this.colorFrameDescription = this.kinectDevice.ColorFrameSource.CreateFrameDescription(ColorImageFormat.Bgra);

//触发彩色帧事件
this.colorFrameReader.FrameArrived += _ColorFrameReader_FrameArrived;

//存放彩色图像的字节数组的长度=帧宽度*帧高度*每个像素占用的字节数4
this.colorPixelData = new Byte[this.colorFrameDescription.LengthInPixels*4];

//位图初始化,宽度,高度,96.0表示分辨率,像素格式
this.colorBitmap = new WriteableBitmap(this.colorFrameDescription.Width,
this.colorFrameDescription.Height, 96.0, 96.0, PixelFormats.Bgra32, null);

//存放图像像素的矩形框,起点为(0,0),宽度和高度
this.colorBitmapRect = new Int32Rect(0, 0, this.colorBitmap.PixelWidth, this.colorBitmap.PixelHeight);

//一个像素占用4个自己,分别是B、G、R、A的格式
this.colorBitmapStride = this.colorFrameDescription.Width * 4;

colorImage.Source = this.colorBitmap;

this.kinectDevice.Open();

}

//彩色帧处理事件
private void _ColorFrameReader_FrameArrived(object sender, ColorFrameArrivedEventArgs e)
{
//获取一帧彩色图像
using (ColorFrame colorFrame = e.FrameReference.AcquireFrame())
{
//如果获取成功
if (colorFrame != null)
{
//将彩色帧数据存放在字节数组里面,注意格式
colorFrame.CopyConvertedFrameDataToArray(this.colorPixelData, ColorImageFormat.Bgra); //把一帧彩色图像数据拷贝到字节数组中

//将像素写入位图,需要什么我们就补充什么
this.colorBitmap.WritePixels(this.colorBitmapRect, this.colorPixelData, this.colorBitmapStride, 0);
}
}
}

private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
//将打开的手动释放,其实你可以试试,不用写也能释放,不过良好的习惯是必备的素质
if (this.colorFrameReader != null)
{
this.colorFrameReader.Dispose();
this.colorFrameReader = null;
}
if (this.kinectDevice != null)
{
this.kinectDevice.Close();
this.kinectDevice = null;
}
}

}
}
其实蛮详尽的,需要源代码的我稍后补上!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息