1、前言
代码中采集的D-RGB图像在官方教程中有一定的介绍,可以参考一下
微软官方教程(C++)
2、代码功能
1、实时显示彩色视频流
2、按“1”退出、按“5”拍照
3、拍照时保存当前视频流的彩色视频帧、深度帧、红外图像、深度图对齐到彩色图(D-RGB图)、点云保存(比较耗算力,已注释)
4、每次按键拍图后,自动图像编号
5、main函数中修改图像保存路径,路径尽量不带中文名
Kinect_folder_path = r"C:\Users\22898\Desktop\1"
3、代码运行环境
首先要安装Azure Kinect相机的SDK
SDK官方链接
python 3.7.12
opencv-python 4.1.2.30
numpy 1.21.6
pyk4a 1.5.0
open3d 0.17.0(不存点云则不用安装)
其余包缺啥pip啥
4、代码源码
import cv2
import pyk4a
from pyk4a import Config, PyK4A
import cv2
import numpy as np
import cv2
import pyk4a
from pyk4a import PyK4A, Config
import open3d as o3d
def save_pointcloud(file_name, points):
pcd = o3d.geometry.PointCloud()
pcd.points = o3d.utility.Vector3dVector(points.astype(np.float32).reshape(-1, 3))
o3d.io.write_point_cloud(file_name, pcd)
def main():
Kinect_folder_path = r"C:\Users\22898\Desktop\1"
k4a = PyK4A(
Config(
color_resolution=pyk4a.ColorResolution.RES_3072P,
depth_mode=pyk4a.DepthMode.WFOV_UNBINNED,
camera_fps =pyk4a.FPS.FPS_15
)
)
k4a.start()
count = 0 # 累加计数
while True:
capture = k4a.get_capture()
cv2.namedWindow('Kinect-color', cv2.WINDOW_AUTOSIZE)
cv2.imshow('Kinect-color', cv2.resize(capture.color, ( 1024, 728)))
key = cv2.waitKey(1)
if key == ord('5'):
count += 1
#-----------Kinect图像保存---------------#
Kinect_rgb_filename = f"{Kinect_folder_path}/rgb-{count}.png"
cv2.imwrite(Kinect_rgb_filename , capture.color)
print(f"基于Kinect的 rgb-{count}.png 已保存!")
Kinect_depth_filename = f"{Kinect_folder_path}/depth-{count}.png"
cv2.imwrite(Kinect_depth_filename , capture.depth)
print(f"基于Kinect的 depth-{count}.png 已保存!")
Kinect_ir_filename = f"{Kinect_folder_path}/ir-{count}.png"
cv2.imwrite(Kinect_ir_filename , capture.ir)
print(f"基于Kinect的 ir-{count}.png 已保存!")
Kinect_morp_filename = f"{Kinect_folder_path}/morp-{count}.png"
cv2.imwrite(Kinect_morp_filename , capture.transformed_depth)
print(f"基于Kinect的 morp-{count}.png 已保存!")
# Kinect_depth_point_cloud_filename = f"{Kinect_folder_path}\\depth_point_cloud-{count}.pcd"
# save_pointcloud(Kinect_depth_point_cloud_filename , capture.depth_point_cloud)
# print(f"基于Kinect的 depth_point_cloud-{count}.pcd 已保存!")
# Kinect_transformed_depth_point_cloud_filename = f"{Kinect_folder_path}\\transformed_depth_point_cloud-{count}.pcd"
# save_pointcloud(Kinect_transformed_depth_point_cloud_filename , capture.transformed_depth_point_cloud)
# print(f"基于Kinect的 transformed_depth_point_cloud-{count}.pcd 已保存!")
if key == ord('1'):
k4a.stop()
break
if __name__ == "__main__":
main()
5、代码运行效果
运行代码后会显示彩色视频流
。
。
。
。
使用ImageJ软件可以进行16位深度图、红外图、D-RGB图的可视化显示,图像保存成功!