这篇文章用来记录Kinect2.0如何生成点云.

以下示例源自Kinect提供的example修改完成,其名称会在小标题下方注解.


首先,要获取点云需要获取图像的深度数据颜色数据.最后再将深度数据与颜色数据转为点云.

1.获取图像深度数据:

  基于Depth Basic -D2D Example修改

HRESULT CMotionRecognition::GetDepthImage(){

    if (!m_pDepthFrameReader)
{
return E_FAIL;
} IDepthFrame * pDepthFrame = nullptr; HRESULT hr = m_pDepthFrameReader->AcquireLatestFrame(&pDepthFrame); if (SUCCEEDED(hr)){
IFrameDescription * pFrameDescription = nullptr;
USHORT nDepthMinReliableDistance = ;
USHORT nDepthMaxDistance = ;
UINT16 *pBuffer = NULL;
UINT nBufferSize = ; if (SUCCEEDED(hr))
{
hr = pDepthFrame->get_FrameDescription(&pFrameDescription);
} if (SUCCEEDED(hr))
{
hr = pFrameDescription->get_Width(&nDepthWidth);
} if (SUCCEEDED(hr))
{
hr = pFrameDescription->get_Height(&nDepthHeight);
} if (SUCCEEDED(hr))
{
hr = pDepthFrame->get_DepthMinReliableDistance(&nDepthMinReliableDistance);
} if (SUCCEEDED(hr))
{
// In order to see the full range of depth (including the less reliable far field depth)
// we are setting nDepthMaxDistance to the extreme potential depth threshold
nDepthMaxDistance = USHRT_MAX;
// Note: If you wish to filter by reliable depth distance, uncomment the following line.
//// hr = pDepthFrame->get_DepthMaxReliableDistance(&nDepthMaxDistance);
} if (SUCCEEDED(hr))
{
hr = pDepthFrame->AccessUnderlyingBuffer(&nBufferSize, &pBuffer);
} if (SUCCEEDED(hr))
{
ConvertMat_depth(pBuffer, nDepthMinReliableDistance, nDepthMaxDistance);
} SafeRelease(pFrameDescription);
} SafeRelease(pDepthFrame); return hr;
}

2.获取图像颜色数据:

  基于Color Basic-D2D Example修改

HRESULT CMotionRecognition::GetColorImage(){

    if (!m_pColorFrameReader)
{
return E_FAIL;
} IColorFrame* pColorFrame = NULL; HRESULT hr = m_pColorFrameReader->AcquireLatestFrame(&pColorFrame); if (SUCCEEDED(hr))
{
INT64 nTime = ;
IFrameDescription* pFrameDescription = NULL;
ColorImageFormat imageFormat = ColorImageFormat_None;
UINT nBufferSize = ;
RGBQUAD *pBuffer = NULL; hr = pColorFrame->get_RelativeTime(&nTime); if (SUCCEEDED(hr))
{
hr = pColorFrame->get_FrameDescription(&pFrameDescription);
} if (SUCCEEDED(hr))
{
hr = pFrameDescription->get_Width(&nColorWidth);
} if (SUCCEEDED(hr))
{
hr = pFrameDescription->get_Height(&nColorHeight);
} if (SUCCEEDED(hr))
{
hr = pColorFrame->get_RawColorImageFormat(&imageFormat);
} if (SUCCEEDED(hr))
{
if (imageFormat == ColorImageFormat_Bgra)
{
hr = pColorFrame->AccessRawUnderlyingBuffer(&nBufferSize, reinterpret_cast<BYTE**>(&pBuffer));
}
else if (m_pColorRGBX)
{
pBuffer = m_pColorRGBX;
nBufferSize = nColorWidth * nColorHeight * sizeof(RGBQUAD);
hr = pColorFrame->CopyConvertedFrameDataToArray(nBufferSize, reinterpret_cast<BYTE*>(pBuffer), ColorImageFormat_Bgra);
}
else
{
hr = E_FAIL;
}
} if (SUCCEEDED(hr))
{
ConvertMat_color(pBuffer, nColorWidth, nColorHeight);
} SafeRelease(pFrameDescription); } SafeRelease(pColorFrame); return hr; }

3.处理图像数据函数

1/2中有一个ConvertMat_*函数,他是负责处理获取的图像颜色数据的,因为点云的转换需要深度数据和图像颜色数据,注意在这还可以创建OpenCV的Mat.

但这里只给出将获取的数据转存到pDepthBuffer(类中的一个成员)中的案例.

