c++ - Kinect 2 - AcquireLatestFrame() 大部分时间都失败

标签 c++ kinect frame-rate kinect-sdk kinect-v2

下面给出了连续从 Kinect 2 获取最新帧的 C++ 代码。

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

最佳答案

是的,你是对的。

来源:请参阅此处表格中“深度感应”下的“30hz”:(您可能需要向下滚动一点)

https://developer.microsoft.com/en-us/windows/kinect/hardware

函数的文档说:

Return value

Type: HRESULT

Returns S_OK if successful; otherwise, returns a failure code.

(来源:https://msdn.microsoft.com/en-us/library/microsoft.kinect.kinect.ibodyframereader.acquirelatestframe.aspx)

它大部分时间返回的失败 HRESULT 代码是 E_PENDING。这意味着新框架尚未准备就绪。


回答您的问题:为什么 AcquireLatestFrame 最常返回失败的 HRESULT?

因为不需要多次处理相同的输入数据(您只会通过一遍又一遍地计算相同的结果来浪费 CPU 时间)。

关于c++ - Kinect 2 - AcquireLatestFrame() 大部分时间都失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39321263/

相关文章:

java - 缓存部分 2D 绘图是一种好习惯吗?

python - 在Python中更改视频帧速率(fps)

c++ - 为什么这段代码可以在 Coliru 上编译,但不能在 Xcode 上编译?

c++ - 如果发出 SIGINT 或 SIGSTP,是否调用析构函数?

image-processing - OpenCV与kinect初学者的疑惑

image - Kinect:从 RGB 坐标转换为深度坐标

c++ - 我想在qt中创建一个自定义标题栏

c++ - 配饰设计

kinect - Kinect 编程要求

animation - 游戏中的流畅动画