1、方法说明:
import cv2 as cv
video=cv.VideoCapture(filename[, flags])
2、读取视频思路:视频是由每一帧图片组成,读取图片本身就是读取图片;
ret,img=video.read()
if video.isOpened():
# ret:是否读取到?
# img:图片矩阵?
ret,img=video.read()
print(type(ret))
print(type(img))
else:
ret=False
通过循环将每一帧图片展示出来
while open:
ret,frame=video.read()
if frame is None:
break
if ret==True:
img=cv.cvtColor(frame,cv.IMREAD_COLOR)
cv.imshow("result",img)
if cv.waitKey(1) & 0xFF ==27:#设置等待时间(毫秒级)以及 退出按钮:27表示Esc按键
break
# 视频资源释放
video.release()
# 关闭所有窗口
cv.destroyAllWindows()
3、读取视频案例如下:
读取视频案例,下面案例展示了视频读取思路,通过设置while循环进行帧数播放,达到读取图片的效果;
import cv2 as cv
# 读取视频,返回一个视频VideoCapture对象;
video=cv.VideoCapture("../sources/cyq.mp4")
print(type(video))
if video.isOpened():
# open:是否读取到?
# frame:图片矩阵?
open,frame=video.read()
print(type(open))
print(type(frame))
else:
open=False
while open:
ret,frame=video.read()
if frame is None:
break
if ret==True:
img=cv.cvtColor(frame,cv.IMREAD_COLOR)
cv.imshow("result",img)
if cv.waitKey(1) & 0xFF ==27:
break
video.release()
cv.destroyAllWindows()