怎么在OpenCV中使用VideoCapture类

发布时间:2021-03-20 17:10:35 作者:Leah
来源:亿速云 阅读:218

怎么在OpenCV中使用VideoCapture类?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

VideoCapture()是用于从视频文件、图片序列、摄像头捕获视频的类;

#!/usr/bin/env python
#-*- coding:utf-8 -*-
# @Time : 19-4-21 上午10:31
# @Author : chen

"""
VideoCapture()的使用
"""
import cv2
import argparse
import os
import pdb

ap = argparse.ArgumentParser()
ap.add_argument("-v", "--videoPath", default="./video_1.mp4", help="path to input video")
ap.add_argument("-o", "--outputPath", default="grabImages", help="path to output frames")

args = vars(ap.parse_args())

# 初始化,并读取第一帧
# rval表示是否成功获取帧
# frame是捕获到的图像
vc = cv2.VideoCapture(args["videoPath"])
rval, frame = vc.read()

# 获取视频fps
fps = vc.get(cv2.CAP_PROP_FPS)
# 获取视频总帧数
frame_all = vc.get(cv2.CAP_PROP_FRAME_COUNT)
print("[INFO] 视频FPS: {}".format(fps))
print("[INFO] 视频总帧数: {}".format(frame_all))
print("[INFO] 视频时长: {}s".format(frame_all/fps))

outputPath = os.path.sep.join([args["outputPath"]])
if os.path.exists(outputPath) is False:
 print("[INFO] 创建文件夹,用于保存提取的帧")
 os.mkdir(outputPath)

# 每隔100帧保存一张图片
frame_interval = 100
# 统计当前帧
frame_count = 1
# 保存图片个数
count = 0
while rval:
 rval, frame = vc.read()
 if frame_count % frame_interval == 0:
  filename = os.path.sep.join([outputPath, "test_{}.jpg".format(count)])
  cv2.imwrite(filename, frame)
  count += 1
  print("保存图片:{}".format(filename))
 frame_count += 1

# 关闭视频文件
vc.release()
print("[INFO] 总共保存:{}张图片".format(count))

关于怎么在OpenCV中使用VideoCapture类问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

推荐阅读:
  1. 怎么在OpenCV中使用circle函数在图像中画圆
  2. 怎么在python使用opencv调用模型

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

opencv videocapture

上一篇:如何在java中使用elasticsearch进行分组

下一篇:php中与html标签相关的函数有哪些

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》