gpt4 book ai didi

c++ - 从kinect v2获取颜色帧

转载 作者:搜寻专家 更新时间:2023-10-31 02:16:46 24 4
gpt4 key购买 nike

您好,我尝试从 kinect 获取数据并通过 OpenCV 显示。Kinect 正在启动,但程序无法获取图像格式。我遗漏了什么..请解释。

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



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

using namespace cv;
using namespace std;


int main() {


IColorFrameReader* m_pColorFrameReader = nullptr;
RGBQUAD* m_pColorRGBX = nullptr;
const int cColorWidth = 1920;
const int cColorHeight = 1080;
IKinectSensor * m_pKinectSensor = nullptr;
HRESULT hr;

hr = GetDefaultKinectSensor(&m_pKinectSensor);

if (FAILED(hr))
{
return hr;
}

if (m_pKinectSensor)
{
// Initialize the Kinect and get the color reader
IColorFrameSource* pColorFrameSource = NULL;

hr = m_pKinectSensor->Open();

if (SUCCEEDED(hr))
{
hr = m_pKinectSensor->get_ColorFrameSource(&pColorFrameSource);
}

if (SUCCEEDED(hr))
{
hr = pColorFrameSource->OpenReader(&m_pColorFrameReader);

if (SUCCEEDED(hr)) {
cout << "Got Color frame reader \n";

}
}

//SafeRelease(pColorFrameSource);
}

if (!m_pKinectSensor || FAILED(hr))
{
cout << "Major Eror";

}

Sleep(6000);
///////////////////////////////

IColorFrame* pColorFrame;

hr = m_pColorFrameReader->AcquireLatestFrame(&pColorFrame);

if (SUCCEEDED(hr))
{
cout << "Got color frame: \n";
INT64 nTime = 0;
IFrameDescription* pFrameDescription = NULL;
int nWidth = 0;
int nHeight = 0;
ColorImageFormat imageFormat = ColorImageFormat_None;
UINT nBufferSize = 0;
RGBQUAD *pBuffer = NULL;

hr = pColorFrame->get_RelativeTime(&nTime);

if (SUCCEEDED(hr))
{
hr = pColorFrame->get_FrameDescription(&pFrameDescription);
cout << "Frame Desc \n";
}

if (SUCCEEDED(hr))
{
hr = pFrameDescription->get_Width(&nWidth);
cout << "Frame width \n";
}

if (SUCCEEDED(hr))
{
hr = pFrameDescription->get_Height(&nHeight);
cout << "Frame Height \n";
}

if (SUCCEEDED(hr))
{
hr = pColorFrame->get_RawColorImageFormat(&imageFormat);
cout << "Frame Format \n";
}

if (SUCCEEDED(hr))
{
if (imageFormat == ColorImageFormat_Bgra)
{
hr = pColorFrame->AccessRawUnderlyingBuffer(&nBufferSize, reinterpret_cast<BYTE**>(&pBuffer));
cout << "Frame color Format 1 \n";
}
else if (m_pColorRGBX)
{
pBuffer = m_pColorRGBX;
nBufferSize = cColorWidth * cColorHeight * sizeof(RGBQUAD);
hr = pColorFrame->CopyConvertedFrameDataToArray(nBufferSize, reinterpret_cast<BYTE*>(pBuffer), ColorImageFormat_Bgra);
cout << "Frame color Format 2 \n";
}
else
{
hr = E_FAIL;
}
}

if (SUCCEEDED(hr))
{

cout << "Got color data from the kinect\n";

BYTE* bytepImage = reinterpret_cast<BYTE*>(pBuffer));
Mat image = Mat(1080, 1920, CV_8UC4, reinterpret_cast<void*>(bytepImage));
imshow("win",image);


}

//SafeRelease(pFrameDescription);
}

//SafeRelease(pColorFrame);

////////////////////////
cout << "End of";
getchar();

return 0;
}

该程序在获取帧高度之前一直在成功运行。之后它就失败了。

提前致谢..

最佳答案

找到问题原因;

 RGBQUAD*                m_pColorRGBX = nullptr;

应该是

RGBQUAD*                m_pColorRGBX = new RGBQUAD[1920*1080]

谢谢,

关于c++ - 从kinect v2获取颜色帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36592966/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com