C# kinect v2学习笔记(三)红外图像展示

       今天休假,晚上发现没更新记录呢。

      从今天开始,就用WPF来写了,我从学习C#开始,就几乎没写几个控制台程序,感觉跳级学习还是蛮有压力的,而且从一开始就是从kinectV2学起,这简直坑爹啊,在要做体感开发之前,我压根不知道有这种东西好么? 好吧,我表示我又无知了,最恨的是我英语还很差啊!给同样入门的小白看看吧!

     直接上代码,代码中文注释,一看就懂,没有压力,配合官方的API,比较好学!

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 红外图像
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        //体感器设备
        private KinectSensor KinectDevice = null;

        //红外帧读取变量
        private InfraredFrameReader infraredFrameReader = null;

        //红外帧描述
        private FrameDescription infraredFrameDescription = null;

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

        //存放一帧红外图像像素
        private ushort[] infraredData;

        //一帧红外图像转换为bgra格式的字节数组
        private Byte[] infraredDataConverted;

        public MainWindow()
        {
            InitializeComponent();

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

            //红外帧读取变量初始化
            this.infraredFrameReader = this.KinectDevice.InfraredFrameSource.OpenReader();

            //红外帧描述,宽度和高度
            this.infraredFrameDescription = this.KinectDevice.InfraredFrameSource.FrameDescription;

            //触发红外帧事件
            this.infraredFrameReader.FrameArrived += InfraredFrameReader_FrameArrived;

            //存放红外图像的像素,无符号短整型=2字节=16位
            this.infraredData = new ushort[this.infraredFrameDescription.LengthInPixels];

            //存放红外图像的字节数组的长度=像素个数*每个像素占用的字节数4
            this.infraredDataConverted = new byte[this.infraredFrameDescription.LengthInPixels * 4];

            this.infraredBitmap = new WriteableBitmap(this.infraredFrameDescription.Width,
                                                this.infraredFrameDescription.Height,96.0,96.0,PixelFormats.Bgra32,null);

            //将位图赋给Image控件显示出来
            image.Source = this.infraredBitmap;

            //启动体感器
            this.KinectDevice.Open();

        }
     
        void InfraredFrameReader_FrameArrived(object sender, InfraredFrameArrivedEventArgs e)
        {
            //打开一张红外图像帧
            using(InfraredFrame irFrame = e.FrameReference.AcquireFrame())
            {
                //确定红外数据有效
                if(irFrame != null)
                {
                    //将红外图像帧拷贝到数组
                    irFrame.CopyFrameDataToArray(this.infraredData);

                    //遍历数组
                    for(int i=0;i<this.infraredData.Length;i++)
                    {
                        byte intensity = (byte)(this.infraredData[i] >> 8);
                        this.infraredDataConverted[i * 4] = intensity;
                        this.infraredDataConverted[i * 4 + 1] = intensity;
                        this.infraredDataConverted[i * 4 + 2] = intensity;
                        this.infraredDataConverted[i * 4 + 3] = 255;
                    }
                    //更新到位图
                    this.infraredBitmap.WritePixels(new Int32Rect(0, 0, 512,424), this.infraredDataConverted, 512*4, 0);
                }
            }
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            //程序关闭,记得手动释放是一个良好的习惯,虽然系统也可能帮我做了释放工作
            //红外图像关闭处理
            if (this.infraredFrameReader != null)
            {
                this.infraredFrameReader.Dispose();
                this.infraredFrameReader = null;
            }

            //体感器关闭处理
            if (this.KinectDevice != null)
            {
                this.KinectDevice.Close();
                this.KinectDevice = null;
            }
        }
    }
}

源代码地址,有需要的可以去下: 点击打开链接 http://download.csdn.net/download/htiannuo/9466835



  • 4
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值