给出了以下C++代码,它连续获取Kinect 2的最新帧。
int main()
{
setupKinect();
acquireFrames();
return 0;
}
template<class Interface>
inline static void safeRelease(Interface *&interfaceToRelease)
{
if (interfaceToRelease != nullptr) {
interfaceToRelease->Release();
interfaceToRelease = nullptr;
}
}
void acquireFrames() {
while (true) {
if (bodyFrameReader != nullptr) {
IBodyFrame* bodyFrame = nullptr;
HRESULT hr = bodyFrameReader->AcquireLatestFrame(&bodyFrame);
if (SUCCEEDED(hr)) {
// processing bodyFrame
} else {
// acquiring frame failed
}
safeRelease(bodyFrame);
}
}
}
void setupKinect() {
IKinectSensor * sensor = nullptr;
HRESULT hr = GetDefaultKinectSensor(&sensor);
if (SUCCEEDED(hr)) {
hr = sensor->Open();
if (SUCCEEDED(hr)) {
IBodyFrameSource* bodyFrameSource = nullptr;
hr = sensor->get_BodyFrameSource(&bodyFrameSource);
if (SUCCEEDED(hr)) {
hr = bodyFrameSource->OpenReader(&bodyFrameReader);
}
safeRelease(bodyFrameSource);
}
}
safeRelease(sensor);
}
为什么AcquireLatestFrame
经常返回失败的HRESULT?一些测试表明,该函数每秒成功只有约30次,因此似乎某个帧最多只能被此函数获取或返回一次(Kinect的帧率为30 fps)。这是正确的吗?