以下 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)。这是正确的吗?