通过在 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 很陌生。
最佳答案
你没有增加文件名,所以它被一次又一次地覆盖。此外,您还需要一个 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()
关于python - 如何将实时视频分割成图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43181758/