EmguCV+Kinect2.0获取BodyIndex图

来源:互联网 发布:知客是什么意思 编辑:程序博客网 时间:2024/06/06 09:03

我的环境为KinetSDK2.0+EmguCV3.0.0+vs2015

依旧为WinFrom ,并在主窗口内拖入ImageBox

不知道ImageBox的可以看我之前的博文http://blog.csdn.net/qq_22033759/article/details/47299269

代码中需要用到指针,需要在项目属性中调一下

上代码

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using Microsoft.Kinect;using Emgu.CV;using Emgu.CV.Util;using Emgu.CV.CvEnum;using Emgu.CV.Structure;using Emgu.Util;namespace EmguCV_Kinect_2{    public partial class Form1 : Form    {        KinectSensor kinect = null;        BodyIndexFrameReader bindexframereader = null;        FrameDescription fd = null;        Image<Bgra, byte> bodyindeximg;        byte[] data = null;        public Form1()        {            InitializeComponent();            kinect = KinectSensor.GetDefault();            bindexframereader = kinect.BodyIndexFrameSource.OpenReader();            bindexframereader.FrameArrived += Bindexframereader_FrameArrived;            fd = kinect.BodyIndexFrameSource.FrameDescription;                        bodyindeximg = new Image<Bgra, byte>(fd.Width, fd.Height);            data = new byte[fd.LengthInPixels*4];            kinect.Open();Console.WriteLine(bodyindeximg.Bytes.Count<byte>());        }        private unsafe void ProcessBodyIndexFrameData(IntPtr bodyIndexFrameData, uint bodyIndexFrameDataSize)        {            byte* frameData = (byte*)bodyIndexFrameData;            for (int i = 0; i < (int)bodyIndexFrameDataSize; ++i)            {                if (frameData[i] <6)                {                    this.data[i*4] = 0;                    this.data[i * 4+1] = 0;                    this.data[i * 4+2] = 150;                      this.data[i * 4+3] = 255;     //人物显示为红色                }                else                {                    this.data[i*4] =0;                    this.data[i * 4+1] = 0;                    this.data[i * 4+2] = 0;                    this.data[i * 4+3] = 255;     //背景显示为黑色                }            }        }        private void Bindexframereader_FrameArrived(object sender, BodyIndexFrameArrivedEventArgs e)        {            using (BodyIndexFrame bframe = e.FrameReference.AcquireFrame())            {                if (bframe != null)                {                    using (KinectBuffer kB = bframe.LockImageBuffer())                    {                        if(fd.Width*fd.Height==kB.Size)                            this.ProcessBodyIndexFrameData(kB.UnderlyingBuffer, kB.Size);                        bodyindeximg.Bytes = data;                        imageBox1.Image = bodyindeximg;                    }                }            }        }        private void Form1_FormClosing(object sender, FormClosingEventArgs e)        {            if (this.kinect != null)            {                this.kinect.Close();                this.kinect = null;            }        }    }}
运行图如下:


再安利一个日本人写的博客,写的不错,只不过要用翻译才能看的懂,用的是opencv+kinectsdk

地址在这:http://www.buildinsider.net/small/kinectv2cpp

0 0
原创粉丝点击