如何使用OpenCV Python从视频获取图像 [英] How to get image from video using opencv python

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

问题描述

我想从视频中获取图像并将其以'.jpeg'或'.png'格式存储,请帮助我如何使用opencv做到这一点.我的代码是

I want to get image from video and store it in '.jpeg' or '.png' format please help me how to do this with opencv My code is

import cv2
vidcap = cv2.VideoCapture('video1.mp4')
success,image = vidcap.read()
count = 0;
print "I am in success"
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

在这里,我试图逐帧从视频中获取图像并将其另存为frame1,frame2,frame3

Here i am trying to get the image from video frame by frame and save it as frame1,frame2,frame3

推荐答案

这是我用来读取视频并保存帧的方法:

This is what I use to read in a video and save off the frames:

import cv2
import os

def video_to_frames(video, path_output_dir):
    # extract frames from a video and save to directory as 'x.png' where 
    # x is the frame index
    vidcap = cv2.VideoCapture(video)
    count = 0
    while vidcap.isOpened():
        success, image = vidcap.read()
        if success:
            cv2.imwrite(os.path.join(path_output_dir, '%d.png') % count, image)
            count += 1
        else:
            break
    cv2.destroyAllWindows()
    vidcap.release()

video_to_frames('../somepath/myvid.mp4', '../somepath/out')

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

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