当前,我们正在使用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+ cm的距离上这不是问题。