EmguCV+Kinect2.0获取BodyIndex图

我的环境为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

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

cccccc1212

这是c币不是人民币,不要充值

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值