#include "stdafx.h"
#include <iostream>
#include "Windows.h"
#include "NuiApi.h"
#include "cv.h"
#include "highgui.h"
using namespace std;
RGBQUAD Nui_ShortToQuad_Depth( USHORT s )//<span style="background-color: rgb(255, 255, 255); ">//该函数我是调用的SDK自带例子的函数。</span>
{
USHORT RealDepth = (s & 0xfff8) >> 3;//提取距离信息
USHORT Player = s & 7 ;//提取ID信息
//16bit的信息,其中最低3位是ID(所捕捉到的人的ID),剩下的13位才是信息
BYTE l = 255 - (BYTE)(256*RealDepth/0x0fff);//因为提取的信息时距离信息,这里归一化为0-255。
RGBQUAD q;
q.rgbRed = q.rgbBlue = q.rgbGreen = 0;
switch( Player )
{
case 0:
q.rgbRed = l / 2;
q.rgbBlue = l / 2;
q.rgbGreen = l / 2;
break;
case 1:
q.rgbRed = l;
break;
case 2:
q.rgbGreen = l;
break;
case 3:
q.rgbRed = l / 4;
q.rgbGreen = l;
q.rgbBlue = l;
break;
case 4:
q.rgbRed = l;
q.rgbGreen = l;
q.rgbBlue = l / 4;
break;
case 5:
q.rgbRed = l;
q.rgbGreen = l / 4;
q.rgbBlue = l;
break;
case 6:
q.rgbRed = l / 2;
q.rgbGreen = l / 2;
q.rgbBlue = l;
break;
case 7:
q.rgbRed = 255 - ( l / 2 );
q.rgbGreen = 255 - ( l / 2 );
q.rgbBlue = 255 - ( l / 2 );
}
return q;
}
int main(int argc,char * argv[])
{
IplImage *depthIndexImage=NULL;
depthIndexImage = cvCreateImage(cvSize(320, 240), 8, 3);
//初始化NUI
HRESULT hr = NuiInitialize(NUI_INITIALIZE_FLAG_USES_DEPTH_AND_PLAYER_INDEX );
if( hr != S_OK )
{
cout<<"NuiInitialize failed"<<endl;
return hr;
}
//打开KINECT设备的彩色图信息通道
HANDLE h1 = CreateEvent( NULL, TRUE, FALSE, NULL );
HANDLE h2 = NULL;
hr = NuiImageStreamOpen(NUI_IMAGE_TYPE_DEPTH_AND_PLAYER_INDEX,NUI_IMAGE_RESOLUTION_320x240,0,2,h1,&h2);//这里根据文档信息,当初始化是NUI_INITIALIZE_FLAG_USES_DEPTH_AND_PLAYER_INDEX时,分辨率只能是320*240或者80*60
if( FAILED( hr ) )
{
cout<<"Could not open color image stream video"<<endl;
NuiShutdown();
return hr;
}
while(1)
{
const NUI_IMAGE_FRAME * pImageFrame = NULL;
if (WaitForSingleObject(h1, INFINITE)==0)
{
NuiImageStreamGetNextFrame(h2, 0, &pImageFrame);
INuiFrameTexture *pTexture = pImageFrame->pFrameTexture;
NUI_LOCKED_RECT LockedRect;
pTexture->LockRect(0, &LockedRect, NULL, 0);
if( LockedRect.Pitch != 0 )
{
cvZero(depthIndexImage);
for (int i=0; i<240; i++)
{
uchar* ptr = (uchar*)(depthIndexImage->imageData+i*depthIndexImage->widthStep);
BYTE * pBuffer = (BYTE *)(LockedRect.pBits)+i*LockedRect.Pitch;
USHORT * pBufferRun = (USHORT*) pBuffer;//注意这里需要转换,因为每个数据是2个字节,存储的同上面的颜色信息不一样,这里是2个字节一个信息,不能再用BYTE,转化为USHORT
for (int j=0; j<320; j++)
{
RGBQUAD rgb = Nui_ShortToQuad_Depth(pBufferRun[j]);//调用函数进行转化
ptr[3*j] = rgb.rgbBlue;
ptr[3*j+1] = rgb.rgbGreen;
ptr[3*j+2] = rgb.rgbRed;
}
}
cvShowImage("depthIndexImage", depthIndexImage);
}
else
{
cout<<"Buffer length of received texture is bogus\r\n"<<endl;
}
//释放本帧数据,准备迎接下一帧
NuiImageStreamReleaseFrame( h2, pImageFrame );
}
if (cvWaitKey(30) == 27)
break;
}
//关闭NUI链接
NuiShutdown();
return 0;
}
Kinect for Windows 实践笔记No.2 深度图像显示
最新推荐文章于 2024-06-13 15:24:07 发布