How to split a video by openCV?

openCV: How to split a video into image sequence?

  • Using opencv, how can one split a video into image sequence. How can i split it so the output be a sequence of images.

  • Answer:

    Even after my edits, the question looks more like a "send-me-the-codez" demand than a typical StackOverflow question whereby the OP provides detailed information and demonstrates that he or she has put some real thought into it. So I'll just give a "half answer"... Anyway I'm only moderately familiar with OpenCV's machine learning module and barely cognizant of the rest of the library. ;-) In a nutshell, the pseudo code should look like that and the implementation would typically require the OpenCV methods mentioned. myCapt = cvCreateFileCapture("myInput.avi") // Assuming the feed is from a file. while (there are frames and we still want them) cvGrabFrame(myCapt) myImg = cvRetrieveFrame(myCapt) // alternatively to cvGrabFrame + cvRetrieveFrame, could use cvQueryFrame() // save the file (or do something with it...) cvSaveImage("some_file_name_with_seq_nr", myImage) cvReleaseCapture(capture) As hinted the above needs much syntax and logic work (eg. figuring out when to stop, producing the file name of the individual frames, and of course declaring properly the variables and using the right level of indirection w/ all them pointers ;-). Of course some of this is made easier if you use the Python interface. The http://opencv.willowgarage.com/documentation/c/highgui_reading_and_writing_images_and_video.html, provides more detailed info about the individual methods.

meroz at Stack Overflow Visit the source

Was this solution helpful to you?

Other answers

For my surprise, I couldn't find an answer to this question on StackoverFlow. I'm currently using OpenCV 2.1. This might be a little old but it works like a charm. The program will read an input file and create a sequence of images on the current folder named *frame_xx.jpg* #include <stdio.h> #include <stdlib.h> #include "cv.h" #include "highgui.h" int main( int argc, char** argv ) { if (argc < 2) { printf("!!! Usage: ./program <filename>\n"); return -1; } printf("* Filename: %s\n", argv[1]); CvCapture *capture = cvCaptureFromAVI(argv[1]); if(!capture) { printf("!!! cvCaptureFromAVI failed (file not found?)\n"); return -1; } int fps = (int) cvGetCaptureProperty(capture, CV_CAP_PROP_FPS); printf("* FPS: %d\n", fps); IplImage* frame = NULL; int frame_number = 0; char key = 0; while (key != 'q') { // get frame frame = cvQueryFrame(capture); if (!frame) { printf("!!! cvQueryFrame failed: no frame\n"); break; } char filename[100]; strcpy(filename, "frame_"); char frame_id[30]; itoa(frame_number, frame_id, 10); strcat(filename, frame_id); strcat(filename, ".jpg"); printf("* Saving: %s\n", filename); if (!cvSaveImage(filename, frame)) { printf("!!! cvSaveImage failed\n"); break; } frame_number++; // quit when user press 'q' key = cvWaitKey(1000 / fps); } // free resources cvReleaseCapture(&capture); return 0; }

karlphillip

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.