kinect 学习笔记一
颜色深度图像的显示:初始化、绑定流、提取流。1、提取颜色数据:
#include <iostream>
#include "Windows.h"
#include "MSR_NuiApi.h"
#include "cv.h"
#include "highgui.h"
using namespace std;
int main(int argc,char * argv[])
{
IplImage *colorImage=NULL;
colorImage = cvCreateImage(cvSize(640, 480), 8, 3);
//初始化NUI
HRESULT hr = NuiInitialize(NUI_INITIALIZE_FLAG_USES_COLOR);
if( hr != S_OK )
{
cout<<"NuiInitialize failed"<<endl;
return hr;
}
//定义事件句柄
HANDLE h1 = CreateEvent( NULL, TRUE, FALSE, NULL );//控制KINECT是否可以开始读取下一帧数据
HANDLE h2 = NULL;//保存数据流的地址,用以提取数据
hr = NuiImageStreamOpen(NUI_IMAGE_TYPE_COLOR,NUI_IMAGE_RESOLUTION_640x480,0,2,h1,&h2);//打开KINECT设备的彩色图信息通道
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);//得到该帧数据
NuiImageBuffer *pTexture = pImageFrame->pFrameTexture;
KINECT_LOCKED_RECT LockedRect;
pTexture->LockRect(0, &LockedRect, NULL, 0);//提取数据帧到LockedRect,它包括两个数据对象:pitch每行字节数,pBits第一个字节地址
if( LockedRect.Pitch != 0 )
{
cvZero(colorImage);
for (int i=0; i<480; i++)
{
uchar* ptr = (uchar*)(colorImage->imageData+i*colorImage->widthStep);
BYTE * pBuffer = (BYTE*)(LockedRect.pBits)+i*LockedRect.Pitch;//每个字节代表一个颜色信息,直接使用BYTE
for (int j=0; j<640; j++)
{
ptr[3*j] = pBuffer[4*j];//内部数据是4个字节,0-1-2是BGR,第4个现在未使用
ptr[3*j+1] = pBuffer[4*j+1];
ptr[3*j+2] = pBuffer[4*j+2];
}
}
cvShowImage("colorImage", colorImage);//显示图像
}
else
{
cout<<"Buffer length of received texture is bogus\r\n"<<endl;
}
//释放本帧数据,准备迎接下一帧
NuiImageStreamReleaseFrame( h2, pImageFrame );
}
if (cvWaitKey(30) == 27)
break;
}
//关闭NUI链接
NuiShutdown();
return 0;
}
实验结果:
2、提取带有用户ID的深度数据
#include <iostream>
#include "Windows.h"
#include "MSR_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。======这里一直不明白为什么是除以0x0fff,希望了解的同志给解释一下。
RGBQUAD q;
q.rgbRed = q.rgbBlue = q.rgbGreen = 0;
switch( Player )
{
case 0:
q.r
补充:综合编程 , 其他综合 ,