Kinect v2将颜色坐标映射到相机空间

时间:2015-01-11 16:57:45

标签: c++ kinect kinect-sdk coordinate-transformation

我正在尝试将色彩空间的坐标映射到相机空间。 我正在使用的代码如下:

HRESULT ModelRecognizer::MapColorToCameraCoordinates(const std::vector<ColorSpacePoint>& colorsps, std::vector<CameraSpacePoint>& camerasps)
{
    //Access frame
    HRESULT hr = GetDepthFrame();

    if (SUCCEEDED(hr))
    {
        ICoordinateMapper* pMapper;
        hr = m_pKinectSensor->get_CoordinateMapper(&pMapper);
        if (SUCCEEDED(hr))
        {
            CameraSpacePoint* cameraSpacePoints = new CameraSpacePoint[cColorWidth * cColorHeight];
            hr = pMapper->MapColorFrameToCameraSpace(nDepthWidth * nDepthHeight, depthImageBuffer, cColorWidth * cColorHeight, cameraSpacePoints);
            if (SUCCEEDED(hr))
            {
                for (ColorSpacePoint colorsp : colorsps)
                {
                    long colorIndex = (long)(colorsp.Y * cColorWidth + colorsp.X);
                    CameraSpacePoint csp = cameraSpacePoints[colorIndex];
                    camerasps.push_back(csp);
                }
            }
            delete[] cameraSpacePoints;
        }
    }
    ReleaseDepthFrame();
    return hr;
}

我没有得到任何错误,但是,结果似乎旋转了180度并且有一个偏移量。有谁有人建议我做错了什么?任何帮助表示赞赏。

为了更好地了解我为什么需要这个:

我正在使用open cv跟踪从彩色图像粘贴在桌面上的彩色胶带。然后我在3D网格中的磁带位置创建墙。此外,我正在使用KinectFusion生成表格中其他对象的网格。但是,当我在Meshlab中打开两个网格时,可以清楚地看到错位。我假设在CameraSpace中正确创建了KinectFusion的网格,并且我在上面的函数返回的CameraSpacePoints中创建了墙的网格,我很确定错误在于CoordinateMapping过程。

可以在http://imgur.com/UsrEdZb,ZseN2br#0http://imgur.com/UsrEdZb,ZseN2br#1

找到显示错位的图片

1 个答案:

答案 0 :(得分:3)

我终于明白了:无论出于何种原因,返回的CameraSpacePoints在X和Y的原点进行镜像,但不在Z.如果有人对此有解释,我仍然感兴趣。

现在可以使用以下代码:

/// <summary>
/// Maps coordinates from ColorSpace to CameraSpace
/// Expects that the Points in ColorSpace are mirrored at x (as Kinects returns it by default).
/// </summary>
HRESULT ModelRecognizer::MapColorToCameraCoordinates(const std::vector<ColorSpacePoint>& colorsps, std::vector<CameraSpacePoint>& camerasps)
{
    //Access frame
    HRESULT hr = GetDepthFrame();

    if (SUCCEEDED(hr))
    {
        ICoordinateMapper* pMapper;
        hr = m_pKinectSensor->get_CoordinateMapper(&pMapper);
        if (SUCCEEDED(hr))
        {
            CameraSpacePoint* cameraSpacePoints = new CameraSpacePoint[cColorWidth * cColorHeight];
            hr = pMapper->MapColorFrameToCameraSpace(nDepthWidth * nDepthHeight, depthImageBuffer, cColorWidth * cColorHeight, cameraSpacePoints);
            if (SUCCEEDED(hr))
            {
                for (ColorSpacePoint colorsp : colorsps)
                {
                    int colorX = static_cast<int>(colorsp.X + 0.5f);
                    int colorY = static_cast<int>(colorsp.Y + 0.5f);
                    long colorIndex = (long)(colorY * cColorWidth + colorX);
                    CameraSpacePoint csp = cameraSpacePoints[colorIndex];
                    camerasps.push_back(CameraSpacePoint{ -csp.X, -csp.Y, csp.Z });
                }
            }
            delete[] cameraSpacePoints;
        }
    }
    ReleaseDepthFrame();
    return hr;
}