linux中cv2保存视频
时间: 2024-03-03 15:52:18 浏览: 68
在Linux中使用OpenCV库中的cv2模块保存视频可以按照以下步骤进行:
1. 导入cv2模块
```python
import cv2
```
2. 创建VideoWriter对象
```python
fourcc = cv2.VideoWriter_fourcc(*'XVID') # 定义视频编码器
out = cv2.VideoWriter('output.avi', fourcc, 20.0, (640, 480)) # 创建VideoWriter对象
```
其中,第一个参数是输出视频的文件名,第二个参数是视频编码器,第三个参数是帧率,第四个参数是视频的分辨率。
3. 逐帧写入视频
```python
while cap.isOpened():
ret, frame = cap.read()
if ret:
out.write(frame)
else:
break
```
其中,cap是读取视频的VideoCapture对象,frame是每一帧的图像。
4. 释放资源
```python
cap.release()
out.release()
cv2.destroyAllWindows()
```
完整的代码示例:
```python
import cv2
cap = cv2.VideoCapture(0)
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi', fourcc, 20.0, (640, 480))
while cap.isOpened():
ret, frame = cap.read()
if ret:
out.write(frame)
else:
break
cap.release()
out.release()
cv2.destroyAllWindows()
```
阅读全文
相关推荐
















