使用Kinect进行身体关节角度

时间:2012-09-19 17:10:45

标签: c# kinect

我刚刚开始使用kinect和C#编程,所以我非常喜欢它 我想知道关节角度。

我创建此代码只是为了测试我的技能而不使用kinect但是我遇到了代码问题。

我假设vector3 v1和vector3 v2作为关节替换,但Find_angle的值返回为NaN

我在这个过程中遗漏了什么?请帮助这方面的任何帮助。

Vector3 V1 = new Vector3(100,40,90);
Vector3 v2 = new Vector3(160,60,90);
public MainWindow()
{
    InitializeComponent();

    Vector3.Normalize(V1);
    Vector3.Normalize(v2);

    float Result = this.find_angle(V1,v2);
    MessageBox.Show(Result.ToString());
}
public float find_angle(Vector3 va, Vector3 vb)
{
    float dot_pro=Vector3.Dot(va, vb);
    double angle = Math.Acos(dot_pro);

    angle = angle * 180 / Math.PI;

    return (float)(angle);
}

3 个答案:

答案 0 :(得分:2)

获取你想要的关节的X和Y,然后在它的任何一侧得到一个关节。然后看看如何计算How do I calculate angle from two coordinates?

的角度

基本上做:

float x1 = joint1.X;
float y1 = joint1.Y;

float x2 = joint2.X;
float y2 = joint2.Y;

float angleRadians;

float diffX = x2 - x1;
float diffY = y2 - y1;

float atan2Result = (float)Math.Atan2(diffX, diffY);
angleRadians = atan2Result / 2;
if (angleRadians < 0.0f)
    angleRadians += (float)Math.PI;

float tempCosine = (float)Math.Cos(angleRadians);
float tempSine = ((float)Math.Sin(angleRadians) * -1);

修改

您可能希望Kinect sideways skeleton tracking了解联合跟踪的功能

答案 1 :(得分:1)

如果您使用Kinect SDK来获取骨架跟踪,您可以使用:

    /// <summary>
    /// Return the angle between 3 Joints
    /// Regresa el ángulo interno dadas 3 Joints
    /// </summary>
    /// <param name="j1"></param>
    /// <param name="j2"></param>
    /// <param name="j3"></param>
    /// <returns></returns>
    public static double AngleBetweenJoints(Joint j1, Joint j2, Joint j3)
    {
        double Angulo = 0;
        double shrhX = j1.Position.X - j2.Position.X;
        double shrhY = j1.Position.Y - j2.Position.Y;
        double shrhZ = j1.Position.Z - j2.Position.Z;
        double hsl = vectorNorm(shrhX, shrhY, shrhZ);
        double unrhX = j3.Position.X - j2.Position.X;
        double unrhY = j3.Position.Y - j2.Position.Y;
        double unrhZ =j3.Position.Z - j2.Position.Z;
        double hul = vectorNorm(unrhX, unrhY, unrhZ);
        double mhshu = shrhX * unrhX + shrhY * unrhY + shrhZ * unrhZ;
        double x = mhshu / (hul * hsl);
        if (x != Double.NaN) 
        {
            if (-1 <= x && x <= 1)
            {
                double angleRad = Math.Acos(x);
                Angulo = angleRad *(180.0 / Math.PI);
            }
            else
                Angulo = 0;


        }
        else
            Angulo = 0;


        return Angulo;

    }


    /// <summary>
    /// Euclidean norm of 3-component Vector
    /// </summary>
    /// <param name="x"></param>
    /// <param name="y"></param>
    /// <param name="z"></param>
    /// <returns></returns>
    private static double vectorNorm(double x, double y, double z)
    {

        return Math.Sqrt(Math.Pow(x, 2) + Math.Pow(y, 2) + Math.Pow(z, 2));

    }

此方法使用3个关节来获得角度。

enter image description here

答案 2 :(得分:0)

我不熟悉它,但你应该在应用acos之前将矢量的乘积除以它们的大小。

示例:

double len_prod = va.Len * vb.Len;
double angle = Math.Acos(dot_pro / len_prod);