您好我尝试从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;
}
该程序在获得帧高度方面取得了成功。之后它失败了。
先谢谢..
答案 0 :(得分:2)
找到问题原因;
RGBQUAD* m_pColorRGBX = nullptr;
应该是
RGBQUAD* m_pColorRGBX = new RGBQUAD[1920*1080]
谢谢,