Kinect SDK C++ - 2. Kinect Depth Data

Today we will learn how to get depth data from a kinect and what the format of the data is

kinect code

kinect Initialization

To get the depth data from the kinect, simply change the argument to NuiImageStreaOpen().

The First argument is now NUI_IMAGE_TYPE_DEPATH,telling the Kinect that wo now want depath images

instead of RGB iamges.(For clarity we also changed the name of the handle to reflect this)

We also should enable the Near Mode.let the kinect to be more sensitive to closer objects(say from 50cm to

200cm),otherwise,from 80 to 400cm.

To done that,passing flag NUI_IMAGE_FLAG_ENABLE_NEAR_MODE as the third argument

<span style="font-size:12px;"><span style="font-size:10px;">// NEW VARIABLE
HANDLE depthStream;

bool initKinect() {
    // Get a working kinect sensor
    int numSensors;
    if (NuiGetSensorCount(&numSensors) < 0 || numSensors < 1) return false;
    if (NuiCreateSensorByIndex(0, &sensor) < 0) return false;

    // Initialize sensor
    sensor->NuiInitialize(NUI_INITIALIZE_FLAG_USES_DEPTH | NUI_INITIALIZE_FLAG_USES_COLOR);

        // --------------- START CHANGED CODE -----------------
    sensor->NuiImageStreamOpen(
        NUI_IMAGE_TYPE_DEPTH,                     // Depth camera or rgb camera?
        NUI_IMAGE_RESOLUTION_640x480,             // Image resolution
        NUI_IMAGE_STREAM_FLAG_ENABLE_NEAR_MODE,   // Image stream flags, e.g. near mode
        2,      // Number of frames to buffer
        NULL,   // Event handle
        &depthStream);
        // --------------- END CHANGED CODE -----------------
    return sensor;
}</span></span>

For more information about the near mode,please prefer to offficial blog.

getting a depth frame from the kinect

the display of the dapth image from the kinect in grayscale.Each pixel will just be the pixel's distance

from the kinect(in millimeters)mod 256.

note the NuiDepthPixelToDepth fuction,calling this function returns the depth in millimeters at that pixel.

The depth data is 16 bits,so we use a USHORT to read it in.

<span style="font-size:12px;"><span style="font-size:10px;">      const USHORT* curr = (const USHORT*) LockedRect.pBits;
        const USHORT* dataEnd = curr + (width*height);

        while (curr < dataEnd) {
            // Get depth in millimeters
            USHORT depth = NuiDepthPixelToDepth(*curr++);

            // Draw a grayscale image of the depth:
            // B,G,R are all set to depth%256, alpha set to 1.
            for (int i = 0; i < 3; ++i)
                *dest++ = (BYTE) depth%256;
            *dest++ = 0xff;
        }
</span></span>

that's all the Kinect code! The rest is just how to get it to display.

时间: 2024-09-20 05:41:15

Kinect SDK C++ - 2. Kinect Depth Data的相关文章

Kinect SDK v1.7迎来重大更新

http://www.aliyun.com/zixun/aggregation/13717.html">Kinect SDK v1.7迎来重大更新 发表于2013-03-18 14:01| 次阅读| 来源CSDN| 0 条评论| 作者张宁 KinectKinect Fusion微软开发工具SDK开源 摘要:Kinect for Windows SDK 1.7,这是自去年发布Kinect for Windows SDK以来最重要的一次更新.Kinect Interactions手势识别(Pu

Microsoft Kinect SDK Wrapper For Unity Crash Bug Fix

There's a great free Kinect SDK wrapper available for Unity. It's free & open source but there are still a few problems getting it to run with the 1.0 SDK (as opposed to the beta). The first problem is that it is pointing to the wrong dll file. When

Kinect for Windows SDK开发入门(十一)手势识别 下:基本手势识别

