使用Kinect 1.0 SDK无法将深度图像拟合到RGB图像

时间:2012-04-12 04:30:45

标签: c++ opencv kinect

我正在尝试将Kinect深度相机像素叠加到RGB相机上。我正在使用带有Xbox Kinect,OpenCV的C ++ Kinect 1.0 SDK并尝试使用新的“NuiImageGetColorPixelCoordinateFrameFromDepthPixelFrameAtResolution”方法。

我已经看到图像以慢动作呈现自身,看起来好像在一帧中多次绘制像素。它首先从顶部和左边界绘制自己,然后它到达一个点(你可以在那里看到45度角),它开始绘制怪异。

我一直试图将我的代码基于亚当·斯密在MSDN forums编写的C#代码,但没有骰子。我已经剥离了叠加的东西,只想绘制深度归一化的深度像素,它应该“在RGB图像中”。

左边的图像是我在尝试将深度图像适合RGB空间时得到的图像,右边的图像是我喜欢看到的“原始”深度图像。我希望我的方法可以创建一个与右边的图像相似的图像,但会有轻微的扭曲。

Messed up depth map

这是我目前的代码和对象定义:

// From initialization
INuiSensor *m_pNuiInstance;
NUI_IMAGE_RESOLUTION m_nuiResolution = NUI_IMAGE_RESOLUTION_640x480;
HANDLE m_pDepthStreamHandle;
IplImage *m_pIplDepthFrame;
IplImage *m_pIplFittedDepthFrame;


m_pIplDepthFrame = cvCreateImage(cvSize(640, 480), 8, 1);
m_pIplFittedDepthFrame = cvCreateImage(cvSize(640, 480), 8, 1);

// Method
IplImage *Kinect::GetRGBFittedDepthFrame() {
    static long *pMappedBits = NULL;

    if (!pMappedBits) {
         pMappedBits = new long[640*480*2];
    }

    NUI_IMAGE_FRAME pNuiFrame;
    NUI_LOCKED_RECT lockedRect;
    HRESULT hr = m_pNuiInstance->NuiImageStreamGetNextFrame(m_pDepthStreamHandle, 0, &pNuiFrame);

    if (FAILED(hr)) {
        // return the older frame
        return m_pIplFittedDepthFrame;
    }

    bool hasPlayerData = HasSkeletalEngine(m_pNuiInstance);

    INuiFrameTexture *pTexture = pNuiFrame.pFrameTexture;
    pTexture->LockRect(0, &lockedRect, NULL, 0);

    if (lockedRect.Pitch != 0) {
        cvZero(m_pIplFittedDepthFrame);

        hr = m_pNuiInstance->NuiImageGetColorPixelCoordinateFrameFromDepthPixelFrameAtResolution(
            m_nuiResolution,
            NUI_IMAGE_RESOLUTION_640x480,
            640 * 480, /* size is previous */ (unsigned short*) lockedRect.pBits,
            (640 * 480) * 2,  /* size is previous */ pMappedBits);

        if (FAILED(hr)) {
            return m_pIplFittedDepthFrame;
        }

    for (int i = 0; i < lockedRect.size; i++) {
            unsigned char* pBuf = (unsigned char*) lockedRect.pBits + i;
            unsigned short* pBufS = (unsigned short*) pBuf;
            unsigned short depth = hasPlayerData ? ((*pBufS) & 0xfff8) >> 3 :  ((*pBufS) & 0xffff);
            unsigned char intensity = depth > 0 ? 255 - (unsigned char) (256 * depth / 0x0fff) : 0;

            long
                x = pMappedBits[i], // tried with *(pMappedBits + (i * 2)),
                y = pMappedBits[i + 1]; // tried with *(pMappedBits + (i * 2) + 1);

            if (x >= 0 && x < m_pIplFittedDepthFrame->width && y >= 0 && y < m_pIplFittedDepthFrame->height) {
                m_pIplFittedDepthFrame->imageData[x + y * m_pIplFittedDepthFrame->widthStep] = intensity;
            }

        }
    }

    pTexture->UnlockRect(0);
    m_pNuiInstance->NuiImageStreamReleaseFrame(m_pDepthStreamHandle, &pNuiFrame);

    return(m_pIplFittedDepthFrame);
}

由于

1 个答案:

答案 0 :(得分:2)

我发现问题在于循环,

for (int i = 0; i < lockedRect.size; i++) {
    // code
}

基于每个字节进行迭代,而不是基于每个短(2个字节)。由于lockedRect.size返回修复只是将增量更改为i + = 2的字节数,更好的是将其更改为sizeof(short),就像这样,

for (int i = 0; i < lockedRect.size; i += sizeof(short)) {
    // code
}