【Kinect】Kinect2函數使用

開發部分實現方法:
===============================================================================
獲取人物關節點的注意事項:
//1.先檢查人物是否被檢測
if (_manager.IsUserDetected())
//2.獲取人物ID
long userId = _manager.GetPrimaryUserID();
//3.關節索引
int jointIndex = (int)KinectInterop.JointType.HandLeft;
//4.判斷需要跟蹤的關節點是否已經被識別
if (_manager.IsJointTracked(userId, jointIndex))
//5.獲取骨骼節點信息
Vector3 leftHandPos = _manager.GetJointKinectPosition(userId, jointIndex);

===============================================================================
獲取手勢信息 
 //獲取右手狀態
KinectInterop.HandState rightHandState = _manager.GetRightHandState(userId);
//判斷右手狀態值
if (rightHandState == KinectInterop.HandState.Closed)
{
debugText.text = "右手握拳";
}
else if (rightHandState == KinectInterop.HandState.Open)
{
debugText.text = "右手展開";
}
else if (rightHandState == KinectInterop.HandState.Lasso)
{
debugText.text = "yes 手勢";
}
===============================================================================
Kinect座標到屏幕座標的轉換
//1.獲取骨骼節點座標
Vector3 jointKinectPos = _manager.GetJointKinectPosition(userId, jointIndex);
//2.空間座標轉換成深度座標
Vector2 posDepth = _manager.MapSpacePointToDepthCoords(jointKinectPos);
//3.根據深度座標獲取深度值
ushort depthValue = _manager.GetDepthForPixel((int)posDepth.x, (int)posDepth.y);
if (depthValue > 0)//深度值大於0
{
//4.深度圖像轉換成彩色圖像
Vector2 posColor = _manager.MapDepthPointToColorCoords(posDepth, depthValue);
//5.按彩色圖像轉換座標
float xNorm = (float)posColor.x / _manager.GetColorImageWidth();
float yNorm = (float)posColor.y / _manager.GetColorImageHeight();

if (overlayObject)
{
//將視口座標轉換爲空間座標
    Vector3 vpos = Camera.main.ViewportToWorldPoint(xNorm, yNorm, distanceToCamera);
    overlayObject.transform.position = Vector3.Lerp(overlayObject.transform.position, vpos, Time.deltaTime * 5);
}
}
===============================================================================
//UI點擊的實現
1.獲取右手(左手/關節點)的3D座標
Vector3 rightHandPos = _manager.GetJointKinectPosition(userId, jointIndex);//右手座標
2.3D座標轉換成屏幕座標
Vector3 rightScreenPos = Camera.main.WorldToScreenPoint(rightHandPos);//轉換成屏幕座標
Vector2 screenPosTemp = new Vector2(rightScreenPos.x, rightScreenPos.y);//屏幕座標
3.屏幕座標轉換成UGUI座標(得到手的位置在UGUI的位置)
 Vector2 uguiPos;
if (RectTransformUtility.ScreenPointToLocalPointInRectangle(canvas, screenPosTemp, null, out uguiPos))
{
hand.anchoredPosition = uguiPos;
}
4.判斷手的位置是否在UGUI控件上方
bool isOverBtn1 = RectTransformUtility.RectangleContainsScreenPoint(btn1, screenPosTemp, null);
5.手如果在控件上方,判斷是否握拳
 //獲取右手狀態
KinectInterop.HandState rightHandState = _manager.GetRightHandState(userId);
//判斷右手狀態值
if (rightHandState == KinectInterop.HandState.Closed)
{
if (isOverBtn1)
print("握住btn1");
}
else if (rightHandState == KinectInterop.HandState.Open){}
else if (rightHandState == KinectInterop.HandState.Lasso){}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章