快速入门:生成第一个 Azure Kinect 应用程序

Azure Kinect DK 入门指南 本快速入门将帮助你迅速上手并运行设备!

如果没有 Azure 订阅,请在开始之前创建一个免费帐户

涵盖以下功能:

先决条件

  1. 设置 Azure Kinect DK 设备
  2. 下载 并安装 Azure Kinect 传感器 SDK。

标头

你只需一个标题,就是 k4a.h。 确保您所选的编译器已设置为使用 SDK 的 lib 文件夹和 include 文件夹。 你还需要链接k4a.libk4a.dll文件。 可能需要参考 将 Azure Kinect 库添加到项目

#include <k4a/k4a.h>

查找 Azure Kinect DK 设备

多个 Azure Kinect DK 设备可以连接到计算机。 首先,我们将首先了解使用 k4a_device_get_installed_count() 函数连接了多少个,或者是否有任何连接。 此函数应立即工作,无需进行任何其他设置。

uint32_t count = k4a_device_get_installed_count();

确定有设备连接到计算机后,可以通过k4a_device_open()来打开它。 可以提供要打开的设备索引,或者只需使用 K4A_DEVICE_DEFAULT 来表示第一个设备。

// Open the first plugged in Kinect device
k4a_device_t device = NULL;
k4a_device_open(K4A_DEVICE_DEFAULT, &device);

与 Azure Kinect 库中的大部分内容一样,打开某些内容时,还应在完成作后将其关闭! 关闭时,请记得给 k4a_device_close() 打电话。

k4a_device_close(device);

设备打开后,我们可以进行测试以确保其正常工作。 因此,让我们读取设备的序列号!

// Get the size of the serial number
size_t serial_size = 0;
k4a_device_get_serialnum(device, NULL, &serial_size);

// Allocate memory for the serial, then acquire it
char *serial = (char*)(malloc(serial_size));
k4a_device_get_serialnum(device, serial, &serial_size);
printf("Opened device: %s\n", serial);
free(serial);

启动相机

打开设备后,需要使用 k4a_device_configuration_t 对象来配置相机。 相机配置具有许多不同的选项。 选择最适合自己情境的设置。

// Configure a stream of 4096x3072 BRGA color data at 15 frames per second
k4a_device_configuration_t config = K4A_DEVICE_CONFIG_INIT_DISABLE_ALL;
config.camera_fps       = K4A_FRAMES_PER_SECOND_15;
config.color_format     = K4A_IMAGE_FORMAT_COLOR_BGRA32;
config.color_resolution = K4A_COLOR_RESOLUTION_3072P;

// Start the camera with the given configuration
k4a_device_start_cameras(device, &config);

// ...Camera capture and application specific code would go here...

// Shut down the camera when finished with application logic
k4a_device_stop_cameras(device);

错误处理

为了简洁明了,我们在一些内联示例中不显示错误处理。 但是,错误处理始终很重要! 许多函数将返回常规成功/失败类型 k4a_result_t,或具有详细信息的更具体的变体,例如 k4a_wait_result_t。 检查每个函数的文档或 IntelliSense,来查看你应该看到哪些错误消息!

可以使用 K4A_SUCCEEDEDK4A_FAILED 宏来检查函数的结果。 因此,与其只打开 Azure Kinect DK 设备,我们可以考虑如下方式来保护函数调用:

// Open the first plugged in Kinect device
k4a_device_t device = NULL;
if ( K4A_FAILED( k4a_device_open(K4A_DEVICE_DEFAULT, &device) ) )
{
    printf("Failed to open k4a device!\n");
    return;
}

完整来源

#pragma comment(lib, "k4a.lib")
#include <k4a/k4a.h>

#include <stdio.h>
#include <stdlib.h>

int main()
{
    uint32_t count = k4a_device_get_installed_count();
    if (count == 0)
    {
        printf("No k4a devices attached!\n");
        return 1;
    }

    // Open the first plugged in Kinect device
    k4a_device_t device = NULL;
    if (K4A_FAILED(k4a_device_open(K4A_DEVICE_DEFAULT, &device)))
    {
        printf("Failed to open k4a device!\n");
        return 1;
    }

    // Get the size of the serial number
    size_t serial_size = 0;
    k4a_device_get_serialnum(device, NULL, &serial_size);

    // Allocate memory for the serial, then acquire it
    char *serial = (char*)(malloc(serial_size));
    k4a_device_get_serialnum(device, serial, &serial_size);
    printf("Opened device: %s\n", serial);
    free(serial);

    // Configure a stream of 4096x3072 BRGA color data at 15 frames per second
    k4a_device_configuration_t config = K4A_DEVICE_CONFIG_INIT_DISABLE_ALL;
    config.camera_fps       = K4A_FRAMES_PER_SECOND_15;
    config.color_format     = K4A_IMAGE_FORMAT_COLOR_BGRA32;
    config.color_resolution = K4A_COLOR_RESOLUTION_3072P;

    // Start the camera with the given configuration
    if (K4A_FAILED(k4a_device_start_cameras(device, &config)))
    {
        printf("Failed to start cameras!\n");
        k4a_device_close(device);
        return 1;
    }

    // Camera capture and application specific code would go here

    // Shut down the camera when finished with application logic
    k4a_device_stop_cameras(device);
    k4a_device_close(device);

    return 0;
}

后续步骤

了解如何使用传感器 SDK 查找和打开 Azure Kinect DK 设备