EmguCV获取kinect的彩色帧和红外图

如不能正确配置EmguCV  看这里http://blog.csdn.net/qq_22033759/article/details/47299269

我使用的配置是Emgucv3+kinect2.0sdk+vs2015

用的是WinFrom,不是WPF,虽然WPF可以添加WinFrom控件 ,但还是原生的用着方便。

把Emgncv配好后添加kinect的引用


还是要在from中放入一个ImageBox控件;然后再来看代码。先来个彩色帧的吧。

using System;
using System.Windows;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
using Emgu.CV.UI;
using Microsoft.Kinect;

namespace EmguCV_winform
{
    public partial class Form1 : Form
    {
        KinectSensor kinect = null;
        ColorFrameReader colorframereader;
        private byte[] pixels = null;
        Image<Bgra, byte> colorImg
        public Form1()
        {
            kinect = KinectSensor.GetDefault();
            colorframereader = kinect.ColorFrameSource.OpenReader();
            colorframereader.FrameArrived += Colorframereader_FrameArrived;
            FrameDescription colorFrameDescription = this.kinect.ColorFrameSource.CreateFrameDescription(ColorImageFormat.Bgra);
            pixels = new byte[colorFrameDescription.Width * colorFrameDescription.Height * colorFrameDescription.BytesPerPixel];
            colorImg = new Image<Bgra, byte>(colorFrameDescription.Width, colorFrameDescription.Height);
            CvInvoke.UseOpenCL = false;
            InitializeComponent();
            kinect.Open();
        }

        private void Colorframereader_FrameArrived(object sender, ColorFrameArrivedEventArgs e)
        {

            using (ColorFrame colorFrame = e.FrameReference.AcquireFrame())
            {
                if (colorFrame != null)
                {

                    colorFrame.CopyConvertedFrameDataToArray(pixels, ColorImageFormat.Bgra);   //复制出ColorFrame中的图像数据  pixel、colorImg、ColorFrame中包含的真实图像数据大小必须相同,否则会出现异常
                    colorImg.Bytes = pixels;
                    imageBox1.Image = colorImg;
                }
            }
        }
    }
}

最好对窗体在添加一个closing的事件来关闭kinect

EmguCV获取kinect的彩色帧和红外图_第1张图片

代码如下:

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.kinect != null)
            {
                this.kinect.Close();
                this.kinect = null;
            }
        }


运行后就能获得彩色图像 EmguCV获取kinect的彩色帧和红外图_第2张图片

红外图像的获取也不算难 ,就是需要一个转换,这次就用另一个方式来获取吧,MultiSourceFrameReader在同时获取多个帧时特别方便在openreader时指定获取哪些帧就可以了,像这样
reader = kinect.OpenMultiSourceFrameReader(FrameSourceTypes.Infrared | FrameSourceTypes.Body| FrameSourceTypes.Audio| FrameSourceTypes.Depth);

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 Emgu.CV;
using Microsoft.Kinect;
using Emgu.CV.Structure;

namespace emgucv_winfrom_kinect
{
    public partial class Form1 : Form
    {
        KinectSensor kinect = null;
        MultiSourceFrameReader reader;
        Body[] bodies = null;
        ushort[] irData;
        byte[] irDataConverted;
        Image<Bgra, byte> Img;
        public Form1()
        {
            kinect = KinectSensor.GetDefault();
            kinect.Open();
            FrameDescription fd = kinect.InfraredFrameSource.FrameDescription;
            irDataConverted = new byte[fd.LengthInPixels * 4];
            irData = new ushort[fd.LengthInPixels];
            Img = new Image<Bgra, byte>(fd.Width, fd.Height);
            reader = kinect.OpenMultiSourceFrameReader(FrameSourceTypes.Infrared | FrameSourceTypes.Body);
            reader.MultiSourceFrameArrived += reader_MultiSourceFrameArrived;
            InitializeComponent();
        }

        private void reader_MultiSourceFrameArrived(object sender, MultiSourceFrameArrivedEventArgs e)
        {
            var reference = e.FrameReference.AcquireFrame();
            using (var irframe = reference.InfraredFrameReference.AcquireFrame())
            {
                if (irframe != null)
                {
                    irframe.CopyFrameDataToArray(irData);       
                    for (int i = 0; i < irData.Length; i++)          
                    {
                        byte intensity = (byte)(irData[i] >> 8);
                        irDataConverted[i * 4] = intensity;
                        irDataConverted[i * 4 + 1] = intensity;
                        irDataConverted[i * 4 + 2] = intensity;
                        irDataConverted[i * 4 + 3] = 255;
                    }
                    Img.Bytes = irDataConverted;
                    imageBox1.Image = Img;
                }
            }

        }
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            
            if (kinect != null)
            {
                kinect.Close();
                kinect = null;
            }
        }
    }
}

运行截图


你可能感兴趣的:(kinect,EmguCV)