Kinect之彩色图像数据
彩色图像很有用,很有用!!说到图像识别,未来肯定是个大方向!在机器人视觉和一些智能识别在应用很广,而获取下来的数据再加上Opencv就能做出很多很好玩很有趣的功能。这个以后等我进一步成长后再回来慢慢记录。
这里首先要遵循下我在博客里第一篇文章kinect基本认识(http://www.cnblogs.com/carsonche/p/5891517.html)上讲的流程进行源码分析
流程:开始程序-获取kinect摄像机-打开读取器-打开Kinect-获取读取器的相关帧数据-使用帧数据-关闭帧-关闭读取器-关闭Kinect-关闭程序
官方的SDK把获取彩色帧的数据放在了Color source manager,而显示帧的数据放在了Color Source View里。
Color source manager的操作流程:(在源码上做了注释和解析)
u
sing UnityEngine; using System.Collections; using Windows.Kinect; public class ColorSourceManager : MonoBehaviour { public int ColorWidth { get; private set; } public int ColorHeight { get; private set; } public uint BytesPerPixel; public uint LengthInPixels; private KinectSensor _Sensor; private ColorFrameReader _Reader; private Texture2D _Texture; private byte[] _Data; //获取图像 public Texture2D GetColorTexture() { return _Texture; } void Start() { //获取传感器 _Sensor = KinectSensor.GetDefault(); if (_Sensor != null) { //获取颜色帧读取器 _Reader = _Sensor.ColorFrameSource.OpenReader(); //获取RGBA的彩色帧的分辨率为1920*1080,30帧,每像素 var frameDesc = _Sensor.ColorFrameSource.CreateFrameDescription(ColorImageFormat.Rgba); ColorWidth = frameDesc.Width; ColorHeight = frameDesc.Height; _Texture = new Texture2D(frameDesc.Width, frameDesc.Height, TextureFormat.RGBA32, false); //定义字节数据大小,大小为没像素字节*像素长度(1920*1080) _Data = new byte[frameDesc.BytesPerPixel * frameDesc.LengthInPixels]; if (!_Sensor.IsOpen) { //若Kinect没有开启,则开启kinect _Sensor.Open(); } } } void Update () { if (_Reader != null) { //获取最新的帧 var frame = _Reader.AcquireLatestFrame(); //或存在帧 if (frame != null) { //把帧按照RGBA的个数保存在DATA里 frame.CopyConvertedFrameDataToArray(_Data, ColorImageFormat.Rgba); //把图像按行写入数据 _Texture.LoadRawTextureData(_Data); //更新图像 _Texture.Apply(); //释放并关闭帧 frame.Dispose(); //帧为空 frame = null; } } } void OnApplicationQuit() { //程序关闭时关闭并释放读取器 if (_Reader != null) { _Reader.Dispose(); _Reader = null; } //程序关闭时关闭kinect if (_Sensor != null) { if (_Sensor.IsOpen) { _Sensor.Close(); } _Sensor = null; } } }
从上面可以看到,主要整个类为了一个函数,GetColorTexture()而存在的,主要的操作是先定义各种要的参数(字节数组,图片大小,帧读取器等),然后下面这三行就是核心程序了
从最新的帧里保存在data数组里,并转化赋值给texture。后续可以把图像的所有会用到的函数集合在这个类里,并且进行调用。
图像的显示在unity里很简单,只需要一行代码就行了,
gameObject.GetComponent<Renderer>().material.mainTexture = _ColorManager.GetColorTexture();
将之前获得的图像转给material,这样我们就能获得一个很基本的实时的color图像了。
如果您觉得本文对你有用,不妨帮忙点个赞,或者在评论里给我一句赞美,小小成就都是今后继续为大家编写优质文章的动力,carson拜谢! 欢迎您持续关注我的博客:)
作者:carsonche
出处:https://home.cnblogs.com/u/carsonche/
版权所有,欢迎保留原文链接进行转载:)