简介:
Realsense相机是消费级相机中的经典产品,其D435、D455、D457等产品广泛应用在工业智能硬件产品中,相机取图是视觉感知的第一步,写了一份范例脚本供研究者或工程师方便使用。
依赖:
Realsense SDK、open3d、opencv、numpy
代码:
import os
import cv2
import numpy as np
import open3d as o3d
import pyrealsense2 as rs
def depth_to_pointcloud(depth_image, intrinsic):
# Create Open3D Image from depth map
o3d_depth = o3d.geometry.Image(depth_image)
# Get intrinsic parameters
fx, fy, cx, cy = intrinsic.fx, intrinsic.fy, intrinsic.ppx, intrinsic.ppy
# Create Open3D PinholeCameraIntrinsic object
o3d_intrinsic = o3d.camera.PinholeCameraIntrinsic(width=depth_image.shape[1], height=depth_image.shape[0], fx=fx, fy=fy, cx=cx, cy=cy)
# Create Open3D PointCloud object from depth image and intrinsic parameters
pcd = o3d.geometry.PointCloud.create_from_depth_image(o3d_depth, o3d_intrinsic)
return pcd
def save_pointcloud(pcd, file_name):
o3d.io.write_point_cloud(file_name, pcd)
# 初始化 RealSense 相机
pipeline = rs.pipeline()
config = rs.config()
config.enable_stream(rs.stream.depth, 1280, 720, rs.format.z16, 15)
config.enable_stream(rs.stream.color, 1280, 720, rs.format.bgr8, 15)
profile = pipeline.start(config)
align_to = rs.stream.color
align = rs.align(align_to)
cv2.namedWindow('RealSense', cv2.WINDOW_AUTOSIZE)
depth_profile = rs.video_stream_profile(profile.get_stream(rs.stream.depth))
depth_intrinsics = depth_profile.get_intrinsics()
if not os.path.exists('data_0912'):
os.makedirs('data_0912')
subfolders = ['images', 'depths', 'point_clouds']
for folder in subfolders:
folder_path = os.path.join('data_0912', folder)
if not os.path.exists(folder_path):
os.makedirs(folder_path)
counter = 0
try:
while True:
frames = pipeline.wait_for_frames()
aligned_frames = align.process(frames)
aligned_depth_frame = aligned_frames.get_depth_frame()
if not aligned_depth_frame:
continue
depth_frame = frames.get_depth_frame()
color_frame = aligned_frames.get_color_frame()
if not aligned_depth_frame:
continue
depth_image = np.asanyarray(aligned_depth_frame.get_data())
color_image = np.asanyarray(color_frame.get_data())
color_intrinsics = color_frame.profile.as_video_stream_profile().intrinsics
depth_intrinsics = aligned_depth_frame.profile.as_video_stream_profile().intrinsics
pc = depth_to_pointcloud(depth_image, depth_intrinsics)
cv2.imshow('RealSense', color_image)
depth_colormap = cv2.applyColorMap(cv2.convertScaleAbs(depth_image, alpha=0.008), cv2.COLORMAP_JET)
cv2.imshow('depth_color', depth_colormap)
# 检查是否按下了 't' 键,如果按下了,就保存当前帧的 RGB、深度图和点云
key = cv2.waitKey(1)
if key == ord('t'):
# 保存 RGB 图像
rgb_file_path = os.path.join('data_0912', 'images', 'rgb_20230912_{:04d}.png'.format(counter))
cv2.imwrite(rgb_file_path, color_image)
print('color saved', rgb_file_path)
# 保存深度图像
depth_file_path = os.path.join('data_0912', 'depths', 'depth_20230912_{:04d}.png'.format(counter))
cv2.imwrite(depth_file_path, depth_image)
print('depth saved', depth_file_path)
# 将点云保存为 pcd 文件
pcd_file_path = os.path.join('data_0912', 'point_clouds', 'point_cloud_20230912_{:04d}.pcd'.format(counter))
save_pointcloud(pc, pcd_file_path)
print('pc saved', pcd_file_path)
# 更新计数器
counter += 1
# 检查是否按下了 ESC 键,如果按下了,就退出循环
if key == 27:
cv2.destroyAllWindows()
break
finally:
pipeline.stop()
本文提供了一个使用Realsense相机的D435/455/457系列获取深度图像并转换为Open3D点云的Python脚本,展示了如何使用OpenCV、numpy和RealSenseSDK进行数据处理和保存操作。
&spm=1001.2101.3001.5002&articleId=132835858&d=1&t=3&u=e28ff1d816954fc5b0f149781aa26020)
1495

被折叠的 条评论
为什么被折叠?



