在帧上剪切视频,每隔3个节省一次?

时间:2017-04-04 03:53:22

标签: python image opencv video

我试着想一想,但是因为新的 - 它并不适合我。 是否有人可以提供帮助,我们应该在这里添加什么来保存,例如,每隔3或5帧? 这是代码

    import cv2
    vidcap = cv2.VideoCapture('myvid.mp4')
    success,image = vidcap.read()
    count = 0;
    print "I am in success"
    while success:
      success,image = vidcap.read()
      if count % 3 == 0:
      cv2.imwrite("img_%3d.jpg" % count, image)     
      if cv2.waitKey(10) == 27:                     
          break
      count += 1

非常感谢你在这个愚蠢的问题上提供帮助^^'

跳过n帧的代码并保存您需要的内容。每第3帧的示例:

import cv2
vidcap = cv2.VideoCapture('myvid.mp4')
success,image = vidcap.read()
count = 0;
print "I am in success"
while success:
  success,image = vidcap.read()
  if count % 3 == 0:
  cv2.imwrite("img_%3d.jpg" % count, image)     
  if cv2.waitKey(10) == 27:                     
      break
  count += 1

2 个答案:

答案 0 :(得分:2)

您需要做的就是检查是否count % 3 == 0。但是,您的代码中还有另一个问题

import cv2
vidcap = cv2.VideoCapture('myvid.mp4')
success,image = vidcap.read()
count = 0;

# number of frames to skip
numFrameToSave = 3

print "I am in success"
while success: # check success here might break your program
  success,image = vidcap.read() #success might be false and image might be None
  #check success here
  if not success:
    break

  # on every numFrameToSave 
  if (count % numFrameToSave ==0):
    cv2.imwrite("img_%3d.jpg" % count, image)   

  if cv2.waitKey(10) == 27:                     
      break
  count += 1

答案 1 :(得分:0)

import cv2
vidcap = cv2.VideoCapture('/content/drive/MyDrive/Front-Dash-Cam-1.m4v')
length = int(vidcap.get(cv2.CAP_PROP_FRAME_COUNT))
print("Total frames in this vid :", length )
success,image = vidcap.read()
count = 0
skipframes = 5 # no of frames to skip
print('started slicing..')
success = True
while success:
  success,image = vidcap.read()
  if (count % skipframes == 0):
    # print('slicing...')
    image  = cv2.resize(image, (256,256)) # resize frame
    cv2.imwrite("frames/frame_%3d.png" % count, image)     # save frame as JPEG file
  if cv2.waitKey(10) == 27:                     # exit if Escape is hit
      break
  count += 1
print('finished..')