C# 使用kinect和Visual Studio跟踪速度
我需要追踪踢腿的速度。我编写了这段代码,但当我运行程序时,即使我的右脚移动得非常快,速度也不会有太大变化。C# 使用kinect和Visual Studio跟踪速度,c#,visual-studio,kinect,C#,Visual Studio,Kinect,我需要追踪踢腿的速度。我编写了这段代码,但当我运行程序时,即使我的右脚移动得非常快,速度也不会有太大变化。 怎么了? 有不同的方法吗 if (bandera == true) { X1 = skeleton.Joints[JointType.FootRight].Position.X; Y1 = skeleton.Joints[JointType.FootRight].Position.Y; Z1 = skeleton.Joints[JointType.FootRight].Position.Z
怎么了? 有不同的方法吗
if (bandera == true)
{
X1 = skeleton.Joints[JointType.FootRight].Position.X;
Y1 = skeleton.Joints[JointType.FootRight].Position.Y;
Z1 = skeleton.Joints[JointType.FootRight].Position.Z;
}
if (bandera == false)
{
X2 = skeleton.Joints[JointType.FootRight].Position.X;
Y2 = skeleton.Joints[JointType.FootRight].Position.Y;
Z2 = skeleton.Joints[JointType.FootRight].Position.Z;
}
bandera = !bandera;
float d= (((X1 - X2) * (X1 - X2)) + ((Y1 - Y2) * (Y1 - Y2)) + ((Z1 - Z2 * (Z1 - Z2))));
double distance = System.Math.Sqrt(d);
double speed= 30 * distance;
Console.WriteLine(speed);
如你所知:速度=距离/时间
我知道每秒有30帧,所以时间=1/30
所以速度等于距离除以(1/30)等于30*距离。。。因为你使用的是相同的点。。。所以当你减法的时候,都是0
X1 = skeleton.Joints[JointType.FootRight].Position.X;
...
X2 = skeleton.Joints[JointType.FootRight].Position.X;
...
相同的点,所以x1=x2
,x1-x2=0
,然后一切都变成梨形
根据您的更新:
X
和Y
中的一些不会被初始化(因为它只会通过其中一种情况)d=t*s
,因此您的距离计算完全正确如果在初始化变量和计算后丢弃一些调试输出,会得到什么?你弄乱了距离公式。放置亲子关系时要格外小心。应该是这样的
float d = (X1 - X2) * (X1 - X2) + (Y1 - Y2) * (Y1 - Y2) + (Z1 - Z2) * (Z1 - Z2);
此外,无需将布尔值与true
进行比较。您还可以将距离计算移到它自己的方法中,以使事情更清楚,并避免需要时间才能找到的错误。我建议这样做:
double distance(Joint a, Joint b)
{
float d2 = (b.X - a.X) * (b.X - a.X) + (b.Y - a.Y) * (b.Y - a.Y) + (b.Z - a.Z) * (b.Z - a.Z);
return System.Math.Sqrt(d2)
}
...
if (bandera)
{
a = skeleton.Joints[JointType.FootRight];
}
else
{
b = skeleton.Joints[JointType.FootRight];
}
bandera = !bandera;
double speed = 30 * distance(a, b);
Console.WriteLine(speed);
您的速度计算很容易出现波动,因此平均10帧可能也会有所帮助。我在代码中看到的唯一错误是在距离计算中缺少括号:(Z1-Z2)必须介于括号之间,否则操作顺序不正确。其他一切都应该很好。正如维斯蒂奇所说,当踢腿进行时,速度会有所不同,因此出于您所说的目的,我建议您保持最大值 萨卢多斯 这段代码(我假设)是在Kinect的帧事件上运行的,正如她所说,它每秒运行30次。因此,每隔一帧,X1和X2更新为不同的值。