如何使用 python 将特定的视频帧保存到文件夹中?

How do I save particular frames of video into folders using python?

我有一个代码可以进行运动检测。它以视频作为输入,然后当对象出现在场景中时,背景发生变化,代码将“对象被看到”作为检测到的帧上的文本发布。

我的问题是如何将“看到对象时”的帧保存到文件夹中(包括彩色和灰色图像)?我知道我可以使用“cv2.imwrite("frame%d.jpg" % count, resizedFrame)”来编写这些帧,但不幸的是它不起作用。

如何将检测到的帧保存到“彩色”和“灰色”文件夹?

你必须先通过cv2.imread()阅读图像,然后 cv2.imwrite() 这是一个小例子

import
cv2
 
# read image as grey scale
grey_img=cv2.imread('/home/img/python.png',cv2.IMREAD_GRAYSCALE)
 # save image
status=cv2.imwrite('/home/img/python_grey.png',grey_img)
 print("Image written to file-system : ",status)

如果您没有文件夹路径,请使用此 status=cv2.imwrite('python_grey.png',grey_img) 它会将照片保存在您保存 .py 文件

的默认文件夹中

如果你想保存不同的图片这里是代码

import cv2
import time

# for number of images you need different names so set name automatically
name=str(time.time())+str(.png))
 # read image as grey scale
grey_img=cv2.imread('/home/img/python.png',cv2.IMREAD_GRAYSCALE)
 # save image
status=cv2.imwrite('%s'%name,grey_img)
 print("Image written to file-system : ",status)