Kinect2.0+EmguCV获取景深图

先说一下我的环境:

Kinect sdk2.0+EmguCV3.0.0+vs2015

具体代码如下

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;
        ushort[] ddata;
        byte[] ddataconvert;
        DepthFrameReader depthframereader;
	Image<Bgr, byte> img;
        public Form1()
        {
            kinect = KinectSensor.GetDefault();
            depthframereader = kinect.DepthFrameSource.OpenReader();
            depthframereader.FrameArrived += Depthframereader_FrameArrived;
            CvInvoke.UseOpenCL = false;
            FrameDescription fd = kinect.DepthFrameSource.FrameDescription;
            ddata = new ushort[fd.LengthInPixels];
            ddataconvert = new byte[fd.LengthInPixels * 3];
	    img = new Image<Bgr, byte>(fd.Width, fd.Height);
            InitializeComponent();
            kinect.Open();
        }


        private void Depthframereader_FrameArrived(object sender, DepthFrameArrivedEventArgs e)
        {
            using (DepthFrame dframe = e.FrameReference.AcquireFrame())
            {
                if (dframe != null)
                {
                    dframe.CopyFrameDataToArray(ddata);
                    for (int i = 0; i < ddata.Length; i++)
                    {
                        byte intensity = (byte)(ddata[i]);
                        ddataconvert[i * 3] = intensity;
                        ddataconvert[i * 3 + 1] = intensity;
                        ddataconvert[i * 3 + 2] = intensity;
                    }
                    img.Bytes = ddataconvert;
                    imageBox1.Image = img;
                }
            }
        }

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




运行效果如下,因为就是直接从原始数据只复制过来的所以和sdk示例中的D2D里的DepthBasic效果一样(右图为示例中D2D)


如果想让效果和c#示例的一样,修改这里就可以,


代码段如下(也可把5改为其他数值,只是5的话和示例效果一致)

for (int i = 0; i < ddata.Length; i++)
{
	intensity = (byte)(ddata[i] >> 5);
        ddataconvert[i * 3] = intensity;
        ddataconvert[i * 3 + 1] = intensity;
        ddataconvert[i * 3 + 2] = intensity;
}


运行图和对比如下图所示


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