上文简要介绍了手势识别的基本概念和手势识别的基本方法,并以八种手势中的挥手(wave)为例讲解了如何使用算法对手势进行识别,本文接上文,继续介绍如何建立一个手部追踪类库,并以此为基础,对剩余7中常用的手势进行识别做一些介绍. 1. 基本的手势追踪 手部追踪在技术上和手势识别不同,但是它和手势识别中用到的一些基本方法是一样的.在开发一个具体的手势控件之前,我们先建立一个可重用的追踪手部运动的类库以方便我们后续开发.这个手部追踪类库包含一个以动态光标显示的可视化反馈机制.手部追踪和手势控件之间的交互

Draw the RGB data from kinect C++ via opengl

In order to improve my English writing skills,I am going to  write the blogs in English form now! -------------------------------------------------------------------------------------------Luxuriant line-----------------------------------------------

Kinect for Windows SDK开发入门(五)景深数据处理 下

1. 简单的景深影像处理 在上篇文章中,我们讨论了如何获取像素点的深度值以及如何根据深度值产生影像.在之前的例子中,我们过滤掉了阈值之外的点.这就是一种简单的图像处理,叫阈值处理.使用的阈值方法虽然有点粗糙,但是有用.更好的方法是利用机器学习来从每一帧影像数据中计算出阈值.Kinect深度值最大为4096mm,0值通常表示深度值不能确定,一般应该将0值过滤掉.微软建议在开发中使用1220mm(4')~3810mm(12.5')范围内的值.在进行其他深度图像处理之前,应该使用阈值方法过滤深度数据至

Kinect for Windows SDK开发入门(二)基础知识 上

上篇文章介绍了Kinect开发的环境配置,这篇文章和下一篇文章将介绍Kinect开发的基本知识,为深入研究Kinect for Windows SDK做好基础. 每一个Kinect应用都有一些基本元素.应用程序必须探测和发现链接到设备上的Kinect传感器.在使用这些传感器之前,必须进行初始化,一旦初始化成功后,就能产生数据,我们的程序就能处理这些数据.最后当应用程序关闭是,必须合理的释放这些传感器. 本文第一部分将会介绍如何探测初始化几释放传感器,这是非常基础的话题,但是对于基于Kinect开

Kinect for Windows SDK 1.8的改进及新特性

今年3月, 微软推出了Kinect for Windows SDK 1.7 更新,包括了手势识别 Kinect Interactions 和实时 3D 建模 Kinect Fusion 两项新技术. 昨天,微软发布了Kinect for Windows SDK的1.8版本,新版本SDK进行了少量更新,但对Kinect Developer Toolkit中的示例进行了大量更新,包括新添加了对HTML5的支持,新的背景移除功能.Kinect Fusion 的色彩捕捉功能. Kinect Fusion

Kinect for Windows SDK开发入门(十八)Kinect Interaction交互控件

今年三月份发布了1.7版本的SDK,这一版本的SDK较前一版本最大的变化是添加了Kinect Interactions 和 Kinect Fusion.Kinect Interactions 提供了一些新的带有姿势识别的控件如 push-to-press 按钮, grip-to-pan 列表控件, 而且支持多用户,同时二个人进行的交互,这些新添加的控件能够非常方便的集成到应用程序中,极大的简化了开发和调试过程. 1.7 SDK中新增的第二个功能是去年在Build 2012大会上提到的Kinect

Kinect for Windows SDK开发入门(十六)面部追踪上

在前面一篇文章中,我们使用Emgu来识别人的脸部,当时的Kinect SDK版本是1.0,五月份发布1.5版本的SDK之后,我们就能够直接使用Kinect实现人脸识别,而不需要借助第三方类库. SDK1.5中新增了人脸识别类库:Microsoft.Kinect.Toolkit.FaceTracking使得在Kinect中进行人脸识别变得简单,该类库的源代码也在Developer Toolkit中.在Developer Toolkit中也自带人脸识别的例子,您也可以打开运行或者查看源代码. 开发入