【问题标题】:Getting a specific sequence of frames from video in python从python中的视频中获取特定的帧序列
【发布时间】:2019-02-19 11:21:37
【问题描述】:

我需要从视频中提取特定的帧序列,例如我想要的

例如每 10 帧提取一次 (frame_1,frame_10,frame_20,...)。我是

使用下面的代码,但它提取了所有帧,知道怎么做吗?

import cv2
vidcap = cv2.VideoCapture('big_buck_bunny_720p_5mb.mp4')
success,image = vidcap.read()
count = 0
success = True
while success:

   cv2.imwrite("frame%d.jpg" % count, image)     # save frame as JPEG file
   success,image = vidcap.read()

count += 1

【问题讨论】:

    标签: python opencv ffmpeg opencv3.0


    【解决方案1】:

    只是为了添加另一个解决方案,您可以使用set 函数和CAP_PROP_POS_FRAMES prperty id 来获取10帧后的帧,如下所示:

    import cv2
    vidcap = cv2.VideoCapture('big_buck_bunny_720p_5mb.mp4')
    success,image = vidcap.read()
    count = 0
    success = True
    while success:
       cv2.imwrite("frame%d.jpg" % count, image)     # save frame as JPEG file
       success,image = vidcap.read()
       count += 1
       vidcap.set(cv2.CAP_PROP_POS_FRAMES, count * 10 )
    

    与其他答案相比,这可能会给您带来性能优势,因为它不会读取每一帧,但如果它是来自相机的视频,我认为这个解决方案不会做任何事情,而另一个会做。

    【讨论】:

      【解决方案2】:

      使用帧数的modulo

      import cv2
      vidcap = cv2.VideoCapture('big_buck_bunny_720p_5mb.mp4')
      success,image = vidcap.read()
      count = 0
      frame = 0 #after first frame read, so frame 0 will be saved, next every 10th
      success = True
      while success:
      
         if frame % 10 == 0:
             cv2.imwrite("frame%d.jpg" % count, image)     # save frame as JPEG file
             count += 1
         success,image = vidcap.read()
         frame += 1
      

      【讨论】:

      • 非常感谢@J.D 和@api55
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-20
      • 2019-03-29
      • 2019-05-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多