ConvertMat_depth()
void CMotionRecognition::ConvertMat_depth(const UINT16* _pBuffer, USHORT nMinDepth, USHORT nMaxDepth)
{
const UINT16
* pBuffer = _pBuffer,
* pBufferEnd = _pBuffer + (nDepthWidth * nDepthHeight); UINT16 * pDepthBufferTmp = pDepthBuffer; while (pBuffer < pBufferEnd)
{
*pDepthBufferTmp = *pBuffer; ++pDepthBufferTmp;
++pBuffer;
} }

ConvertMat_color()
void CMotionRecognition::ConvertMat_color(const RGBQUAD* _pBuffer, int nWidth, int nHeight)
{
const RGBQUAD
* pBuffer = _pBuffer,
* pBufferEnd = pBuffer + (nWidth * nHeight); RGBQUAD * pBufferTmp = m_pColorRGBX; while (pBuffer < pBufferEnd)
{
*pBufferTmp = *pBuffer;
++pBufferTmp;
++pBuffer;
} }

4.合成为点云:

  基于CoordinateMappingBasics-D2D Example修改

osg::ref_ptr<osg::Node> CMotionRecognition::AssembleAsPointCloud(float _angle, int _axisX, int _axisY, int _axisZ)
{
if (!m_pKinectSensor)
{
return E_FAIL;
}
// osg空间坐标
osg::ref_ptr<osg::Vec3Array> point3dVec = new osg::Vec3Array();
// osg颜色值
osg::ref_ptr<osg::Vec4Array> colorVec = new osg::Vec4Array(); ICoordinateMapper * m_pCoordinateMapper = nullptr; HRESULT hr = m_pKinectSensor->get_CoordinateMapper(&m_pCoordinateMapper); for (size_t y = ; y != nDepthHeight; y++)
{
for (size_t x = ; x != nDepthWidth; x++)
{
DepthSpacePoint depthSpacePoint = { static_cast<float>(x), static_cast<float>(y) };
UINT16 currDepth = pDepthBuffer[y * nDepthWidth + x];

       // Coordinate Mapping Depth to Color Space
ColorSpacePoint colorSpacePoint = { 0.0f, 0.0f };
m_pCoordinateMapper->MapDepthPointToColorSpace(depthSpacePoint, currDepth, &colorSpacePoint);
int colorX = static_cast<int>(std::floor(colorSpacePoint.X + 0.5f)),
colorY = static_cast<int>(std::floor(colorSpacePoint.Y + 0.5f));
if (( <= colorX) && (colorX < nColorWidth) && ( <= colorY) && (colorY < nColorHeight))
{
RGBQUAD color = m_pColorRGBX[colorY * nColorWidth + colorX];
colorVec->push_back(osg::Vec4f((float)color.rgbBlue / , (float)color.rgbGreen / , (float)color.rgbRed / , ));
}

       // Coordinate Mapping Depth to Camera Space
CameraSpacePoint cameraSpacePoint = { 0.0f, 0.0f, 0.0f };
m_pCoordinateMapper->MapDepthPointToCameraSpace(depthSpacePoint, currDepth, &cameraSpacePoint);
if (( <= colorX) && (colorX < nColorWidth) && ( <= colorY) && (colorY < nColorHeight))
{
point3dVec->push_back(osg::Vec3(cameraSpacePoint.X, cameraSpacePoint.Y, cameraSpacePoint.Z));
}
}
} // 叶节点
osg::ref_ptr<osg::Geode> geode = new osg::Geode(); // 用来存储几何数据信息 构造图像 保存了顶点数组数据的渲染指令
osg::ref_ptr<osg::Geometry> geom = new osg::Geometry();
  
geom->setVertexArray(point3dVec.get()); geom->setColorArray(colorVec.get());
// 每一个颜色对应着一个顶点
geom->setColorBinding(osg::Geometry::BIND_PER_VERTEX);
// 指定数据绘制的方式
geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::POINTS, , point3dVec->size()));
// 加载到Geode中
geode->addDrawable(geom.get());

  return geode;
}

下面是使用GLUT显示的结果:

可以看到帧率只有2.1左右,在后期要是在需要做处理的话则要更小了.

若谁还有更好的办法生成点云的话,欢迎留言 : )

