欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Kinect获取深度图像,并呈现在Plane上

程序员文章站 2022-05-13 15:08:15
...

1、创建一个Plane,材质球的属性如下。

Kinect获取深度图像,并呈现在Plane上

Kinect获取深度图像,并呈现在Plane上

2、先看效果,因为是Plane面板,所以位置是可以随便调整的。

Kinect获取深度图像,并呈现在Plane上

2、脚本如下。值得一提的是:此脚本只锁定第一个人,其他人不会被识别和成像。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
public class MyDepthView1 : MonoBehaviour
{
    public KinectManager manager;
    private int depthImageWidth;
    private int depthImageHeight;
    public UnityEngine.GameObject objplan;
    private  Texture2D texture;
    ushort[] rawDepthMap;
    //人面对摄像机  前后左右 距离
    private float scope_X_left =  -0.5f;
    private float scope_X_right=0.5f;
    private float scope_Z_bef =0.5f;
    private float scope_Z_aft =1.5f;
    Color[] Depthcolors;// = new Color[512 * 424];
    ////  xml.SaveToFile(name);
    //顺带 显示 彩色图像-----------------------
    Color32[] coltodep;//-----
    private Texture2D textureCol;
    Vector2[] myv2;
    void Start () {
        manager = KinectManager.Instance;
        //-------------------------------------------------
        if (manager && manager.IsInitialized())
        {
            KinectInterop.SensorData sensorData = manager.GetSensorData();
            if (sensorData != null && sensorData.sensorInterface != null)
            {
                //深度图像的宽高
                depthImageWidth = sensorData.depthImageWidth;
                depthImageHeight = sensorData.depthImageHeight;
                //深度图像的每一个像素点的颜色
                Depthcolors = new Color[depthImageWidth * depthImageHeight];
                //深度图像每一个像素点的坐标
                myv2 = new Vector2[217088]; //深度512*424 = 217088
                //创建一个宽高为512*424具有alpha的纹理的Texture2D
               texture = new Texture2D(depthImageWidth, depthImageHeight, TextureFormat.ARGB32, false);
               texture.wrapMode = TextureWrapMode.Clamp;
               texture.filterMode = FilterMode.Trilinear;
                rawDepthMap = sensorData.depthImage;//深度数据
                }
            }
        }
 private Int64 userID1;
    private Int64 userID2;
    private Int64 userID3;
    private Int64 userID4;
    private Int64 userID5;
    private Int64 userID6;
 // 刚开始找到人吗 
 private bool gbl_zhaodaoL = false;
    //suoding的ID
    private Int64 userID_suoding;
    //
    static public Int64 userID_suodingA;//给别的脚本用
 static public Int64 sd_userID;
 static public Vector3 posPointManLeft;//一定的距离 范围内---
 Vector3 posPointMan1;
 Vector3 posPointMan2;
 Vector3 posPointMan3;
 Vector3 posPointMan4;
 Vector3 posPointMan5;
 Vector3 posPointMan6;

