【问题标题】:How to split real-time video into image?如何将实时视频分割成图像?
【发布时间】:2017-04-03 10:05:18
【问题描述】:

通过在 python 上使用 opencv。我录制了视频,然后将其拆分为图片,但是花了很长时间,所以我想在录制视频时立即将视频拆分为图片。我在互联网上找到了代码,但它只能捕获 1 张图片。

import cv2

def main():
    cam = cv2.VideoCapture(0)
    frame = cam.read()[1]
    cv2.imwrite(filename='img%d.jpg',img=frame)

if __name__== '__main__':
    main()

谁能帮助我?我对 python 和 opencv 很陌生。

【问题讨论】:

  • 使用 imwrite 函数并每次提供不同的文件名(例如通过使用和字符串和递增的数值。所以您的问题可能是如何将数字添加到字符串中。不幸的是我不能帮不上忙,因为我对 python 不太熟悉

标签: python opencv video


【解决方案1】:

你没有增加文件名,所以它被一次又一次地覆盖。此外,您需要一个 while 循环。试试:

import cv2

def main():
    cam = cv2.VideoCapture(0)
    frameNum = 0
    isCaptured = True
    while True:
        isCapture, frame = cam.read()
        if not isCapture:
            # no more frame, exit loop
            break
        frameNum = frameNum + 1
        fileName = 'img{:d}.jpg'.format(frameNum)
        cv2.imwrite(filename=fileName,img=frame)

if __name__== '__main__':
    main()

【讨论】:

  • 非常感谢您的帮助:D。
猜你喜欢
  • 2011-05-20
  • 1970-01-01
  • 2021-01-16
  • 2016-12-22
  • 2013-12-05
  • 2023-02-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多