EmguCV+Kinect2.0获取高清面部帧

基础信息请看这,本文中的代码是基于这篇文章中的代码修改而得EmguCv +Kinect2.0 获取面部信息


这段代码依然只能追踪一个人,但我将所追踪到的人脸与彩色帧进行的重合,所以用到了CoordinateMap来转换坐标

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

namespace Kinect_HDface
{
    public partial class Form1 : Form
    {
        KinectSensor kinect = null;
        MultiSourceFrameReader reader = null;
        HighDefinitionFaceFrameSource facesource = null;
        HighDefinitionFaceFrameReader facereader = null;
        FrameDescription fd = null;
        Image<Bgra, byte> colorimg = null;
        Body[] bodies = null;
        Body target;
        byte[] cdata;
        FaceAlignment currentFaceAlignment = null;
        FaceModel currentFaceModel = null;
        CoordinateMapper coordinate = null;
        public Form1()
        {
            InitializeComponent();
            kinect = KinectSensor.GetDefault();
            reader = kinect.OpenMultiSourceFrameReader(FrameSourceTypes.Body | FrameSourceTypes.Color);
            reader.MultiSourceFrameArrived += Reader_MultiSourceFrameArrived;
            facesource = new HighDefinitionFaceFrameSource(kinect);
            facereader = facesource.OpenReader();
            facereader.FrameArrived += Facereader_FrameArrived;
            fd = kinect.ColorFrameSource.FrameDescription;
            colorimg = new Image<Bgra, byte>(fd.Width, fd.Height);
            cdata = new byte[colorimg.Bytes.Count<byte>()];
            currentFaceModel = new FaceModel();
            currentFaceAlignment = new FaceAlignment();
            coordinate = kinect.CoordinateMapper;
            kinect.Open();
        }

        private void Facereader_FrameArrived(object sender, HighDefinitionFaceFrameArrivedEventArgs e)
        {
            using (HighDefinitionFaceFrame fframe = e.FrameReference.AcquireFrame())
            {
                if (fframe == null)
                    return;
                if (!fframe.IsTrackingIdValid)
                    return;

                fframe.GetAndRefreshFaceAlignmentResult(currentFaceAlignment);
                if (currentFaceAlignment == null)
                    return;
                var vertices = this.currentFaceModel.CalculateVerticesForAlignment(this.currentFaceAlignment);
                ColorSpacePoint[] colorpoint = new ColorSpacePoint[vertices.Count];
                coordinate.MapCameraPointsToColorSpace(vertices.ToArray<CameraSpacePoint>(), colorpoint);
                foreach (var point in colorpoint)
                {
                    CvInvoke.Circle(colorimg, new Point((int)point.X , (int)point.Y), 2, new Bgra(0, 0, 255, 255).MCvScalar, 2);
                }
            }
        }

        private void Reader_MultiSourceFrameArrived(object sender, MultiSourceFrameArrivedEventArgs e)
        {
            MultiSourceFrame frame = e.FrameReference.AcquireFrame();
            if (frame == null)
                return;
            ColorFrame cframe = frame.ColorFrameReference.AcquireFrame();
            BodyFrame bframe = frame.BodyFrameReference.AcquireFrame();
            if (cframe == null || bframe == null)
                return;
            if (bodies == null)
                bodies = new Body[bframe.BodyCount];
            bframe.GetAndRefreshBodyData(bodies);

            if (!facesource.IsTrackingIdValid)
            {
                target = (from body in bodies where body.IsTracked select body).FirstOrDefault();
                if (target != null)
                {
                    facesource.TrackingId = target.TrackingId;
                }
            }
            cframe.CopyConvertedFrameDataToArray(cdata, ColorImageFormat.Bgra);
            colorimg.Bytes = cdata;
            imageBox1.Image = colorimg;

            cframe.Dispose();
            bframe.Dispose();
        }

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

        }
    }
}
运行效果图



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