Azure Kinnect DK采集RGB图像和深度图像(Python)
提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录
前言
基于Azure Kinect DK 传感器 SDK 的 Python 3 库的接口函数进行图像采集
一、下载SDK
首先需要在官网下载 Azure Kinect 传感器 SDK
链接: link
二、配置环境
在anaconda3中新建一个环境
1.引入库
主要安装的库有:
numpy、opencv-python、open3d
2. Azure Kinect DK 传感器 SDK 的 Python 3 库
直接从GitHub上git或者下载:pyKinectAzure
然后把下载的库 pyKinectAzure这个文件夹移动到安装环境安装库的目录中
例如我的环境安装库路径:D:\Users\Administrator\anaconda3\envs\kinect\Lib\site-packages
最后,在环境终端进行本地导入。
pip install pykinect_azure
三、同时获取RGB图像和深度图像
这里运行代码后,按 ‘s’ 键开始保存每一帧图像,按 ‘w’ 键停止保存,按 ‘q’ 键退出程序。
import cv2
import pykinect_azure as pykinect
import os
# 初始化 Azure Kinect SDK 库
pykinect.initialize_libraries()
# 配置设备参数
device_config = pykinect.default_configuration
device_config.color_format = pykinect.K4A_IMAGE_FORMAT_COLOR_BGRA32 # RGB 图像格式
device_config.color_resolution = pykinect.K4A_COLOR_RESOLUTION_1536P # 高分辨率 RGB 图像
device_config.depth_mode = pykinect.K4A_DEPTH_MODE_WFOV_2X2BINNED # 深度图模式
# 启动设备
device = pykinect.start_device(config=device_config)
# 状态变量:是否正在保存图像
saving = False
# 创建保存文件夹
output_dir = "saved_frames"
os.makedirs(output_dir, exist_ok=True)
frame_count = 0
print("按 's' 键开始保存每一帧图像,按 'w' 键停止保存,按 'q' 键退出程序。")
while True:
# 获取当前帧数据
capture = device.update()
# 获取 RGB 图像
ret_color, color_image = capture.get_color_image()
# 获取深度图像
ret_depth, depth_image = capture.get_transformed_depth_image()
# 如果未成功获取 RGB 或深度图像,继续下一帧
if not ret_color or not ret_depth:
continue
# 显示 RGB 图像和深度图像
cv2.imshow('RGB Image', color_image)
cv2.imshow('Depth Image', depth_image)
# 保存当前帧图像
if saving:
rgb_filename = os.path.join(output_dir, f"rgb_frame_{frame_count:04d}.png")
depth_filename = os.path.join(output_dir, f"depth_frame_{frame_count:04d}.png")
cv2.imwrite(rgb_filename, color_image)
cv2.imwrite(depth_filename, depth_image)
print(f"已保存帧 {frame_count}: RGB({rgb_filename}) 和 深度({depth_filename})")
frame_count += 1
# 按键操作
key = cv2.waitKey(1)
if key == ord('s'): # 按下 's' 键开始保存
if not saving:
saving = True
frame_count = 0 # 重置帧计数
print("开始保存帧数据...")
elif key == ord('w'): # 按下 'w' 键停止保存
if saving:
saving = False
print("停止保存帧数据。")
elif key == ord('q'): # 按下 'q' 键退出
print("退出程序。")
break
# 释放设备和窗口资源
device.stop_device()
cv2.destroyAllWindows()
采集结果如图所示:
RGB图像 2048*1536:
16位深度图 :
ImageJ查看深度图结果:
这里的RGB图和深度图应该是对齐好的(有待验证!!)
get_transformed_depth_image()这个函数应该是获取转换后(深度图对齐到RGB图)的深度图
总结
第一次发稿写得有点乱
参考了几位大佬的文章:
Azure Kinect 的python接口实现获取图像
Azure Kinect相机图像采集代码(纯python)
作者:weixin_38327555