Instagram
youtube
Facebook
Twitter

Extract Images from video

  • With the help of OpenCV we can extract frames from a video and save them as image files.

  • cv2.videocapture() can be used to extract frames from a video by calling the read() method.

  • We can save or process the extracted frames.

In this tutorial, we’ll learn about extracting images from video using OpenCV.

input:

Code:

import cv2
vidcap = cv2.VideoCapture('video1.mp4')
success, image = vidcap.read()
count = 0
increment = 5


while success:
  cv2.imwrite("frame%d.jpg" % count, image)
  success, image = vidcap.read()
  count += increment


print("Extracted %d frames from the video" % count)
  • First we imported required OpenCV.

  • Then, we used the cv2.VideoCapture() function to read the video file.

  • Then, we used vidcap.read() function to read every frame from the video and stored it into success.

  • After this we initialized two variables count and increment.

  • The count variable is used to keep track of the number of frames extracted and the increment variable is used to specify the number of frames to be skipped.

  • Then, we created a while loop to run till the last frame of the video.

  • Inside while loop. We used cv2.imwrite function to save each extracted frame as a jpg file.

  • At last, we incremented the value of count.  And printed the total number of count.

 

Result:

Extracted 2330 frames from the video