程序来源 网络,待调试,仅供参考
#include<k4a/k4a.h>
#include<k4abt.h>
#include<iostream>
#define Max_bodies 5
using namespace std;
int main()
{
int device_amount;//设备数目
k4a_device_t device;//设备
k4a_result_t device_result;//设备打开结果
k4a_device_configuration_t deviceConfig;//设备参数
k4a_capture_t sensor_capture;//计录设备的捕获信息
k4a_wait_result_t get_capture_result;//计录各个设备的捕获是否成功
k4a_image_t rgbImage = NULL;//记录从捕获得到的rgb图
k4a_image_t depthImage = NULL;//记录从捕获得到的depth图
k4a_calibration_t sensor_calibration;//校准信息
k4abt_tracker_t tracker;//创建人体跟踪器
k4abt_tracker_configuration_t tracker_config; //人体跟踪器参数
int64_t get_colorImage_time;//时间戳的记录
k4a_wait_result_t queue_capture_result;//计录设备的人体捕获入队是否返回值
k4a_wait_result_t pop_frame_result;//计录设备的人体捕获出队是否返回值
k4abt_frame_t body_frame;//有人图像的序列
k4abt_skeleton_t skeleton[Max_bodies];//人体关节信息
device_amount = k4a_device_get_installed_count();
cout << "发现"<< device_amount <<"台设备!" << endl;
if (device_amount == 0) return 1;
else if (device_amount > 1) return 1;
//打开设备
device_result = k4a_device_open(0, &device);
cout << device_result << endl;
cout << "打开设备成功" << endl;
// 设置参数信息
deviceConfig = K4A_DEVICE_CONFIG_INIT_DISABLE_ALL;
deviceConfig.depth_mode = K4A_DEPTH_MODE_NFOV_2X2BINNED;
deviceConfig.color_resolution = K4A_COLOR_RESOLUTION_720P;
deviceConfig.camera_fps = K4A_FRAMES_PER_SECOND_30;
deviceConfig.color_format = K4A_IMAGE_FORMAT_COLOR_BGRA32;
cout << "设置参数信息成功" << endl;
//打开相机
k4a_device_start_cameras(device, &deviceConfig);
cout << "打开相机成功" << endl;
//查询传感器校准
k4a_device_get_calibration(device, deviceConfig.depth_mode, deviceConfig.color_resolution, &sensor_calibration);
cout << "查询传感器校准成功" << endl;
//创建人体跟踪器
tracker_config = K4ABT_TRACKER_CONFIG_DEFAULT;
k4abt_tracker_create(&sensor_calibration, tracker_config, &tracker);
if (get_tracker_result == K4A_RESULT_SUCCEEDED) cout << "创建人体跟踪器成功" << endl;
else { cout << "创建人体跟踪器失败" << endl; return 1; }
while (true)
{
//从设备中获取捕获
get_capture_result = k4a_device_get_capture(device, &sensor_capture, K4A_WAIT_INFINITE);
if (get_capture_result == K4A_WAIT_RESULT_SUCCEEDED)
{
//从捕获中获取rgb图像
rgbImage = k4a_capture_get_color_image(sensor_capture);
//从捕获中获取深度图像
depthImage = k4a_capture_get_depth_image(sensor_capture);
//获取时间戳
get_colorImage_time = k4a_image_get_device_timestamp_usec(rgbImage);
cout << "时间戳是: " << get_colorImage_time << endl;
//获取人体跟踪器入队返回结果
queue_capture_result = k4abt_tracker_enqueue_capture(tracker, sensor_capture, K4A_WAIT_INFINITE);
//释放捕获空间
k4a_capture_release(sensor_capture);
cout << "释放捕获成功" << endl;
if (queue_capture_result == K4A_WAIT_RESULT_SUCCEEDED)
{
//获取人体跟踪器出队返回结果
pop_frame_result = k4abt_tracker_pop_result(tracker, &body_frame, K4A_WAIT_INFINITE);
if (pop_frame_result == K4A_WAIT_RESULT_SUCCEEDED)
{
//从序列中得到观察人的数量
int num_bodies = k4abt_frame_get_num_bodies(body_frame);
cout << "观察到有 " << num_bodies << " 个人!" << endl;
if (num_bodies > 5) { cout << "超过5人!"; return 1; }
for (int m = 0; m < num_bodies; m++)
{
int result;
//从图像序列中获取关节信息
k4abt_frame_get_body_skeleton(body_frame, m, &skeleton[m]);
//cout << typeid(skeleton[m].joints->position.v).name() << endl;
}
//释放人像序列
k4abt_frame_release(body_frame);
cout << "释放人像序列成功" << endl;
}
}
}
//释放图像空间
k4a_image_release(rgbImage);
k4a_image_release(depthImage);
}
//注意下面结束的顺序
//关闭人体跟踪器
k4abt_tracker_shutdown(tracker);
//销毁跟踪器
k4abt_tracker_destroy(tracker);
//停止相机
k4a_device_stop_cameras(device);
///关闭设备
k4a_device_close(device);
cout << "关闭设备成功" << endl;
}