EmguCv +Kinect2.0 获取面部信息

我使用的环境为EmguCv3.0.0+KinectSDK2.0+vs2015

使用Kinect获取面部帧比较特殊,必须要在x64环境下才行,另外,还需要将示例项目文件夹中的NuiDatabase复制到程序的根目录里


利用face库里的FaceFrameFeatures可以很方便的获取面部的多种信息

 FaceFrameFeatures DefaultFaceFrameFeatures = FaceFrameFeatures.PointsInColorSpace
                                        | FaceFrameFeatures.RotationOrientation
                                        | FaceFrameFeatures.BoundingBoxInColorSpace;
我这只获取了在这个项目中用的到的几种,下图是全部的可获取的信息



还是用的WinFrom和ImageBox

我这个和SDK里的不同 只能追踪一个人

代码如下:

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 EmguCV_Kinect_Face
{
    public partial class Form1 : Form
    {
        KinectSensor kinect = null;
        MultiSourceFrameReader reader = null;
        FaceFrameSource facesource = null;
        FaceFrameReader facereader = null;
        FrameDescription fd = null;
        Image<Bgra, byte> colorimg = null;
        Body[] bodies = null;
        Body target;
        byte[] cdata;
        FaceFrameFeatures DefaultFaceFrameFeatures = FaceFrameFeatures.PointsInColorSpace
                                        | FaceFrameFeatures.RotationOrientation
                                        | FaceFrameFeatures.BoundingBoxInColorSpace;
        public Form1()
        {
            InitializeComponent();
            kinect = KinectSensor.GetDefault();
            reader = kinect.OpenMultiSourceFrameReader(FrameSourceTypes.Body | FrameSourceTypes.Color);
            reader.MultiSourceFrameArrived += Reader_MultiSourceFrameArrived;
            facesource = new FaceFrameSource(kinect, 0, DefaultFaceFrameFeatures);
            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>()];
            kinect.Open();
        }

        private void Facereader_FrameArrived(object sender, FaceFrameArrivedEventArgs e)
        {
            using (FaceFrame fframe = e.FrameReference.AcquireFrame())
            {
                if (!fframe.IsTrackingIdValid)
                    return;
                var result = fframe.FaceFrameResult;
                if (result == null)
                    return;
                foreach(var point in result.FacePointsInColorSpace)
                {
                    if (point.Key == FacePointType.None)
                        continue;
                    CvInvoke.Circle(colorimg, new Point((int)point.Value.X,(int)point.Value.Y),5, new Bgra(0,0,255,255).MCvScalar, 4);
                }
                var space=result.FaceBoundingBoxInColorSpace;
                Rectangle faceBox = new Rectangle(space.Left, space.Top,space.Right - space.Left, space.Bottom - space.Top) ;
                CvInvoke.Rectangle(colorimg, faceBox, new Bgra(0, 0, 255, 255).MCvScalar, 5);
                //imageBox2.Image = colorimg.Copy(faceBox);
            }
        }

        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;
            }

        }
    }
}

运行图


评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

cccccc1212

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

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

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

打赏作者

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

抵扣说明:

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

余额充值