帮我写一个保存T265双目相机图像的方法
时间: 2024-02-02 12:03:23 浏览: 18
以下是一个基本的保存T265双目相机图像的Python代码示例:
```python
import pyrealsense2 as rs
import numpy as np
import cv2
# 初始化相机
pipeline = rs.pipeline()
config = rs.config()
config.enable_stream(rs.stream.fisheye, 1) # 左侧相机
config.enable_stream(rs.stream.fisheye, 2) # 右侧相机
pipeline.start(config)
# 循环读取图像并保存
try:
while True:
# 等待获取一帧数据
frames = pipeline.wait_for_frames()
left_frame = frames.get_fisheye_frame(1)
right_frame = frames.get_fisheye_frame(2)
# 将图像数据转换为numpy数组
left_image = np.asanyarray(left_frame.get_data())
right_image = np.asanyarray(right_frame.get_data())
# 显示图像
cv2.imshow("Left Image", left_image)
cv2.imshow("Right Image", right_image)
# 保存图像
cv2.imwrite("left_image.png", left_image)
cv2.imwrite("right_image.png", right_image)
# 按下ESC键退出循环
if cv2.waitKey(1) == 27:
break
finally:
# 停止相机并关闭所有窗口
pipeline.stop()
cv2.destroyAllWindows()
```
该代码使用pyrealsense2库初始化T265相机并启用左右两个相机流。在每个循环迭代中,它等待获取一帧数据并将左右两个相机的图像数据转换为numpy数组。然后,它显示这些图像,并将它们保存为PNG文件。最后,它等待用户按下ESC键以退出循环,并停止相机并关闭所有窗口。