2015-11-10 42 views
2

我們目前使用Kinect來跟蹤窗口中的對象,現在我們需要獲取3D而不是「2D」座標。基本上,我們需要在一個點上獲得LockedRect.pBits的深度值。OpenGL + Kinect SDK - 如何從單個像素獲取深度值?

我們當前的代碼返回隨心所欲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); 

}

回答

2

我們找到了解決辦法!對任何想知道同樣事情的人來說, pBits是USHORT格式中的最小值,並且您希望在它之後找到第x個像素。用矩陣表示它,你想找到例如深度。在5 * 4矩陣中向下2行和3列pBits,給你= 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); 

除了我們不secrifice漂亮的代碼1*width+1差異是不超過40釐米的距離的問題。