AcquireLatestFrame始终返回E_PENDING [英] AcquireLatestFrame always returns E_PENDING

查看:196
本文介绍了AcquireLatestFrame始终返回E_PENDING的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在从Kinect获取数据时遇到问题。示例程序(特别是DepthBasics-D2D)正常运行。其他一些人报告说AcquireLatestFrame()在一帧或几帧之后返回E_PENDING,但我的实现总是返回E_PENDING
而我无法读取深度图。谁能发现我的问题?谢谢。

I'm having problems getting data off of my Kinect. The sample programs (notably DepthBasics-D2D) run properly. Some others have reported that AcquireLatestFrame() returns E_PENDING after either one or a few frames, but my implementation always returns E_PENDING and I cannot read the depth map. Can anyone spot my problem? Thanks.

#include <iostream>

#include <Windows.h>
#include <Kinect.h>

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>

#include <cstdlib>

using namespace std;
using namespace cv;

template<class Interface>
inline void SafeRelease(Interface *& pInterfaceToRelease)
{
	if (pInterfaceToRelease != nullptr) {
		pInterfaceToRelease->Release();
		pInterfaceToRelease = nullptr;
	}

}

int main(int argc, char** argv) {
	IKinectSensor* pSensor;
	IDepthFrameSource* pSource;
	IDepthFrameReader* pReader;
	IFrameDescription* pDescription;

	HRESULT hResult = S_OK;

	hResult = GetDefaultKinectSensor(&pSensor);
	if (FAILED(hResult)) {
		std::cerr << "ERROR: GetDefaultKinectSensor" << std::endl;
		return -1;
	}

	hResult = pSensor->Open();
	if (FAILED(hResult)) {
		std::cerr << "ERROR: IKinectSensor::Open()" << std::endl;
		return -1;
	}

	hResult = pSensor->get_DepthFrameSource(&pSource);
	if (FAILED(hResult)) {
		std::cerr << "Error: IKinectSensor::get_FrameSource()" << std::endl;
		return -1;
	}

	hResult = pSource->OpenReader(&pReader);
	if (FAILED(hResult)) {
		std::cerr << "Error: IFrameSource::OpenReader()" << std::endl;
		return -1;
	}

	hResult = pSource->get_FrameDescription(&pDescription);
	if (FAILED(hResult)) {
		std::cerr << "Error: IFrameSource::get_FrameDescription" << std::endl;
		return -1;
	}

	int i = 0;

	int width = 0;
	int height = 0;

	pDescription->get_Width(&width);
	pDescription->get_Height(&height);

	unsigned int bufferSize = width * height * sizeof(unsigned short);
	unsigned short* pBuffer = nullptr;

	while (i < 100) {
		if (!pReader)
		{
			std::cerr << "Error: Reader not open" << std::endl;
			return 0;
		}

		IDepthFrame* pFrame = nullptr;
		hResult = pReader->AcquireLatestFrame(&pFrame);
		if (SUCCEEDED(hResult)) {

			hResult = pFrame->AccessUnderlyingBuffer(&bufferSize, &pBuffer);
			if (SUCCEEDED(hResult)) {
				std::cout << "Frame: " << i;
			}
		}

		i++;
		SafeRelease(pFrame);
	}

	SafeRelease(pSource);
	SafeRelease(pReader);
	if (pSensor) {
		pSensor->Close();
	}
	SafeRelease(pSensor);

	delete[] pBuffer;

	return 0;
}

推荐答案

做的样品工作?您可能需要屈服以确保有时间到达帧,因此在循环中添加Sleep(1)。
do the samples work? You may need to yield to ensure there is time for a frame to arrives so add a Sleep(1) in the loop.


这篇关于AcquireLatestFrame始终返回E_PENDING的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