Unity3D+Kinect OpenNI开发获取身高

Kinect Openni在Unity3D中获取人的身高的脚本。

using UnityEngine;
using System.Collections;
using System;
using System.Collections.Generic;
public class GetHeight : MonoBehaviour {
public static double height=0.0; //人的身高
public float people_kinectSpleed=2.4f; //人到kinect的距离,(kinect自身有效距离大概在1.2m-3.5m之间【XBOX】)
public bool isDynamicGetHeight=false; //是否动态获取身高

void Zig_Update(ZigInput zig) {
foreach (ZigTrackedUser trackedUser in zig.TrackedUsers.Values) {
if(isDynamicGetHeight){ //
if(trackedUser.SkeletonTracked){
height=Height(trackedUser.Skeleton);
}
}else{
if(Math.Round(trackedUser.Position.z / 1000,1)<-people_kinectSpleed){
if (trackedUser.SkeletonTracked && height==0.0) {
height=Height(trackedUser.Skeleton); //得到当前使用用户所有的节点
}
}
}
}
}

public double Height(ZigInputJoint[] skeleton)
{

const double HEAD_DIVERGENCE = 0.1;
ZigInputJoint head = skeleton[(int)ZigJointId.Head];
ZigInputJoint neck = skeleton[(int)ZigJointId.Neck];
ZigInputJoint spine = skeleton[(int)ZigJointId.Torso];
ZigInputJoint waist = skeleton[(int)ZigJointId.Waist];
ZigInputJoint hipLeft = skeleton[(int)ZigJointId.LeftHip];
ZigInputJoint hipRight = skeleton[(int)ZigJointId.RightHip];
ZigInputJoint kneeLeft = skeleton[(int)ZigJointId.LeftKnee];
ZigInputJoint kneeRight = skeleton[(int)ZigJointId.RightKnee];
ZigInputJoint ankleLeft = skeleton[(int)ZigJointId.LeftAnkle];
ZigInputJoint ankleRight = skeleton[(int)ZigJointId.RightAnkle];
ZigInputJoint footLeft = skeleton[(int)ZigJointId.LeftFoot];
ZigInputJoint footRight = skeleton[(int)ZigJointId.RightFoot];
// Find which leg is tracked more accurately.
int legLeftTrackedJoints = NumberOfTrackedJoints(hipLeft, kneeLeft, ankleLeft, footLeft);
int legRightTrackedJoints = NumberOfTrackedJoints(hipRight, kneeRight, ankleRight, footRight);
double legLength = legLeftTrackedJoints > legRightTrackedJoints ? Length(hipLeft, kneeLeft, ankleLeft, footLeft) : Length(hipRight, kneeRight, ankleRight, footRight);
return Math.Round(Length(head, neck, spine, waist) + legLength + HEAD_DIVERGENCE,2);
}

public double Length(ZigInputJoint p1, ZigInputJoint p2)
{

return Mathf.Sqrt(Mathf.Pow((p1.Position.x - p2.Position.x) / 1000, 2) +Mathf.Pow((p1.Position.y - p2.Position.y) / 1000, 2) + Mathf.Pow((p1.Position.z - p2.Position.z) / 1000, 2));
}

public double Length(params ZigInputJoint[] joints)
{
double length = 0;
for (int index = 0; index < joints.Length - 1; index++)
{
length += Length(joints[index], joints[index + 1]);
}
return length;
}

public int NumberOfTrackedJoints(params ZigInputJoint[] joints)
{
int trackedJoints = 0;
foreach (ZigInputJoint joint in joints)
{

trackedJoints++;

}
return trackedJoints;
}
}


 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值