纳金网
标题:
[脚本]Unity3D+Kinect OpenNI 开发获取身高【ZIG】
[打印本页]
作者:
铁锹
时间:
2012-12-17 14:10
标题:
[脚本]Unity3D+Kinect OpenNI 开发获取身高【ZIG】
本文与大家分享的是Kinect Openni在Unity3D中获取人的身高的脚本!原作者:taohuagege1 ,请往下阅读:
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;
}
}
作者:
.
时间:
2012-12-17 21:20
好棒哦!哈哈~期待楼主有更多的作品分享给我们!
欢迎光临 纳金网 (http://wwww.narkii.com/club/)
Powered by Discuz! X2.5