OpenCV Python - Extract Images from Video
A video is nothing but a sequence of frames and each frame is an image. By using OpenCV, all the frames that compose a video file can be extracted by executing imwrite() function till the end of video.
The cv2.read() function returns the next available frame. The function also gives a return value which continues to be true till the end of stream. Here, a counter is incremented inside the loop and used as a file name.
Following program demonstrates how to extract images from the video −
import cv2
import os
cam = cv2.VideoCapture("video.avi")
frameno = 0
while(True):
ret,frame = cam.read()
if ret:
# if video is still left continue creating images
name = str(frameno) + '.jpg'
print ('new frame captured...' + name)
cv2.imwrite(name, frame)
frameno += 1
else:
break
cam.release()
cv2.destroyAllWindows()
Advertisements