下面代码使用OpenCV-Python版,实现本地/摄像头视频读取、处理和写入磁盘。
# -*- coding: utf-8 -*-
import cv2
# 打开笔记本自带摄像头拍摄视频
# cap = cv2.VideoCapture(0)
# 读取本地视频
videoFilePath = 'D:\Video_example.mp4'
capture = cv2.VideoCapture(videoFilePath)
# 获取视频信息:宽、高、帧率
width = int(capture.get(propId=3))
height = int(capture.get(propId=4))
fps = capture.get(propId=5)
# 定义编码,创建VideoWriter对象
# fourcc = cv2.VideoWriter.fourcc('M', 'J', 'P', 'G')
fourcc = cv2.VideoWriter_fourcc(*'MJPG')
out = cv2.VideoWriter('output.avi', fourcc, fps, (width, height))
while(capture.isOpened()):
# 读取视频的每一帧
ret, frame = capture.read()
if(ret==True):
# 对每一帧进行处理(灰度化 或者 转为 对数极坐标 形式)
#grayFrame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
logpolar_frame = cv2.logPolar(frame, (width/2, height/2), 50, cv2.WARP_FILL_OUTLIERS)
# 显示处理的结果,并将处理后的结果写入
cv2.imshow('logpolar_frame', logpolar_frame)
out.write(logpolar_frame)
if cv2.waitKey(1) == ord('q'):
break
else:
break
# 所有操作完成后,释放内存
capture.release()
out.release()
cv2.destroyAllWindows()

本文介绍如何使用OpenCV-Python进行视频读取、处理及保存。通过实例代码,详细展示了从本地视频文件读取帧,进行灰度化或对数极坐标转换处理,并将处理后的视频保存至磁盘的全过程。
3213

被折叠的 条评论
为什么被折叠?



