Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get image from video using opencv python

Tags:

python

opencv

I want to get image from video and store it in '.jpeg' or '.png' format please help me how to do this with opencv My code is

import cv2
vidcap = cv2.VideoCapture('video1.mp4')
success,image = vidcap.read()
count = 0;
print "I am in success"
while success:
  success,image = vidcap.read()
  cv2.imwrite("frame%d.jpg" % count, image)     # save frame as JPEG file
  if cv2.waitKey(10) == 27:                     # exit if Escape is hit
      break
  count += 1

Here i am trying to get the image from video frame by frame and save it as frame1,frame2,frame3

like image 767
programminglover Avatar asked May 09 '15 05:05

programminglover


People also ask

What is OpenCV in Python?

Python provides various libraries for image and video processing. One of them is OpenCV. OpenCV is a vast library that helps in providing various functions for image and video operations.

How to extract images from video using Python OpenCV?

Extract Images from Video using Python OpenCV 1. Importing Modules. The first step just like any other project is to import the modules. We would be needing just the... 2. Capturing the video. To capture the video, we will be using the VideoCapture function of the opencv module and store... 3. ...

How to capture a video from the camera using OpenCV?

With OpenCV, we can capture a video from the camera. It lets you create a video capture object which is helpful to capture videos through webcam and then you may perform desired operations on that video. Steps to capture a video: Use cv2.VideoCapture () to get a video capture object for the camera.

How to convert images to a video file in Python?

We can use python moviepy to convert images to a video file. Here are tutorials: However, the converted effect is not good. Fortunately, we also can use python opencv to convert images to a video file. In this tutorial, we will introduce how to do.


2 Answers

This is what I use to read in a video and save off the frames:

import cv2
import os

def video_to_frames(video, path_output_dir):
    # extract frames from a video and save to directory as 'x.png' where 
    # x is the frame index
    vidcap = cv2.VideoCapture(video)
    count = 0
    while vidcap.isOpened():
        success, image = vidcap.read()
        if success:
            cv2.imwrite(os.path.join(path_output_dir, '%d.png') % count, image)
            count += 1
        else:
            break
    cv2.destroyAllWindows()
    vidcap.release()

video_to_frames('../somepath/myvid.mp4', '../somepath/out')
like image 56
Scott Avatar answered Oct 13 '22 03:10

Scott


This is my code for get image from video with opencv

import cv2
print(cv2.__version__)
vidcap = cv2.VideoCapture("Malar.mp4")
print vidcap.read()
success,image = vidcap.read()
count = 0
success = True
while success:
  success,image = vidcap.read()
  print 'Read a new frame: ', success
  cv2.imwrite("frame%d.jpg" % count, image)     # save frame as JPEG file
  count += 1
like image 34
Manivannan Murugavel Avatar answered Oct 13 '22 05:10

Manivannan Murugavel