Ask Your Question
1

convert video to image and save it

asked 2012-11-24 02:03:34 -0600

javani gravatar image

updated 2017-08-22 14:27:53 -0600

hi

I want to convert video to image and save it

Opencv Can you do it?

Thanks in advance...

edit retag flag offensive close merge delete

3 answers

Sort by ยป oldest newest most voted
4

answered 2012-11-24 05:25:17 -0600

Hi Javani,

Yes you can do it (assuming that you have the proper codecs installed to read the video). I think it is a beautiful exercise to get acquainted with OpenCV, you can do something like this:

#include <iostream>
#include <stdio.h>
#include <opencv2/opencv.hpp>

int main (int argc, char **argv)
{
  if (argc !=2)
  { 
    std::cout << "USE: " << argv[0] << " <video-filename>" << std::endl;
    return 1;
  }

  //Open the video that you pass from the command line
  cv::VideoCapture cap(argv[1]);
  if (!cap.isOpened())
  {
    std::cerr << "ERROR: Could not open video " << argv[1] << std::endl;
    return 1;
  }

  int frame_count = 0;
  bool should_stop = false;

  while(!should_stop)
  {
    cv::Mat frame;
    cap >> frame; //get a new frame from the video
    if (frame.empty())
    {
      should_stop = true; //we arrived to the end of the video
      continue;
    }

    char filename[128];
    sprintf(filename, "frame_%06d.jpg", frame_count);
    cv::imwrite(filename, frame);
    frame_count++;
  }

  return 0;
}

Note that I just coded this "on the fly", I don't know if the code compiles or works properly, but you get the idea.

Also, ffmpeg can convert a video to images and you don't need to code anything.

I hope this helps

edit flag offensive delete link more

Comments

hi i am using ur code for my learning can u please tel me how can i store this images to specific folder? iam using strcpy("path name",filename); but it shows " Access violation writing location 0x00007FF7D4633318." at run time?

usuf gravatar imageusuf ( 2017-10-06 01:36:46 -0600 )edit
1

answered 2012-11-24 03:43:56 -0600

Haris gravatar image

updated 2012-11-24 03:48:20 -0600

You can have some thing like

 cvSaveImage("image.jpeg",frame);
edit flag offensive delete link more
0

answered 2017-10-06 00:57:43 -0600

usuf gravatar image

hi i am using ur code for my learning can u please tel me how can i store this images to specific folder? iam using strcpy("path name",filename); but it shows " Access violation writing location 0x00007FF7D4633318." at run time?

edit flag offensive delete link more

Question Tools

Stats

Asked: 2012-11-24 02:03:34 -0600

Seen: 10,468 times

Last updated: Nov 24 '12