OpenGL + Kinect SDK - 如何从单个像素获取深度值?
OpenGL + Kinect SDK - how to get depth value from a single pixel?
我们目前正在使用 Kinect 跟踪 window 中的对象,现在我们需要获取 3D 而不是“2D”坐标。基本上我们需要单点LockedRect.pBits
得到的深度值
我们当前的代码returns任意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+ 厘米的距离下不是问题。
我们目前正在使用 Kinect 跟踪 window 中的对象,现在我们需要获取 3D 而不是“2D”坐标。基本上我们需要单点LockedRect.pBits
得到的深度值
我们当前的代码returns任意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+ 厘米的距离下不是问题。