프로그래밍/C,C++

Kinect sdk 이미지 얻기

chance 2017. 10. 26. 23:49

#include <Kinect.h>



IKinectSensor* pSensor;

GetDefaultKinectSensor(&pSensor);

pSensor->Open();




// Source

IColorFrameSource* pColorSource;

pSensor->get_ColorFrameSource(&pColorSource);


IDepthFrameSource* pDepthSource;

pSensor->get_DepthFrameSource(&pDepthSource);


IInfraredFrameSource* pInfraredSource;

pSensor->getInfraredFrameSource(&pInfraredSource);




// Reader

IColorFrameReader* pColorReader;

pColorSource->OpenReader(&pColorReader);


IDepthFrameReader* pDepthReader;

pDepthSource->OpenReader(&pDepthReader);


IInfraredFrameReader* pInfraredReader;

pInfraredSource->OpenReader(&pInfraredReader);






// Description

color : 1920*1080*4

depth: 512*424*2


IFrameDescription* colorFrameDesc;

colorFrameSource->CreateFrameDescription( ColorImageFormat::ColorImageFormat_Bgra,&colorFrameDesc);

colorFrameDesc->get_Width(&width);

colorFrameDesc->get_Height(&height);

colorFrameDesc->get_BytesPerPixel( &bytesPerPixel);

int colorSize = width*height*bytesPerPixel;




Mat colorBufferMat(height, width, CV_8UC4);

Mat colorMat(height/2, width/2, CV_8UC4);



int depthSize = 512*424*2;


Mat depthBufferMat( 512, 424, CV_16UC1);

Mat depthMat( 512,424, CV_8UC1);



// Frame


* 호출 즉시 프레임 데이터가 입력되지 않으므로 대기하는 루틴 필요 *


IColorFrame* colorFrame;

pColorFrameReader->AcquireLastestFrame( &colorFrame );



IDepthFrame* depthFrame;

pDepthFrameReader->AcquireLatestFrame( &depthFrame);



IInfraredFrame* infraredFrame;

pInfraredFrameReader->AcquireLastestFrame( &infraredFrame);




// convert

colorFrame->CopyConvertedFrameDataToArray( 

colorSize , 

reinterpret_cast<BYTE*>(colorBufferMat.data), ColorImageFormat_Bgra);



// depth와 ir프레임은 16bit 값을 가지고 있으므로, 

depthFrame->AccessUnderlyingBuffer(

&depthSize,

reinterpret_cast<UINT16**>(&depthBufferMat.data) );


depthBufferMat.convertTo( depthMat, CV_8U, -255>>8, 255.0f); // 색상 반전시키려면 세번째 파라미터를 양수로




// release

if( pSensor != nullptr )

{

pSensor->Close();

}


SAFE_RELEASE(colorFrame);

SAFE_RELEASE(colorFrameDesc);

.

.

SAFE_RELEASE(pSensor);