c# - 将 Kinect 的 v2.0 运动存储到 BVH 文件

标签 c# kinect motion-detection

我想将来自 Kinect 2 的 Action 捕捉数据存储为 BVH 文件。我在 Kinect 1 中找到了这样做的代码 here .我浏览了代码,发现了一些我无法理解的地方。 例如,在提到的代码中,我试图理解 Skeleton skel 到底是什么。 对象,在代码中的几个地方找到,实际上是。如果没有,是否有任何已知的应用程序可用于实现预期目的?

编辑:我试图将 Skeleton skel 更改为 Body skel,我认为它是 kinect SDK 2.0 的对应对象。但是,当我尝试获取 body 的位置时出现错误:

tempMotionVektor[0] = -Math.Round( skel.Position.X * 100,2);
tempMotionVektor[1] = Math.Round( skel.Position.Y * 100,2) + 120;
tempMotionVektor[2] = 300 - Math.Round( skel.Position.Z * 100,2);

我在调用 Body skel 的 Position 函数时出错。如何在 sdk 2.0 中检索骨架的 X、Y、Z?我尝试将以上三行改为:

tempMotionVektor[0] = -Math.Round(skel.Joints[0].Position.X * 100, 2);
tempMotionVektor[1] = Math.Round(skel.Joints[0].Position.Y * 100, 2) + 120;
tempMotionVektor[2] = 300 - Math.Round(skel.Joints[0].Position.Z * 100, 2);

编辑:基本上我在组合 bodyBasicsWPF 和 kinect2bvh 后设法存储了 bvh 文件。但是,我存储的骨架似乎效率不高。肘部有奇怪的 Action 。我想了解是否必须更改文件 kinectSkeletonBVH.cp 中的某些内容.更具体地说,kinect 2 版本的关节轴方向有何变化。如何更改以下行:skel.BoneOrientations[JointType.ShoulderCenter].AbsoluteRotation.Quaternion;我试图用 skel.JointOrientations[JointType.ShoulderCenter].Orientation 更改该行.我对吗?我正在使用以下代码将关节添加到 BVHBone 对象:

BVHBone hipCenter = new BVHBone(null, JointType.SpineBase.ToString(), 6, TransAxis.None, true);
BVHBone hipCenter2 = new BVHBone(hipCenter, "HipCenter2", 3, TransAxis.Y, false);
BVHBone spine = new BVHBone(hipCenter2, JointType.SpineMid.ToString(), 3, TransAxis.Y, true);
BVHBone shoulderCenter = new BVHBone(spine, JointType.SpineShoulder.ToString(), 3, TransAxis.Y, true);

BVHBone collarLeft = new BVHBone(shoulderCenter, "CollarLeft", 3, TransAxis.X, false);
BVHBone shoulderLeft = new BVHBone(collarLeft, JointType.ShoulderLeft.ToString(), 3, TransAxis.X, true);
BVHBone elbowLeft = new BVHBone(shoulderLeft, JointType.ElbowLeft.ToString(), 3, TransAxis.X, true);
BVHBone wristLeft = new BVHBone(elbowLeft, JointType.WristLeft.ToString(), 3, TransAxis.X, true);
BVHBone handLeft = new BVHBone(wristLeft, JointType.HandLeft.ToString(), 0, TransAxis.X, true);

BVHBone neck = new BVHBone(shoulderCenter, "Neck", 3, TransAxis.Y, false);
BVHBone head = new BVHBone(neck, JointType.Head.ToString(), 3, TransAxis.Y, true);
BVHBone headtop = new BVHBone(head, "Headtop", 0, TransAxis.None, false);

我无法理解代码中的位置 the axis for every Joint计算得出。

最佳答案

您用于 Kinect 1.0 以获取 BVH 文件的代码使用关节信息通过读取 骨架 来构建骨骼向量。

