1、方法说明:
复制代码
1
2
3import cv2 as cv video=cv.VideoCapture(filename[, flags])
2、读取视频思路:视频是由每一帧图片组成,读取图片本身就是读取图片;
复制代码
1
2ret,img=video.read()
- ret:<class ‘bool’>是否读取到帧数
- img: <class ‘numpy.ndarray’>图片矩阵
复制代码
1
2
3
4
5
6
7
8
9if video.isOpened(): # ret:<class 'bool'>是否读取到? # img:<class 'numpy.ndarray'>图片矩阵? ret,img=video.read() print(type(ret)) print(type(img)) else: ret=False
通过循环将每一帧图片展示出来
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14while 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循环进行帧数播放,达到读取图片的效果;
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27import cv2 as cv # 读取视频,返回一个视频VideoCapture对象; video=cv.VideoCapture("../sources/cyq.mp4") print(type(video)) if video.isOpened(): # open:<class 'bool'>是否读取到? # frame:<class 'numpy.ndarray'>图片矩阵? 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()
最后
以上就是从容摩托最近收集整理的关于OpenCV使用教程-读取视频VideoCapture的全部内容,更多相关OpenCV使用教程-读取视频VideoCapture内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复