openCV: How to split a video into image sequence?(openCV:如何将视频拆分为图像序列?)
问题描述
使用opencv,如何将视频分割成图像序列?
我如何拆分它以便输出将是一系列图像?
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?
推荐答案
令我惊讶的是,我在 StackoverFlow 上找不到这个问题的答案.
For my surprise, I couldn't find an answer to this question on StackoverFlow.
我目前使用的是 OpenCV 2.1.这可能有点旧,但它就像一个魅力.该程序将读取一个输入文件并在名为 *frame_xx.jpg* 的当前文件夹上创建一系列图像
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>
");
        return -1;
    }
    printf("* Filename: %s
", argv[1]);   
    CvCapture *capture = cvCaptureFromAVI(argv[1]);
    if(!capture) 
    {
        printf("!!! cvCaptureFromAVI failed (file not found?)
");
        return -1; 
    }
    int fps = (int) cvGetCaptureProperty(capture, CV_CAP_PROP_FPS);
    printf("* FPS: %d
", 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
");
            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
", filename);
        if (!cvSaveImage(filename, frame))
        {
            printf("!!! cvSaveImage failed
");
            break;
        }
        frame_number++;
        // quit when user press 'q'
        key = cvWaitKey(1000 / fps);
    }
    // free resources
    cvReleaseCapture(&capture);
    return 0;
}
                        这篇关于openCV:如何将视频拆分为图像序列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:openCV:如何将视频拆分为图像序列?
				
        
 
            
        - 近似搜索的工作原理 2021-01-01
 - 与 int by int 相比,为什么执行 float by float 矩阵乘法更快? 2021-01-01
 - C++ 协变模板 2021-01-01
 - 如何对自定义类的向量使用std::find()? 2022-11-07
 - Stroustrup 的 Simple_window.h 2022-01-01
 - 使用/clr 时出现 LNK2022 错误 2022-01-01
 - 一起使用 MPI 和 OpenCV 时出现分段错误 2022-01-01
 - STL 中有 dereference_iterator 吗? 2022-01-01
 - 静态初始化顺序失败 2022-01-01
 - 从python回调到c++的选项 2022-11-16
 
						
						
						
						
						
				
				
				
				