public static double[] getBoneVectorOutofJointPosition(BVHBone bvhBone, Skeleton skel)
{
    double[] boneVector = new double[3] { 0, 0, 0 };
    double[] boneVectorParent = new double[3] { 0, 0, 0 };
    string boneName = bvhBone.Name;

    JointType Joint;
    if (bvhBone.Root == true)
    {
        boneVector = new double[3] { 0, 0, 0 };
    }
    else
    {
        if (bvhBone.IsKinectJoint == true)
        {
            Joint = KinectSkeletonBVH.String2JointType(boneName);

            boneVector[0] = skel.Joints[Joint].Position.X;
            boneVector[1] = skel.Joints[Joint].Position.Y;
            boneVector[2] = skel.Joints[Joint].Position.Z;
..

来源:Nguyên Lê Đặng - Kinect2BVH.V2

除了Kinect 2.0Skeleton类已经被Body类取代,所以你需要改变它来处理Skeleton类strong>Body,并按照下面引用的步骤获取关节。

// Kinect namespace
using Microsoft.Kinect;

// ...

// Kinect sensor and Kinect stream reader objects
KinectSensor _sensor;
MultiSourceFrameReader _reader;
IList<Body> _bodies;

// Kinect sensor initialization
_sensor = KinectSensor.GetDefault();

if (_sensor != null)
{
    _sensor.Open();
}

We also added a list of bodies, where all of the body/skeleton related data will be saved. If you have developed for Kinect version 1, you notice that the Skeleton class has been replaced by the Body class. Remember the MultiSourceFrameReader? This class gives us access on every stream, including the body stream! We simply need to let the sensor know that we need body tracking functionality by adding an additional parameter when initializing the reader:

_reader = _sensor.OpenMultiSourceFrameReader(FrameSourceTypes.Color |
                                             FrameSourceTypes.Depth |
                                             FrameSourceTypes.Infrared |
                                             FrameSourceTypes.Body);

_reader.MultiSourceFrameArrived += Reader_MultiSourceFrameArrived;

The Reader_MultiSourceFrameArrived method will be called whenever a new frame is available. Let’s specify what will happen in terms of the body data:

  1. Get a reference to the body frame
  2. Check whether the body frame is null – this is crucial
  3. Initialize the _bodies list
  4. Call the GetAndRefreshBodyData method, so as to copy the body data into the list
  5. Loop through the list of bodies and do awesome stuff!

Always remember to check for null values. Kinect provides you with approximately 30 frames per second – anything could be null or missing! Here is the code so far:

void Reader_MultiSourceFrameArrived(object sender,
            MultiSourceFrameArrivedEventArgs e)
{
    var reference = e.FrameReference.AcquireFrame();

    // Color
    // ...

    // Depth
    // ...

    // Infrared
    // ...

    // Body
    using (var frame = reference.BodyFrameReference.AcquireFrame())
    {
        if (frame != null)
        {
            _bodies = new Body[frame.BodyFrameSource.BodyCount];

            frame.GetAndRefreshBodyData(_bodies);

            foreach (var body in _bodies)
            {
                if (body != null)
                {
                    // Do something with the body...
                }
            }
        }
    }
}

This is it! We now have access to the bodies Kinect identifies. Next step is to display the skeleton information on-screen. Each body consists of 25 joints. The sensor provides us with the position (X, Y, Z) and the rotation information for each one of them. Moreover, Kinect lets us know whether the joints are tracked, hypothsized or not tracked. It’s a good practice to check whether a body is tracked before performing any critical functions.

The following code illustrates how we can access the different body joints:

if (body != null)
{
    if (body.IsTracked)
    {
        Joint head = body.Joints[JointType.Head];

        float x = head.Position.X;
        float y = head.Position.Y;
        float z = head.Position.Z;

        // Draw the joints...
    }
}

来源:Vangos Pterneas Blog - KINECT FOR WINDOWS VERSION 2: BODY TRACKING

关于c# - 将 Kinect 的 v2.0 运动存储到 BVH 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30453656/

相关文章:

c# - protobuf 和 List<object> - 如何序列化/反序列化?

c# - 为什么 == 在 System.Double 中被覆盖但在 System.Int32 中没有被覆盖,它的后果是什么?

c++ - 如何获取在深度流中选择的 ROI 的 RGB 值

math - SAD和SSD的区别

c# - 从单轨铁路到 ASP.Net MVC

c# - 等待的网络流信号发送数据

opencv - 使用 Kinect 进行手指/手势识别

java - 我怎样才能解决 CLASSPATH 问题并在 OSX 上编译 OpenKinect Java 示例?

android - 运动检测与节省电池

android - 查看只注册 MotionEvent.ACTION_DOWN