openCV:如何将视频分割成图像序列?

发布于 2024-10-05 22:43:48 字数 50 浏览 0 评论 0原文

使用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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

背叛残局 2024-10-12 22:43:48

令我惊讶的是,我在 StackoverFlow 上找不到这个问题的答案。

我目前正在使用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;
}

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;
}
霊感 2024-10-12 22:43:48

即使在我编辑之后,这个问题看起来也更像是“发送给我代码”需求,而不是典型的 StackOverflow 问题,OP 提供详细信息并证明他或者她已经认真思考过。

所以我只会给出“一半的答案”......无论如何,我对 OpenCV 的机器学习模块只是中等程度的熟悉,并且几乎不了解库的其余部分。 ;-)

简而言之,伪代码应该像这样,并且实现通常需要提到的 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)

正如所暗示的,上面需要大量的语法和逻辑工作(例如,弄清楚何时停止,生成各个帧的文件名,当然还要正确声明变量并使用所有指针的正确间接级别;-) 。当然,如果您使用 Python 界面,其中一些工作会变得更容易。

读取和写入图像和视频 OpenCV 文档页面 ,提供有关各个方法的更详细信息。

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 Reading and Writing Images and Video OpenCV documentation page, provides more detailed info about the individual methods.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文