zoukankan      html  css  js  c++  java
  • Opencv 视频转为图像序列


    本系列文章由 @YhL_Leo 出品,转载请注明出处。
    文章链接: http://blog.csdn.net/yhl_leo/article/details/50283303


    基于OpenCV的视频转为图像序列方法:

    • 基于C++版本
    #include <iostream>
    
    #include "cv.h"
    #include "opencv2/opencv.hpp"
    
    using namespace std;
    using namespace cv;
    
    void main()
    {
        VideoCapture cap("C:\Users\Leo\Desktop\Megamind.avi");
        if ( !cap.isOpened() )
        {
            return ;
        }
    
        int imgIndex(0);
        for ( ; ; )
        {
            Mat frame;
            cap >> frame;
            if ( frame.empty() )
            {
                break;
            }
    
            char* imageSaveName = new char[64];
            sprintf( imageSaveName, "C:\Users\Leo\Desktop\new\%05d.jpg", imgIndex );
            imwrite( imageSaveName, frame );
            delete[] imageSaveName;
            imgIndex++;
        }
        cout << "total frames: " << imgIndex << endl;
    }
    • 基于C版本
    #include <iostream>
    
    #include "cv.h"
    #include "opencv2/opencv.hpp"
    
    using namespace std;
    using namespace cv;
    
    void main()
    {
        // video read
        CvCapture *capture = cvCreateFileCapture("C:\Users\Leo\Desktop\Megamind.avi");
        IplImage *frame;
    
        int imgIndex(0);
        while(1)
        {
            frame = cvQueryFrame(capture);
            if ( !frame )
            {
                break;
            }
    
            char* imageSaveName = new char[64];
            sprintf( imageSaveName, "C:\Users\Leo\Desktop\new\%05d.jpg", imgIndex );
            cvSaveImage( imageSaveName, frame );
            delete[] imageSaveName;
            imgIndex++;
        }
        cout << "total frames: " << imgIndex << endl;
        cvDestroyWindow( "VideoImage" );
        cvReleaseCapture( &capture );
        cvReleaseImage( &frame );
    }

    测试数据为OpenCV自带的视频:Megamind.avi,可以在opencvsourcessamplescpp utorial_codeHighGUIvideo-input-psnr-ssimvideo路径下查找,共270帧图像,运行结果部分截图如下:

  • 相关阅读:
    beagle ubuntu
    screen usage
    centos install nginx¢os 添加网易源
    心情舒畅,升级到u10.04了
    nginx 运行,检测
    联通宽带测速
    virtualenv usage
    linux 终端下utf8 和gbk相互转换
    [梦]2005725
    git install on centos
  • 原文地址:https://www.cnblogs.com/hehehaha/p/6332202.html
Copyright © 2011-2022 走看看