0
点赞
收藏
分享

微信扫一扫

Azure kinect (四)人体跟踪器运行错误(持续更新中)

上古神龙 2022-03-30 阅读 43

运行代码时发现这样一个问题,待解决!在这里插入图片描述

#include<k4a/k4a.h>
#include<k4abt.h>

#include<opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

#include<iostream>
#define Max_bodies 5

using namespace std;
using namespace cv;
int main()
{
	int device_amount;//设备数目
	k4a_device_t device = NULL;//设备
	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;//校准信息
	k4a_result_t get_tracker_result;//计录各个设备的捕获是否成功
	k4abt_tracker_t tracker = NULL ;//创建人体跟踪器
	k4abt_tracker_configuration_t tracker_config =  K4ABT_TRACKER_CONFIG_DEFAULT; //人体跟踪器参数

	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];//人体关节信息


	Mat cv_rgbImage_with_alpha ;
	Mat cv_rgbImage_no_alpha;
	Mat cv_depth;
	Mat cv_depth_8U;

	double position[32][3] = {0};

	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);
	if (device_result == K4A_RESULT_SUCCEEDED) cout << "打开设备成功" << endl;
	else { cout << "打开设备失败" << endl; return 1; }

	// 设置参数信息
	deviceConfig = K4A_DEVICE_CONFIG_INIT_DISABLE_ALL;
	deviceConfig.depth_mode = K4A_DEPTH_MODE_WFOV_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;

	//创建人体跟踪器
	get_tracker_result = k4abt_tracker_create(&sensor_calibration, tracker_config, &tracker);
	if (get_tracker_result == K4A_RESULT_SUCCEEDED) cout << "创建人体跟踪器成功" << endl;
	else { cout << "创建人体跟踪器失败" << endl; return 1; }


	while (true)
	{
		if (27 == waitKey(1)) break;

		//从设备中获取捕获
		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);
			cv_rgbImage_with_alpha = Mat(k4a_image_get_height_pixels(rgbImage), k4a_image_get_width_pixels(rgbImage), CV_8UC4, k4a_image_get_buffer(rgbImage));
			if (cv_rgbImage_with_alpha.empty()) continue;
			cvtColor(cv_rgbImage_with_alpha, cv_rgbImage_no_alpha, cv::COLOR_BGRA2BGR);
			cout << "获取rgb图像成功" << endl;


			//从捕获中获取depth图像
			depthImage = k4a_capture_get_depth_image(sensor_capture);
			cv_depth = Mat(k4a_image_get_height_pixels(depthImage), k4a_image_get_width_pixels(depthImage), CV_16U, k4a_image_get_buffer(depthImage), k4a_image_get_stride_bytes(depthImage));
			if (cv_depth.empty()) continue;
			cv_depth.convertTo(cv_depth_8U, CV_8U, 1);
			cout << "获取depth图像成功" << endl;

			//获取时间戳
			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;


			//显示
			namedWindow("rgb", WINDOW_FREERATIO);
			imshow("rgb", cv_rgbImage_no_alpha);
			namedWindow("depth", WINDOW_FREERATIO);
			imshow("depth", cv_depth_8U);
	



			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;

						for (int k = 0; k < 32; k++)
						{
							position[k][0] = skeleton[m].joints[k].position.xyz.x;
							position[k][1] = skeleton[m].joints[k].position.xyz.y;
							position[k][2] = skeleton[m].joints[k].position.xyz.z;
							cout << "赋值成功" << endl;
						}

					}

					//释放人像序列
					k4abt_frame_release(body_frame);
					cout << "释放人像序列成功" << endl;

				}

				
			}

			else if (queue_capture_result == K4A_WAIT_RESULT_TIMEOUT)
			{
				cout << "获取人体跟踪器入队返回结果超时!" << endl;
				break;
			}

			else { cout << "获取人体跟踪器入队返回结果错误!" << endl; break; }

		}
		
		//释放图像空间
		k4a_image_release(rgbImage);
		k4a_image_release(depthImage);
		
	}

	for (int k = 0; k < 32; k++)
	{
		cout << "x : " << position[k][0] << " y : " << position[k][1] << " z: " << position[k][2] << endl;
	}


	//注意下面结束的顺序
	//关闭人体跟踪器
	k4abt_tracker_shutdown(tracker);
	//销毁跟踪器
	k4abt_tracker_destroy(tracker);
	//停止相机
	k4a_device_stop_cameras(device);
	///关闭设备
	k4a_device_close(device);

	cout << "关闭设备成功" << endl;
}

与上一篇代码相比,多了对一些错误的判断,更加严谨,并增加了opencv的显示功能。

举报

相关推荐

0 条评论