使用Kinect2.0获取点云以在GLUT中显示的更多相关文章

  1. Kinect2.0获取数据

    最近事情真是多,今天抽空研究一下Kinec2.0的数据获取! 系统要求 https://developer.microsoft.com/en-us/windows/kinect/hardware-se ...

  2. Kinect2.0获取关节姿态(Joint Orientation)

    Bones Hierarchy 骨骼层次结构从SpineBase作为根节点开始,一直延伸到肢体末端(头.指尖.脚): 层级结构如下图所示: 通过IBody::GetJointOrientations函 ...

  3. 38)PHP,获取数据库数据并在html中显示(晋级5)

    还有一个加了单例模式的,在第52个. 首先是我的文件关系: 我的主php文件是index.php,我的配置文件php是BBB.php  我的数据库操作文件是  b.php    我的html文件是lo ...

  4. 37)PHP,获取数据库数据并在html中显示(晋级4)

    我的php文件和html文件的位置关系: 然后我的主php文件是b.php,我的那个配置文件是BBB.php,我的html文件是login.html 然后我的b.php代码展示: <?php c ...

  5. 36)PHP,获取数据库数据并在html中显示(晋级3)

    首先展示我的html代码和php文件的位置关系: 然后我的php文件: <?php class db { public $host ;//= "localhost";//定义 ...

  6. 37)PHP,获取数据库值并在html中显示(晋级2)

    下面的是上一个的改进版,我知道为啥我的那个有问题了,因为我的__construct()这个函数的里面的那个变量名字搞错了,哎,这是经常犯得毛病,傻了吧唧,气死我了. 之前的那个变量的代码样子: cla ...

  7. Kinect2.0点云数据获取

    接上一篇:Kinect2.0获取数据 http://blog.csdn.net/jiaojialulu/article/details/53087988 博主好细心,代码基本上帖过来就可以用,注释掉的 ...

  8. C#微信公众号开发-高级接口-之网页授权oauth2.0获取用户基本信息(二)

    C#微信公众号开发之网页授权oauth2.0获取用户基本信息(一) 中讲解了如果通过微信授权2.0snsapi_base获取已经关注用户的基本信息,然而很多情况下我们经常需要获取非关注用户的信息,方法 ...

  9. 使用Kinect2.0控制VREP中的虚拟模型

    VREP中直接设置物体姿态的函数有3个: simSetObjectOrientation:通过欧拉角设置姿态 simSetObjectQuaternion:通过四元数设置姿态 simSetObject ...

随机推荐

  1. oc-数据模型的建立

    @{@"name":@"David Beckham",@"age":@38,@"gender":@"男&quo ...

  2. 10socket编程

    这一节主要关注的还是粘包问题,我们利用recv实现一个recv_peek函数,它的目的是偷窥目的,它是利用recv的一个msg_peek参数与read的区别,read读取后 会擦除缓冲区的内容,而re ...

  3. nova分析(2)—— nova-all

    nova-all是一个用来启动所有nova服务的辅助脚本,注意只是启动所有服务,不包括停止和重启等功能. nova-all的入口在 nova.cmd.all:main ,脚本也比较简单,这儿就贴下代码 ...

  4. LintCode "Heapify"

    My first try was, using partial sort to figure out numbers layer by layer in the heap.. it only fail ...

  5. Linux删除包含特殊符号文件名的文件

    今天发现机器上有一文件名为 ~~test 的文件名,欲删除之 ,报错查了下, 发现如下解决方法 假设Linux系统中有一个文件名叫“-test”.如果用户想删除它,按照一般的删除方法在命令行中输入“r ...

  6. windows mobile 共享PC网络(win7)

    win7系统安装windows mobile,将设备插入底座后,设备并不能直接共享pc的网络直接上网.原来,当插入底座后,需要打开mobile设备中心,切换一下连接网络,或者打开此窗体后确定一下,即可 ...

  7. activiti自定义流程之整合(四):整合自定义表单部署流程定义

    综合前几篇博文内容,我想在整合这一部分中应该会有很多模块会跳过不讲,就如自定义表单的表单列表那一块,因为这些模块在整合的过程中都几乎没有什么改动,再多讲也是重复无用功. 正因为如此,在创建了流程模型之 ...

  8. 黄聪:如何阻止iframe里引用的网页自动跳转

    今天做了个网页,要在网页里设置一个iframe,然后套用其他的网站.使用http://luanqi-cat.blogbus.com 这个网址的时候,出现了莫名其妙的问题,我的网页居然会强制自动跳转到这 ...

  9. MongoDB 3.0.6的主,从,仲裁节点搭建

    在MongoDB所在路径创建log和data目录mkdir logmkdir data 在data目录下 创建master.slaver.arbiter路径 mkdir master mkdir sl ...

  10. IntelliJ IDEA 注册码

    IntelliJ IDEA 注册码 *.lanyus.com及*.qinxi1992.cn下的全部授权服务器已遭JetBrains封杀 请搭建自己的IntelliJ IDEA授权服务器,教程在http ...