Kinect 从框架数据转换为深度数据,然后再转换回来

Kinect transform from Skeleton data to Depth data and back

本文关键字:数据 转换 然后 回来 框架 Kinect 深度      更新时间:2023-10-16

我正在试验 kinect API,我正在尝试(和失败)实现以下目标:

首先,我从 Kinect 获取骨架数据,并计算用户右手与 Kinect 的距离

mRightHandPosition = skeletonFrame.SkeletonData[i].SkeletonPositions[NUI_SKELETON_POSITION_HAND_RIGHT];    
distance = sqrt(pow(mRightHandPosition.x, 2) + pow(mRightHandPosition.y, 2) + pow(mRightHandPosition.z, 2));

我将右手的骨架数据转换为深度数据,以获得手在(深度/颜色)图像中的位置。

FLOAT curRightX = 0, curRightY = 0;
Vector4 pixelInSkeletonSpace;
NuiTransformSkeletonToDepthImage(mRightHandPosition, &curRightX, &curRightY, cDepthResolution);

获得手的像素位置后,我想将该像素转换回骨架数据,并再次计算该像素(手)中的对象与 Kinect 的距离。我认为这样做应该给我与以前大致相同的距离(当然有一些小误差),但事实并非如此。这是我的工作:

//the position of the depth pixel in the mLockedRect.pBits array 
//i have set the depth sensor resolution to 320x240
int pixelPosition = 2 * ((int)curRightX + (int)curRightY * 320);
USHORT p;
//convert the two consecutive bytes to USHORT
p = (((unsigned short)mLockedRect.pBits[pixelPosition]) << 8) | mLockedRect.pBits[pixelPosition + 1];
//get the pixel in skeleton space
pixelInSkeletonSpace = NuiTransformDepthImageToSkeleton(LONG(curRightX), LONG(curRightY), p, cDepthResolution);
//calculate again the distance (which turns out completely wrong)
distance = sqrt(pow(pixelInSkeletonSpace.x, 2) + pow(pixelInSkeletonSpace.y, 2) + pow(pixelInSkeletonSpace.z, 2));

我错过了一些明显的东西吗? 提前致谢

经过大量搜索,我发现了问题所在。这是其他任何试图做类似事情的人的解决方案

首先为了保存深度数据,最好的方法(我发现)如下

在 processDepth() 函数中:

bghr = m_pBackgroundRemovalStream->ProcessDepth(m_depthWidth * m_depthHeight * cBytesPerPixel, LockedRect.pBits, depthTimeStamp);
const NUI_DEPTH_IMAGE_PIXEL* pDepth = reinterpret_cast<const NUI_DEPTH_IMAGE_PIXEL*>(LockedRect.pBits);
memcpy(mLockedBits, pDepth, m_depthWidth * m_depthHeight * sizeof(NUI_DEPTH_IMAGE_PIXEL));

在 ComposeImage() 函数(或要使用深度数据的任何函数)中:

//transform skeleton data point to depth data
NuiTransformSkeletonToDepthImage(mRightHandPosition, &curRightX, &curRightY, cDepthResolution);
//calculate position of pixel in array
int pixelPosition = (int)curRightX + ((int)curRightY * m_depthWidth);
//get the depth value of the pixel
const USHORT depth = mLockedBits[pixelPosition].depth;
//create a new point in skeleton space using the data we got from the previous transformation
pixelInSkeletonSpace = NuiTransformDepthImageToSkeleton(LONG(curRightX), LONG(curRightY), depth << 3, cDepthResolution);
//calculate estimated distance of right hand from the kinect sensor using our recreated data
FLOAT estimated_distance = sqrt(pow(pixelInSkeletonSpace.x, 2) + pow(pixelInSkeletonSpace.y, 2) + pow(pixelInSkeletonSpace.z, 2));
//calculate the distance of the right hand from the kinect sensor using the skeleton data that we got straight from the sensor
FLOAT actual_distance = sqrt(pow(mRightHandPosition.x, 2) + pow(mRightHandPosition.y, 2) + pow(mRightHandPosition.z, 2));

现在,estimated_distance和actual_distance应该具有大致相同的值,但方差很小。