首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用opencv和python在每千帧中打印一个?

如何使用opencv和python在每千帧中打印一个?
EN

Stack Overflow用户
提问于 2017-10-20 21:14:02
回答 1查看 247关注 0票数 0

我试着在视频的每千帧中保存一帧。下面是我目前使用的代码:

代码语言:javascript
复制
import cv2
import numpy as np
import os
# Playing video from file:
cap = cv2.VideoCapture('D:/01 Projects/AMAZON CATALYST PROJECT/Surgery1.mpg')
try:
    if not os.path.exists('D:/01 Projects/AMAZON CATALYST PROJECT/data_surg1'):
    os.makedirs('D:/01 Projects/AMAZON CATALYST PROJECT/data_surg1')
except OSError:
    print ('Error: Creating directory of data_surg1')
currentFrame = 0
while(True):
    # Capture frame-by-frame
    if currentFrame > 0:
        cap.set(cv2.CAP_PROP_POS_MSEC,currentFrame) 
    ret, frame = cap.read()

    # Saves image of the current frame in jpg file
    name = 'D:/01 Projects/AMAZON CATALYST PROJECT/data_surg1/frame' + str(currentFrame/1000) + '.jpg'
    print ('Creating...' + name)
    cv2.imwrite(name, frame)

    # To stop duplicate images
    currentFrame += 1000

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()

但是,我不确定这是否正确地保存了它。当我查看文件资源管理器中的帧时,这些数字最初非常高,然后与前面的图像相比,形成一个顺序的帧号。我正在使用Python2.7和OpenCV3.3。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-30 17:48:55

为了存储某些帧而不是基于时间的保存,下面的脚本可以工作:

代码语言:javascript
复制
import cv2
import numpy as np
import os
# Playing video from file:
cap = cv2.VideoCapture('D:/01 Projects/AMAZON CATALYST PROJECT/Surgery1.mpg')
try:
    if not os.path.exists('D:/01 Projects/AMAZON CATALYST PROJECT/data_surg1'):
    os.makedirs('D:/01 Projects/AMAZON CATALYST PROJECT/data_surg1')
except OSError:
    print ('Error: Creating directory of data_surg1')
currentFrame = 0
while(True):
    # Capture frame-by-frame
    if currentFrame > 0:
        cap.set(cv2.CAP_PROP_POS_FRAMES,currentFrame) 
    ret, frame = cap.read()

    # Saves image of the current frame in jpg file
    name = 'D:/01 Projects/AMAZON CATALYST PROJECT/data_surg1/frame' + str(currentFrame/1000) + '.jpg'
    print ('Creating...' + name)
    cv2.imwrite(name, frame)

    # To stop duplicate images
    currentFrame += 1

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46857433

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档