之前找了很多代码都没法读取出相机的内参和双目图像进行研究,看了很多博客总结了最简单读取所有图像和内参的代码,大家可以自取:
import pyrealsense2 as rs
import numpy as np
import cv2
pipeline = rs.pipeline()
config = rs.config()
config.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 15) #10、15或者30可选,其他分辨率可以参考官方datasheet
config.enable_stream(rs.stream.infrared, 1, 640, 480, rs.format.y8, 15)
config.enable_stream(rs.stream.infrared, 2, 640, 480, rs.format.y8, 15)
config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 15)
profile = pipeline.start(config)
# Getting the depth sensor's depth scale (see rs-align example for explanation)
depth_sensor = profile.get_device().first_depth_sensor()
depth_scale = depth_sensor.get_depth_scale()
clipping_distance_in_meters = 1 #1 meter
clipping_distance = clipping_distance_in_meters / depth_scale
# Create an align object
# rs.align allows us to perform alignment of depth frames to others frames
# The "align_to" is the stream type to which we plan to align depth frames.
align_to = rs.stream.color
align = rs.align(align_to)
try:
while True:
frames = pipeline.wait_for_frames()
# Align the depth frame to color frame
#一般彩色图和深度图需要配准,所以数据从aligned_frames读出来
aligned_frames = align.process(frames)
# Get aligned frames
aligned_depth_frame = aligned_frames.get_depth_frame() # aligned_depth_frame is a 640x480 depth image
if not aligned_depth_frame:
continue
depth_frame = np.asanyarray(aligned_depth_frame.get_data())
depth_colormap = cv2.applyColorMap(cv2.convertScaleAbs(depth_frame, alpha=0.03), cv2.COLORMAP_JET)
cv2.imshow('1 depth', depth_colormap)
# color frames
color_frame = aligned_frames.get_color_frame()
video_profile = rs.video_stream_profile(color_frame.get_profile())
instrinsics = video_profile.get_intrinsics()
fx = instrinsics.fx
fy = instrinsics.fy
cx = instrinsics.ppx
cy = instrinsics.ppy
print("Width:", instrinsics.width)
print("Height:", instrinsics.height)
print("PPX:", instrinsics.ppx)
print("PPY:", instrinsics.ppy)
print("FX:", instrinsics.fx)
print("FY:", instrinsics.fy)
print("Distortion Model:", instrinsics.model)
print("Coefficients:", instrinsics.coeffs)
#如果需要打印其他内参 可以去官方文档查看instrinsics的具体参数,以及其他配置文件
if not color_frame:
continue
color_frame = np.asanyarray(color_frame.get_data())
cv2.imshow('2 color', color_frame)
#读取左右眼图片,可能是为了通信方便的原因,原先的rgb和红外图像都会以灰度图的形式传输
# left frames
left_frame = frames.get_infrared_frame(1)
if not left_frame:
continue
left_frame = np.asanyarray(left_frame.get_data())
cv2.imshow('3 left_frame', left_frame)
# right frames
right_frame = frames.get_infrared_frame(2)
if not right_frame:
continue
right_frame = np.asanyarray(right_frame.get_data())
cv2.imshow('4 right_frame', right_frame)
c = cv2.waitKey(1)
# 如果按下ESC则关闭窗口(ESC的ascii码为27),同时跳出循环
if c == 27:
cv2.destroyAllWindows()
break
finally:
# Stop streaming
pipeline.stop()