【问题标题】:Kinect v2 + Unity -> MapDepthFrameToColorSpaceUsingIntPtrKinect v2 + Unity -> MapDepthFrameToColorSpaceUsingIntPtr
【发布时间】:2015-12-02 20:00:46
【问题描述】:

我正在尝试将来自 KinectV2 的深度数据映射到 Unity-Script 中的色彩空间。 它使用通常的坐标映射器功能按预期工作

_sensor.CoordinateMapper.MapDepthFrameToColorSpace(_depthData, _colorSpacePoints)

但它使我的帧率降低了 11 - 这是不可接受的 =)

因此,我查看了 Microsoft 提供的统一示例,并找到了一些 使用指针进行映射。

var pDepthData = GCHandle.Alloc(pDepthBuffer, GCHandleType.Pinned);
var pDepthCoordinatesData = GCHandle.Alloc(m_pDepthCoordinates, GCHandleType.Pinned);

m_pCoordinateMapper.MapColorFrameToDepthSpaceUsingIntPtr(
        pDepthData.AddrOfPinnedObject(), 
        (uint)pDepthBuffer.Length * sizeof(ushort),
        pDepthCoordinatesData.AddrOfPinnedObject(), 
        (uint)m_pDepthCoordinates.Length);

pDepthCoordinatesData.Free();
pDepthData.Free();

也存在满足我需求的等效方法。我尝试了指针版本 MapDepthFrameToColorSpace

var pDepthData = GCHandle.Alloc(pDepthBuffer, GCHandleType.Pinned);
var pColorData = GCHandle.Alloc(m_pColorSpacePoints, GCHandleType.Pinned);

m_pCoordinateMapper.MapDepthFrameToColorSpaceUsingIntPtr(
        pDepthData.AddrOfPinnedObject(), 
        pDepthBuffer.Length * sizeof(ushort),
        pColorData.AddrOfPinnedObject(),
        (uint)m_pColorSpacePoints.Length * sizeof(float) * 2);

pColorData.Free();
pDepthData.Free();

pDepthBuffer 在方法调用时具有有效数据,并且 m_pColorSpacePoints 已初始化并且 具有与 pDepthBuffer 相同的长度(如 MSDN 文档中推荐的那样) 我仍在使用 1408 SDK 版本。 函数后的结果是一个具有 Empty/NegativeInfinity 浮点值且没有有效值的数组 颜色空间点。也没有错误消息。 有什么建议吗?

【问题讨论】:

    标签: c# unity3d kinect


    【解决方案1】:

    我让它工作了。更新到最新的 SDK 版本 1409 后 messageall 返回错误消息

    ArgumentException: Value does not fall within the expected range. Rethrow as ArgumentException: This API has returned an exception from an HRESULT: 0x80070057

    所以我玩了一点参数,现在我得到了有效的数据。

    这是我对 GreenScreen 示例的 CoordinateMapperManager 所做的修改。我添加了一个缓冲区 对于映射的颜色值,通过深度值位置获取 HD 颜色流中的像素位置,

    //added declaration and initialization
    private ColorSpacePoint[] m_pColorSpacePoints;
    
    //in awake method
    m_pColorSpacePoints = new ColorSpacePoint[pDepthBuffer.Length];
    
    //accesor to the colorspacepoints
    public ColorSpacePoint[] GetColorSpacePointBuffer()
    {
      return m_pColorSpacePoints;
    }
    
    //the new process frame method
    void ProcessFrame()
    {
      var pDepthData = GCHandle.Alloc(pDepthBuffer, GCHandleType.Pinned);
      var pDepthCoordinatesData = GCHandle.Alloc(m_pDepthCoordinates, GCHandleType.Pinned);
      var pColorData = GCHandle.Alloc(m_pColorSpacePoints, GCHandleType.Pinned);
    
      m_pCoordinateMapper.MapColorFrameToDepthSpaceUsingIntPtr(
        pDepthData.AddrOfPinnedObject(), 
        (uint)pDepthBuffer.Length * sizeof(ushort),
        pDepthCoordinatesData.AddrOfPinnedObject(), 
        (uint)m_pDepthCoordinates.Length);
    
    
      m_pCoordinateMapper.MapDepthFrameToColorSpaceUsingIntPtr(
        pDepthData.AddrOfPinnedObject(),
        pDepthBuffer.Length * sizeof(ushort),
        pColorData.AddrOfPinnedObject(),
        (uint)m_pColorSpacePoints.Length);
    
      pColorData.Free();
      pDepthCoordinatesData.Free();
      pDepthData.Free();
    
      m_pColorRGBX.LoadRawTextureData(pColorBuffer);
      m_pColorRGBX.Apply ();
    }
    

    这里有一个如何使用它的例子。我使用 Untiy 在 GPU Shader 中进行渲染。以下 代码是一段缩短的未经测试的代码,用于演示如何获取数据:

    //Size of the Kinect V2 Depth Stream
    var _width = 512;
    var _height = 424;
    var _particleCount = _width * _height;
    
    //Initialize an Array to store position data for particles
    var _particleArray = new Vector3[_particleCount];
    var _colorArray = new Color[_particleCount];
    
    //Get Depth Data
    var _depthData = _coordinateMapperManager.GetDepthPointBuffer();
    
    //Get Mapped Color Space Points
    var _colorSpacePoints = _coordinateMapperManager.GetColorSpacePointBuffer();
    
    //Get the Colorstream as Texture
    var _texture = _coordinateMapperManager.GetColorTexture();
    
    var c = 0;
    for (var i = 0; i < _height; ++i)
    {
      for (var j = 0; j < _width; ++j)
      {
        _particleArray[c++] = new Vector3(j, i);
      }
    }
    
    for (var i = 0; i < _depthData.Length; ++i)
    {
        _colorArray[i] = _texture.GetPixel((int)_colorSpacePoints[i].X, (int)_colorSpacePoints[i].Y);
        _particleArray[i].z = _depthData[i];
    }
    
    //Now we have all Data to build a colored 3D PointCloud
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-13
      • 2018-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多