使用OpenCV在给定时间从视频中提取图像 [英] Extracting image from video at a given time using OpenCV

查看:507
本文介绍了使用OpenCV在给定时间从视频中提取图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的任务是制作一个可以在几秒钟内拍摄视频和时间的实用程序。

My task is to make a utility that can take a video and time in seconds.

该实用程序应使用给定的输入从视频中写出jpeg图像。

The utility should write out jpeg images from the video with the given input.

例如假设视频名称为 abc.mpeg ,并以 20 秒的时间提供给工具。该实用程序应该在20秒后从视频中写出图像。

E.g. let the video name be abc.mpeg and time be supplied to the tool as 20 seconds. The utility should write out image from video @ 20th second.

    # Import the necessary packages
    import argparse
    import cv2

    vidcap = cv2.VideoCapture('Wildlife.mp4')
    success,image = vidcap.read()
    count = 0;
    while success:
      success,image = vidcap.read()
      cv2.imwrite("frame%d.jpg" % count, image)     # save frame as JPEG file
      if cv2.waitKey(10) == 27:                     # exit if Escape is hit
          break
      count += 1

上面的代码给出了整个视频的所有帧,我担心的是如何传递时间并在指定时间获取帧?

The above code gives all frames of the entire video, my concern is how can I pass time and get the frame at the specified time?

推荐答案

为什么不做,@ micka建议了什么?

why don't you just do, what @micka proposed ?

import cv2

vidcap = cv2.VideoCapture('d:/video/keep/Le Sang Des Betes.mp4')
vidcap.set(cv2.CAP_PROP_POS_MSEC,20000)      # just cue to 20 sec. position
success,image = vidcap.read()
if success:
    cv2.imwrite("frame20sec.jpg", image)     # save frame as JPEG file
    cv2.imshow("20sec",image)
    cv2.waitKey()                    

这篇关于使用OpenCV在给定时间从视频中提取图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