如何使用Python编程来实现在ZED相机上同时捕获左右摄像头的视频流,并将其保存为AVI格式文件?
时间: 2024-10-24 16:11:22 浏览: 16
yolov5直接调用zed相机实现三维测距(python)
在Python中,你可以使用OpenCV库来实现在ZED相机上同时捕获左右摄像头的视频流并保存为AVI格式文件。以下是简单的步骤:
首先,确保已经安装了`opencv-python`, `numpy` 和 `imageio` 等必要的库。如果没有,可以使用pip进行安装:
```bash
pip install opencv-python numpy imageio
```
然后,你需要创建一个Python脚本,导入所需的模块,初始化ZED相机,设置视频编码格式和分辨率,并开始录制:
```python
import cv2
import numpy as np
# 初始化ZED相机
from pyzed.sl import ZEDCamera
zed = ZEDCamera()
# 设置帧率和视频输出大小
frame_rate = 30 # 可调整为所需帧率
resolution_x = 640 # 监视器宽度
resolution_y = 480 # 监视器高度
# 创建左右两个VideoWriter对象,用于保存视频流
left_video_writer = cv2.VideoWriter('left_video.avi', cv2.VideoWriter_fourcc(*'XVID'), frame_rate, (resolution_x, resolution_y))
right_video_writer = cv2.VideoWriter('right_video.avi', cv2.VideoWriter_fourcc(*'XVID'), frame_rate, (resolution_x, resolution_y))
while True:
# 获取一帧
ret_left, left_image = zed.get_frame()
ret_right, right_image = zed.get_depth_frame()
if not ret_left or not ret_right: # 检查是否成功获取
break
# 缩放和处理图像
left_scaled = cv2.resize(left_image, (resolution_x, resolution_y), interpolation=cv2.INTER_LINEAR)
right_scaled = cv2.resize(right_image, (resolution_x, resolution_y), interpolation=cv2.INTER_NEAREST)
# 写入视频流
left_video_writer.write(left_scaled)
right_video_writer.write(right_scaled)
# 显示实时预览
cv2.imshow("Left Camera", left_scaled)
cv2.imshow("Right Camera", right_scaled)
key = cv2.waitKey(1) & 0xFF # 等待按键
# 如果按下 'q' 键,退出循环
if key == ord('q'):
break
# 清理资源
zed.close()
left_video_writer.release()
right_video_writer.release()
cv2.destroyAllWindows()
```
阅读全文