Kinect SDK C++ - 2. Kinect Depth Data

简介: <span style="font-size:12px">Today we will learn how to get depth data from a kinect and what the format of the data is<br><br><br> kinect code<br><br><br> kinect Initialization<br><br><br> To
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.
相关文章
|
2天前
|
存储 缓存 并行计算
C/C++ 数据结构设计与应用(二):自定义数据结构的设计 (Design of Custom Data Structures)
C/C++ 数据结构设计与应用(二):自定义数据结构的设计 (Design of Custom Data Structures)
59 0
|
2天前
|
数据采集 API 开发工具
Baumer工业相机堡盟工业相机如何通过NEOAPI SDK使用Force IP强制修改网口IP功能(C++)
Baumer工业相机堡盟工业相机如何通过NEOAPI SDK使用Force IP强制修改网口IP功能(C++)
33 0
|
2天前
|
安全 API 开发工具
Baumer工业相机堡盟工业相机如何通过NEOAPI SDK使用短曝光功能(C++)
Baumer工业相机堡盟工业相机如何通过NEOAPI SDK使用短曝光功能(C++)
43 0
|
2天前
|
数据采集 API 开发工具
Baumer工业相机堡盟工业相机如何通过NEOAPI SDK设置软件触发模式(C++)
Baumer工业相机堡盟工业相机如何通过NEOAPI SDK设置软件触发模式(C++)
36 0
|
2天前
|
监控 开发工具 C#
Baumer工业相机堡盟工业相机如何通过NEOAPI SDK实现相机掉线自动重连(C++)
Baumer工业相机堡盟工业相机如何通过NEOAPI SDK实现相机掉线自动重连(C++)
34 0
|
2天前
|
编解码 监控 机器人
Baumer工业相机堡盟工业相机如何通过NEOAPI SDK获取相机当前数据吞吐量(C++)
Baumer工业相机堡盟工业相机如何通过NEOAPI SDK获取相机当前数据吞吐量(C++)
33 1
|
2天前
|
存储 数据处理 开发工具
Baumer工业相机堡盟工业相机如何通过NEOAPI SDK实现相机的高速图像保存(C++)
Baumer工业相机堡盟工业相机如何通过NEOAPI SDK实现相机的高速图像保存(C++)
44 0
|
2天前
|
存储 新制造 开发工具
Baumer工业相机堡盟工业相机如何联合NEOAPI SDK和OpenCV实现相机图像转换为Mat图像格式(C++)
Baumer工业相机堡盟工业相机如何联合NEOAPI SDK和OpenCV实现相机图像转换为Mat图像格式(C++)
44 2
|
2天前
|
存储 数据管理 开发工具
Baumer工业相机堡盟工业相机如何通过NEOAPI SDK设置相机本身的数据保存(CustomData)功能(C++)
Baumer工业相机堡盟工业相机如何通过NEOAPI SDK设置相机本身的数据保存(CustomData)功能(C++)
50 0
|
2天前
|
编解码 监控 开发工具
Baumer工业相机堡盟工业相机如何通过NEOAPI SDK使用Binning像素合并功能(C++)
Baumer工业相机堡盟工业相机如何通过NEOAPI SDK使用Binning像素合并功能(C++)
52 0