如何使用opencv python

时间:2015-05-09 05:08:33

标签: python opencv

我希望从视频中获取图片并将其存储在' .jpeg'或者' .png'格式请帮我如何用opencv做这个我的代码是



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

2 个答案:

答案 0 :(得分:12)

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

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')

答案 1 :(得分:0)

这是我使用opencv

从视频中获取图片的代码
import cv2
print(cv2.__version__)
vidcap = cv2.VideoCapture("Malar.mp4")
print vidcap.read()
success,image = vidcap.read()
count = 0
success = True
while success:
  success,image = vidcap.read()
  print 'Read a new frame: ', success
  cv2.imwrite("frame%d.jpg" % count, image)     # save frame as JPEG file
  count += 1