Using opencv, how can one split a video into an image sequence?
How can i split it so that the output will be a sequence of images?
使用opencv,如何将视频分割成图像序列?如何拆分它以使输出成为一系列图像?
2 个解决方案
#1
11
For my surprise, I couldn't find an answer to this question on *.
令我惊讶的是,我在*上找不到这个问题的答案。
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*
我目前正在使用OpenCV 2.1。这可能有点旧,但它就像一个魅力。程序将读取输入文件并在当前文件夹上创建一系列图像,名为* 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;
}
#2
9
Even after my edits, the question looks more like a "send-me-the-codez" demand than a typical * question whereby the OP provides detailed information and demonstrates that he or she has put some real thought into it.
即使在我编辑之后,这个问题看起来更像是一个“发送我代码”的需求而不是典型的*问题,因为OP提供了详细信息并证明他或她已经对其进行了一些真实的思考。
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. ;-)
所以我只是给出一个“半答案”......无论如何,我只是熟悉OpenCV的机器学习模块,几乎不知道库的其余部分。 ;-)
In a nutshell, the pseudo code should look like that and the implementation would typically require the OpenCV methods mentioned.
简而言之,伪代码看起来应该是这样,并且实现通常需要提到的OpenCV方法。
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.
如上所述,上面需要很多语法和逻辑工作(例如,确定何时停止,生成各个帧的文件名,当然正确地声明变量并使用正确的间接级别w /所有指针;-) 。当然,如果使用Python界面,其中一些变得更容易。
The Reading and Writing Images and Video OpenCV documentation page, provides more detailed info about the individual methods.
阅读和编写图像和视频OpenCV文档页面,提供有关各个方法的更多详细信息。
#1
11
For my surprise, I couldn't find an answer to this question on *.
令我惊讶的是,我在*上找不到这个问题的答案。
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*
我目前正在使用OpenCV 2.1。这可能有点旧,但它就像一个魅力。程序将读取输入文件并在当前文件夹上创建一系列图像,名为* 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;
}
#2
9
Even after my edits, the question looks more like a "send-me-the-codez" demand than a typical * question whereby the OP provides detailed information and demonstrates that he or she has put some real thought into it.
即使在我编辑之后,这个问题看起来更像是一个“发送我代码”的需求而不是典型的*问题,因为OP提供了详细信息并证明他或她已经对其进行了一些真实的思考。
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. ;-)
所以我只是给出一个“半答案”......无论如何,我只是熟悉OpenCV的机器学习模块,几乎不知道库的其余部分。 ;-)
In a nutshell, the pseudo code should look like that and the implementation would typically require the OpenCV methods mentioned.
简而言之,伪代码看起来应该是这样,并且实现通常需要提到的OpenCV方法。
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.
如上所述,上面需要很多语法和逻辑工作(例如,确定何时停止,生成各个帧的文件名,当然正确地声明变量并使用正确的间接级别w /所有指针;-) 。当然,如果使用Python界面,其中一些变得更容易。
The Reading and Writing Images and Video OpenCV documentation page, provides more detailed info about the individual methods.
阅读和编写图像和视频OpenCV文档页面,提供有关各个方法的更多详细信息。