1.RealSense D435摄像头的使用
1.1 使用D435读取摄像头RGB和深度图
import pyrealsense2 as rs
import numpy as np
import cv2
# Configure depth and color streams
pipeline = rs.pipeline()
config = rs.config()
# config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)
# config.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30)
config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)
config.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30)
# Start streaming
pipeline.start(config)
try:
while True:
# Wait for a coherent pair of frames: depth and color
frames = pipeline.wait_for_frames()
# 深度图
depth_frame = frames.get_depth_frame()
# 正常读取的视频流
color_frame = frames.get_color_frame()
if not depth_frame or not color_frame:
continue
# Convert images to numpy arrays
depth_image = np.asanyarray(depth_frame.get_data())
color_image = np.asanyarray(color_frame.get_data())
# print(f"depth_image shape: {depth_image.shape} color_image shape: {color_image.shape}")
print(f"depth_image value: {depth_image}") # 里面0值很多,还有很多1900左右的值 300mm 单位是毫米=30厘米=0.3米
# depth_image shape: (480, 640) color_image shape: (480, 640, 3)
# 深度图是单通道 颜色图是三通道的
# Apply colormap on depth image (image must be converted to 8-bit per pixel first)
# 在深度图像上应用colormap(图像必须先转换为每像素8位)
depth_colormap = cv2.applyColorMap(cv2.convertScaleAbs(depth_image, alpha=0.03), cv2.COLORMAP_JET)
# Stack both images horizontally
images = np.hstack((color_image, depth_colormap))
# Show images
cv2.namedWindow('RealSense', cv2.WINDOW_AUTOSIZE)
cv2.imshow('RealSense', images)
cv2.waitKey(1)
finally:
# Stop streaming
pipeline.stop()