2

Hi I tried with getting data from kinect and display through the OpenCV. The Kinect is getting on and the program failed at getting image format.. Am I missing anything .. Please explain.

#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; } 

The program is working success upto getting Frame Height .. After that it is failing.

Thanks in Advance..

1 Answer 1

2

Found the issue reason;

 RGBQUAD* m_pColorRGBX = nullptr; 

should be

RGBQUAD* m_pColorRGBX = new RGBQUAD[1920*1080] 

Thanks,

Sign up to request clarification or add additional context in comments.

2 Comments

please accept your own answer if it answers your question so that the system knows it is answered.
@Micka It seems it takes two days for the permission.. Will do tmrw . Thanks :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.