Kinect 2未能“获取最佳框架”#39;

时间:2015-07-09 16:09:54

标签: opencv kinect

我在网上找到的样本中使用了以下代码。它似乎拿起了一个框架但随后从未成功地获得了#AcquireLatestFrame'。我和身体阅读器有完全相同的问题。任何人都可以看到可能导致此问题的问题吗?

IKinectSensor* pSensor;
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;
}

// Source
IColorFrameSource* pColorSource;
hResult = pSensor->get_ColorFrameSource(&pColorSource);
if (FAILED(hResult)) {
    std::cerr << "Error : IKinectSensor::get_ColorFrameSource()" << std::endl;
    return -1;
}

// Reader
IColorFrameReader* pColorReader;
hResult = pColorSource->OpenReader(&pColorReader);
if (FAILED(hResult)) {
    std::cerr << "Error : IColorFrameSource::OpenReader()" << std::endl;
    return -1;
}

// Description
IFrameDescription* pDescription;
hResult = pColorSource->get_FrameDescription(&pDescription);
if (FAILED(hResult)) {
    std::cerr << "Error : IColorFrameSource::get_FrameDescription()" << std::endl;
    return -1;
}

int width = 0;
int height = 0;
pDescription->get_Width(&width); // 1920
pDescription->get_Height(&height); // 1080
unsigned int bufferSize = width * height * 4 * sizeof(unsigned char);

cv::Mat bufferMat(height, width, CV_8UC4);
cv::Mat colorMat(height / 2, width / 2, CV_8UC4);
cv::namedWindow("Color");

while (1) {
    // Frame
    IColorFrame* pColorFrame = nullptr;
    hResult = pColorReader->AcquireLatestFrame(&pColorFrame);
    if (SUCCEEDED(hResult)) {
        hResult = pColorFrame->CopyConvertedFrameDataToArray(bufferSize, reinterpret_cast<BYTE*>(bufferMat.data), ColorImageFormat::ColorImageFormat_Bgra);
        if (SUCCEEDED(hResult)) {
            cv::resize(bufferMat, colorMat, cv::Size(), 0.5, 0.5);
        }
    }
    else
    {
        cout << "Can't aquire latest frame.\n";
    }

    cv::imshow("Color", colorMat);

    if (cv::waitKey(30) == VK_ESCAPE) {
        break;
    }
}

if (pSensor) {
    pSensor->Close();
}

cv::destroyAllWindows();

1 个答案:

答案 0 :(得分:4)

我没有发布pColorFrame。这样做解决了这个问题。