OpenCV读视频前首先需要创建VideoCapture对象:
代码语言:javascript复制import cv2
import numpy as np
videoCapture = cv2.VideoCapture("girl.mp4") # 创建VideoCapture对象,用于读取视频
接着获取视频的一些基本信息:
代码语言:javascript复制fps = videoCapture.get(cv2.CAP_PROP_FPS)#获取帧率(每秒播放几张图像)
#获取宽高
size = (int(videoCapture.get(cv2.CAP_PROP_FRAME_WIDTH)),
int(videoCapture.get(cv2.CAP_PROP_FRAME_HEIGHT)))
fNUMS = videoCapture.get(cv2.CAP_PROP_FRAME_COUNT) #获取帧数
若是写视频(无声音),则需要创建VideoWriter对象:
代码语言:javascript复制#创建VideoWriter对象,用于写视频
#videoWriter = cv2.VideoWriter("douyin.avi", cv2.VideoWriter_fourcc('I', '4','2','0'), int(fps), newSize) #此格式文件大
videoWriter = cv2.VideoWriter("douyin.avi", cv2.VideoWriter_fourcc('X', 'V','I','D'), int(fps), newSize)
我们可以自定义视频的显示尺寸:
代码语言:javascript复制newSize = (300,650) # 自己设定的缩放后的图像宽高
最后,循环读入每一帧,显示在屏幕上,并写入进新的视频文件:
代码语言:javascript复制success, frame = videoCapture.read() # 读取一帧,返回是否成功和该帧图像数组
while success and cv2.waitKey(1) == -1: #如果成功读取,且没按下ESC键
frame_resized = cv2.resize(frame, newSize) #图像缩放
mirrorred = frame_resized[:, : :-1,:] # 左右镜像
cv2.imshow("original", frame_resized)
cv2.imshow("mirrorred", mirrorred)
videoWriter.write(mirrorred)#写入一帧
cv2.waitKey(int(1000/int(fps))) #延迟
success, frame = videoCapture.read()#读取下一帧
cv2.waitKey()
cv2.destroyAllWindows()
videoCapture.release()#释放资源
下面是完整的代码,里面额外添加了一些边缘检测,求帧差,镜像,添加文字等功能。(上传的动图像素差是腾讯的锅,压缩得太厉害)
代码语言:javascript复制import cv2
import numpy as np
from edge import strokeEdges #自定义的边缘检测函数(之前有介绍)
videoCapture = cv2.VideoCapture("girl.mp4") # 创建VideoCapture对象,用于读取视频
fps = videoCapture.get(cv2.CAP_PROP_FPS)#获取帧率(每秒播放几张图像)
#获取宽高
size = (int(videoCapture.get(cv2.CAP_PROP_FRAME_WIDTH)),
int(videoCapture.get(cv2.CAP_PROP_FRAME_HEIGHT)))
fNUMS = videoCapture.get(cv2.CAP_PROP_FRAME_COUNT) #获取帧数
newSize = (300,650) # 自己设定的缩放后的图像宽高
#创建VideoWriter对象,用于写视频
#videoWriter = cv2.VideoWriter("douyin.avi", cv2.VideoWriter_fourcc('I', '4','2','0'), int(fps), newSize) #文件大
videoWriter = cv2.VideoWriter("douyin.avi", cv2.VideoWriter_fourcc('X', 'V','I','D'), int(fps), newSize)
success, frame = videoCapture.read() # 读取一帧,返回是否成功和该帧图像数组
lastFrame = np.zeros((newSize[1], newSize[0], 3)) #
while success and cv2.waitKey(1) == -1:
frame_resized = cv2.resize(frame, newSize)
edges = strokeEdges(frame_resized,blurKsize=1, edgeKsize=5) #两个ksize须为奇数
grayEdges = cv2.cvtColor(edges,cv2.COLOR_BGR2GRAY)#转灰度图
delta =np.abs(frame_resized - lastFrame)
grayDelta = np.mean(delta, axis =2)#转灰度图的 numpy方法,三通道取均值
mirrorred = frame_resized[:, : :-1,:] # 左右镜像
#在图像上添加文字(此方法不能添加中文)
#putText(img, text, org, fontFace, fontScale, color[, thickness[, lineType[, bottomLeftOrigin]]])
#mirrorred = cv2.putText(mirrorred,"mirrorred", (20,50), font, 1, (0,255,0), 2, 4, False)
mirrorred = cv2.putText(mirrorred,"mirrorred", (20,50), cv2.FONT_HERSHEY_COMPLEX, 1, (0,255,0), 2, 4, False)
#fliped = frame_resized[ : :-1, :,:] #上下镜像
#mirr_fliped = frame_resized[: : -1, : : -1, :] #左右镜像 上下镜像
#同名窗口会“覆盖”掉
cv2.imshow("original", frame_resized)
cv2.imshow("mirrorred", mirrorred)
#cv2.imshow("flipped", fliped)
#cv2.imshow("mirr_flipped", mirr_fliped)
#cv2.imshow("girlEdge", 255-grayEdges)
#cv2.imshow("delta", grayDelta)
lastFrame = frame_resized
videoWriter.write(frame_resized)
cv2.waitKey(int(1000/int(fps))) #延迟
success, frame = videoCapture.read()
cv2.waitKey()
cv2.destroyAllWindows()
videoCapture.release()