2017-04-03 92 views
1

在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很新。

+0

使用和字符串和一个递增的数字值使用imwrite功能,每次提供一个不同的文件名(例如,所以你的问题可能是如何将数字添加到字符串不幸的是,我可以”。在那里帮助,因为我不是那么熟悉蟒蛇 – Micka

回答

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() 
+0

Thx这么多你的帮助:D。 – FillzZ

+0

如果它是有益的,请upvote /接受答案:D –