簡體   English   中英

OpenGL + Kinect SDK - 如何從單個像素獲取深度值?

[英]OpenGL + Kinect SDK - how to get depth value from a single pixel?

我們目前正在使用 Kinect 跟蹤窗口中的對象,現在我們需要獲取 3D 而不是“2D”坐標。 基本上,我們需要LockedRect.pBits在單個點處獲得的深度值。

我們當前的代碼返回任意的 0 等,但這里是:

void getDepthData(GLubyte* dest) {


    NUI_IMAGE_FRAME imageFrame;
    NUI_LOCKED_RECT LockedRect;
    if (sensor->NuiImageStreamGetNextFrame(depthStream, 0, &imageFrame) < 0) return;
    INuiFrameTexture* texture = imageFrame.pFrameTexture;
    texture->LockRect(0, &LockedRect, NULL, 0);
    if (LockedRect.Pitch != 0) {
            const USHORT* curr = (const USHORT*)LockedRect.pBits;
            cout << curr;
            const USHORT* dataEnd = curr + (width*height);

            while (curr < dataEnd) {
                    // Get depth in millimeters
                    USHORT depth = NuiDepthPixelToDepth(*curr++);

                    // Draw a grayscale image of the depth:
                    // B,G,R are all set to depth%256, alpha set to 1.
                    for (int i = 0; i < 3; ++i)
                            *dest++ = (BYTE)depth % 256;
                    *dest++ = 0xff;
            }

    }
    texture->UnlockRect(0);
    sensor->NuiImageStreamReleaseFrame(depthStream, &imageFrame);

}

我們找到了解決方案! 對於任何想知道同樣事情的人; pBits 是 USHORT 格式中的最小值,您想找到它之后的第 x 個像素。 在矩陣中描繪它,你想找到深度,例如。 在 5*4 矩陣中 pBits 向下 2 行和右 3 列,為您提供pBits + 2 * width + 3 = pBits 之后的第 13 位(在紙上可視化有助於此)。

void getDepthData(RotatedRect trackBox, GLubyte* dest) {

NUI_IMAGE_FRAME imageFrame;
NUI_LOCKED_RECT LockedRect;
if (sensor->NuiImageStreamGetNextFrame(depthStream, 0, &imageFrame) < 0) return;
INuiFrameTexture* texture = imageFrame.pFrameTexture;
texture->LockRect(0, &LockedRect, NULL, 0);
if (LockedRect.Pitch != 0) {
    USHORT* curr = (USHORT*)(LockedRect.pBits);
    curr = curr + (USHORT)trackBox.center.y * width + (USHORT)trackBox.center.x;

        // Get depth in millimeters
        USHORT depth = NuiDepthPixelToDepth(*curr);

        cout << depth << "\n";

}
texture->UnlockRect(0);
sensor->NuiImageStreamReleaseFrame(depthStream, &imageFrame); 

除了我們的確實為1*width+1差異提供了漂亮的代碼,這在 40+ 厘米的距離上不是問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM