Azure Kinect DK在python上的使用仍然很空白,开篇blog记录一下利用Open3D开发Kinect DK的笔记,内含利用Open3D对Azure Kinect DK相机读取的信息进行点云实时可视化及图像点云按键交互存储。
对官方的代码做些解读:
可视化:azure_kinect_viewer.py
main函数
import argparse
import open3d as o3d
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Azure kinect mkv recorder.')
parser.add_argument('--config', type=str, help='input json kinect config')
parser.add_argument('--list',
action='store_true',
help='list available azure kinect sensors')
parser.add_argument('--device',
type=int,
default=0,
help='input kinect device id')
parser.add_argument('-a',
'--align_depth_to_color',
action='store_true',
help='enable align depth image to color')
args = parser.parse_args()
if args.list:
o3d.io.AzureKinectSensor.list_devices()
exit()
if args.config is not None:
config = o3d.io.read_azure_kinect_sensor_config(args.config)
else:
config = o3d.io.AzureKinectSensorConfig()
device = args.device
if device < 0 or device > 255:
print('Unsupported device id, fall back to 0')
device = 0
v = ViewerWithCallback(config, device, args.align_depth_to_color)
v.run()
代码首先是对argparse 模块的设置,argparse 模块可参考这篇文章:《argparse 模块详解》。
主要有以下四个参数:
- config: 配置文件。自行配置config可参考该链接调整参数:Enumerations
- list: 判断是否有设备连接。
- device: 设备号,默认为0。
- align_depth_to_color: 对齐深度至颜色(默认不对齐,命令行加-a开启),一般深度图像的范围比较大,采用对齐深度至颜色后将RGB图片区域对应的深度图像裁剪对齐。
视频读取显示类ViewerWithCallback(添加了存储RGB图像和深度图像的回调函数)
class ViewerWithCallback:
def __init__(self, config, device, align_depth_to_color):
self.flag_exit = False
self.align_depth_to_color = align_depth_to_color
self.sensor = o3d.io.AzureKinectSensor(config) # 配置相机参数
if not self.sensor.connect(device):
raise RuntimeError('Failed to connect to sensor')
def escape_callback(self, vis):
self.flag_exit = True
return False
def save_callback(self, vis): # 提取RGB图像和深度图像并保存,按's'键保存
num = np.random.randint(100)
o3d.io.write_image('color'+st