首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >UnityKinectDepthExplorer -如何在Kinect v1中使用此示例?

UnityKinectDepthExplorer -如何在Kinect v1中使用此示例?
EN

Stack Overflow用户
提问于 2017-01-18 20:42:22
回答 1查看 648关注 0票数 1

我对使用Windows版Kinect在Unity中显示深度数据很感兴趣。不幸的是,我使用的是Kinect v1.8,我知道使用Kinect V2 (Kinect Fusion)很容易做到这一点。

我找到了这个例子:我安装了所有必要的组件:https://github.com/rickbarraza/UnityKinectDepthExplorer

无法加载'Assets/Plugins/x86_64/KinectUnityAddin.dll‘

关于我到目前为止阅读的所有帖子和论坛,这是因为我使用的是SDK 1.8 (而不是SDK 2.0)。

有没有办法在Kinect v1.8中使用这个例子?我也试着联系作者,但他没有回复。

非常感谢!

EN

回答 1

Stack Overflow用户

发布于 2018-07-03 03:10:13

我在一个大学项目中也遇到了同样的问题,希望你不要迟到。这是我制作的Kinect v1.8包装器,仅用于读取kinect深度图像。我使用来自Unity-Assetstore的带有MS-SDK的Kinect作为入门指南,你不必下载它。你所需要的就是这个代码。使用Init()方法启动,使用GetDepthArray()函数获取Deapth帧。希望它能为你工作!

您必须安装Kinect 1.8 sdk或在Windows/System32文件夹中至少具有1.8 sdk中的Kinect10.dll

代码语言:javascript
复制
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Runtime.InteropServices;
using System;
using System.Runtime.CompilerServices;

public class OwnKinectWrapper : MonoBehaviour {

DepthBuffer db;

#region Nui Variables/Structs/Intefraces
IntPtr streamReferenz;

public struct NuiImageViewArea
{
    public int eDigitalZoom;
    public int lCenterX;
    public int lCenterY;
}

public struct NuiSurfaceDesc
{
    uint width;
    uint height;
}

public struct DepthBuffer
{
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 640 * 480, ArraySubType = UnmanagedType.U2)]
    public ushort[] pixels;
}

public struct NuiLockedRect
{
    public int pitch;
    public int size;
    public IntPtr pBits;

}
public struct NuiImageFrame
{
    public Int64 liTimeStamp;
    public uint dwFrameNumber;
    public int eImageType;
    public int eResolution;
    public IntPtr pFrameTexture;
    public uint dwFrameFlags_NotUsed;
    public NuiImageViewArea ViewArea_NotUsed;
}



[Guid("13ea17f5-ff2e-4670-9ee5-1297a6e880d1")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[ComImport()]
public interface INuiFrameTexture
{
    [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
    [PreserveSig]
    int BufferLen();
    [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
    [PreserveSig]
    int Pitch();
    [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
    [PreserveSig]
    int LockRect(uint Level, ref NuiLockedRect pLockedRect, IntPtr pRect, uint Flags);
    [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
    [PreserveSig]
    int GetLevelDesc(uint Level, ref NuiSurfaceDesc pDesc);
    [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
    [PreserveSig]
    int UnlockRect(uint Level);
}
#endregion
// Use this for initialization
public int Init() {
    int init = 0;
    try {
        init = NuiInitialize(0x00000020);
        Debug.Log("init : " + init);

        streamReferenz = IntPtr.Zero;
        NuiImageStreamOpen(4, 2, 0, 2, IntPtr.Zero, ref streamReferenz);



    }
    catch (DllNotFoundException e)
    {
        string message = "Please check the Kinect SDK installation.";
        Debug.LogError(message);
        Debug.LogError(e.ToString());

        return -1;
    }
    catch (Exception e)
    {
        Debug.LogError(e.ToString());

        return -1;
    }

    return init;
}

// Update is called once per frame
void Update()
{
    IntPtr imageStreamFrameReferenz = IntPtr.Zero;
    int test = NuiImageStreamGetNextFrame(streamReferenz, 0, ref imageStreamFrameReferenz);
    if (test == 0) {
        NuiImageFrame imageFrame = (NuiImageFrame)Marshal.PtrToStructure(imageStreamFrameReferenz, typeof(NuiImageFrame));
        INuiFrameTexture frameTexture = (INuiFrameTexture)Marshal.GetObjectForIUnknown(imageFrame.pFrameTexture);

        NuiLockedRect lockedRectPtr = new NuiLockedRect();
        IntPtr r = IntPtr.Zero;
        frameTexture.LockRect(0, ref lockedRectPtr, r, 0);
        db = (DepthBuffer)Marshal.PtrToStructure(lockedRectPtr.pBits, typeof(DepthBuffer));
        frameTexture.UnlockRect(0);
        NuiImageStreamReleaseFrame(streamReferenz, imageStreamFrameReferenz);
    }
}

void OnDisable() {
    NuiShutdown();
}

public ushort[] GetDepthArray(){
    return db.pixels;
}


[DllImportAttribute(@"Kinect10.dll",EntryPoint="NuiInitialize")]
public static extern int NuiInitialize (uint dwFlags);


[DllImportAttribute(@"Kinect10.dll", EntryPoint = "NuiImageStreamOpen")]
public static extern int NuiImageStreamOpen(int enumImageType,int enumImgageResolution, uint image_Flags, uint frameBufferLimit, IntPtr nextFrameEvent, ref IntPtr streamHandle );


[DllImportAttribute(@"Kinect10.dll", EntryPoint = "NuiImageStreamGetNextFrame")]
public static extern int NuiImageStreamGetNextFrame(IntPtr streamReferenz, uint dwMillisecondsToWait, ref IntPtr ImageFrameReferenz);


[DllImportAttribute(@"Kinect10.dll", EntryPoint = "NuiImageStreamReleaseFrame")]
public static extern int NuiImageStreamReleaseFrame(IntPtr phStreamHandle, IntPtr ppcImageFrame);


[DllImportAttribute(@"Kinect10.dll",EntryPoint="NuiDepthPixelToDepth")]
public static extern ushort NuiDepthPixelToDepth (ushort depthPixel);


[DllImportAttribute(@"Kinect10.dll", EntryPoint = "NuiShutdown")]
public static extern void NuiShutdown();


}

如果有些东西不工作,或者你需要帮助,你可以随意写几行

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41719743

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档