2

我刚刚从事 kinect 和 C# 编程,所以我非常业余,我想知道关节角度。

我创建此代码只是为了在不使用 kinect 的情况下测试我的技能,但代码有问题。

我认为 vector3v1和 vector3v2作为联合替换,但返回的值 Find_angleNaN.

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

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

3 回答 3

2

获取所需关节的 X 和 Y,然后在其任一侧获取关节。然后看看如何计算角度如何从两个坐标计算角度?

基本上做:

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 侧向骨骼跟踪以了解关节跟踪的功能

于 2012-09-20T02:56:33.950 回答
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 个关节来获得角度。

在此处输入图像描述

于 2012-09-20T12:50:15.223 回答
0

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

例子:

double len_prod = va.Len * vb.Len;
double angle = Math.Acos(dot_pro / len_prod);
于 2012-09-19T17:20:15.867 回答