    //人 的 位置
    Vector3 posPointMan1A;
    //------------------------------------------------------------
    //检测范围  锁定  动作触发  移动位置
private int ttt222;
private int myIdesx;
 public ushort myIdesx1;
    void Update () {
     
        if (manager && manager.IsInitialized())
        {
            KinectInterop.SensorData sensorData = manager.GetSensorData();
            if (sensorData != null && sensorData.sensorInterface != null)
            {
                depthImageWidth = sensorData.depthImageWidth;
                depthImageHeight = sensorData.depthImageHeight;
                int pp = -1;
                //------------------------------锁定人物-----------------------------------------
                  // overlay all joints in the skeleton  至少有一个
                if (manager.IsUserDetected())
                {
                    userID1 = manager ? manager.GetUserIdByIndex(0) : 0;
                    userID2 = manager ? manager.GetUserIdByIndex(1) : 0;
                    userID3 = manager ? manager.GetUserIdByIndex(2) : 0;
                    userID4 = manager ? manager.GetUserIdByIndex(3) : 0;
                    userID5 = manager ? manager.GetUserIdByIndex(4) : 0;
                    userID6 = manager ? manager.GetUserIdByIndex(5) : 0;
                    if (gbl_zhaodaoL == false)
                    {
                        if ((userID1 != 0))
                        {
                            posPointMan1 = manager.GetUserPosition(userID1);//-------------------------
                            if ((posPointMan1.z> scope_Z_bef) &&(posPointMan1.z< scope_Z_aft) &&(posPointMan1.x> scope_X_left) &&(posPointMan1.x< scope_X_right))//z 前正  x 左 负 右 正
                            {
                                gbl_zhaodaoL = true;
                                userID_suoding = userID1;
                            }
                        }
                        if ((userID2 != 0))
                        {
                            posPointMan2 = manager.GetUserPosition(userID2);//-------------------------
                            if ((posPointMan2.z > scope_Z_bef) && (posPointMan2.z < scope_Z_aft) && (posPointMan2.x > scope_X_left) && (posPointMan2.x < scope_X_right))//z 前正  x 左 负 右 正
                            {
                                gbl_zhaodaoL = true;
                                userID_suoding = userID2;
                            }
                        }
                        if ((userID3 != 0))
                        {
                            posPointMan3 = manager.GetUserPosition(userID3);//-------------------------
                            if ((posPointMan3.z > scope_Z_bef) && (posPointMan3.z < scope_Z_aft) && (posPointMan3.x > scope_X_left) && (posPointMan3.x < scope_X_right))//z 前正  x 左 负 右 正
                            {
                                gbl_zhaodaoL = true;
                                userID_suoding = userID3;
                            }
                        }
                        if ((userID4 != 0))
                        {
                            posPointMan4 = manager.GetUserPosition(userID4);//-------------------------
                            if ((posPointMan4.z > scope_Z_bef) && (posPointMan4.z < scope_Z_aft) && (posPointMan4.x > scope_X_left) && (posPointMan4.x < scope_X_right))//z 前正  x 左 负 右 正
                            {
                                gbl_zhaodaoL = true;
                                userID_suoding = userID4;
                            }
                        }
                        if ((userID5 != 0))
                        {
                            posPointMan5 = manager.GetUserPosition(userID5);//-------------------------
                            if ((posPointMan5.z > scope_Z_bef) && (posPointMan5.z < scope_Z_aft) && (posPointMan5.x > scope_X_left) && (posPointMan5.x < scope_X_right))//z 前正  x 左 负 右 正
                            {

                                gbl_zhaodaoL = true;
                                userID_suoding = userID5;
                            }
                        }

                        if ((userID6 != 0))
                        {
                            posPointMan6 = manager.GetUserPosition(userID6);//-------------------------
                            if ((posPointMan6.z > scope_Z_bef) && (posPointMan6.z < scope_Z_aft) && (posPointMan6.x > scope_X_left) && (posPointMan6.x < scope_X_right))//z 前正  x 左 负 右 正
                            {
                                gbl_zhaodaoL = true;
                                userID_suoding = userID6;
                            }
                        }
                    }
                    //Vector2 posDepth;
                    if (gbl_zhaodaoL)
                    {
                        posPointManLeft = manager.GetUserPosition(userID_suoding);
                        if ((posPointManLeft.z > scope_Z_bef) && (posPointManLeft.z < scope_Z_aft) && (posPointManLeft.x > scope_X_left) && (posPointManLeft.x < scope_X_right))//z 前正  x 左 负 右 正
                        {
                            //gbl_zhaodaoL = true;
                            //userID_suoding = userID6;
                        }
                        else
                        {
                            gbl_zhaodaoL = false; //找到的离开了区域 重新找
                            userID_suoding = 0;
                        }

                  
                        pp = manager.GetBodyIndexByUserId(userID_suoding);
                        sd_userID = userID_suoding;
                
                    }
                }
                else
                {
                    userID1 =  0;
                    userID2 =  0;
                    userID3 =  0;
                    userID4 =  0;
                    userID5 =  0;
                    userID6 =  0;

                    gbl_zhaodaoL = false; //找到的离开了区域 重新找   摄像机本身都没找到 重新找
                    userID_suoding = 0;
                }
                myIdesx = pp;//锁定人的IDex               
                rawDepthMap = sensorData.depthImage;//深度数据
                for (int i = 0; i < depthImageHeight; i++)
                {
                    for (int k = 0; k < depthImageWidth; k++)
                    {
                        int Didx = depthImageWidth * i + k;
                        //ushort userDepth = sensorData.depthImage[Didx];  //
                        ushort userMap = sensorData.bodyIndexImage[depthImageWidth * i + k];//
                        Depthcolors[depthImageWidth * i + k] = Color.clear;
                        float r = ((float)sensorData.depthImage[depthImageWidth * i + k]) / 8000;
                        float g = ((float)sensorData.depthImage[depthImageWidth * i + k]) / 8000;
                        float b = ((float)sensorData.depthImage[depthImageWidth * i + k]) / 8000;
                        Color tempColor = new Color(r, g, b, 0);//------------透明度 0  其它不显示 只显示人
                        Depthcolors[depthImageWidth * i + k] = tempColor;
                        if(userMap!=255)//小绿人
                        {
                            ttt222 = (int)userMap & 0x07; 
                            if(ttt222==myIdesx) //小绿人
                            {
                                Color tempColor1 = new Color(0, g, 0, 1);
                                Depthcolors[depthImageWidth * i + k] = tempColor1;

                            }
                        }
                    }
                }       
                texture.SetPixels(Depthcolors);
                texture.Apply();
                objplan.GetComponent<MeshRenderer>().material.mainTexture = texture;//可以显示 扣出来的彩色图像  也可以显示深度图像--
                userID_suodingA = userID_suoding;
            }
        }
	}
}


 

相关标签: Kinect