温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用Opencv怎么将视频转换为图像序列

发布时间:2021-02-22 18:02:10 来源:亿速云 阅读:231 作者:戴恩恩 栏目:编程语言

这篇文章主要介绍了使用Opencv怎么将视频转换为图像序列,此处通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考价值,需要的朋友可以参考下:

基于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怎么将视频转换为图像序列的文章就介绍到这了,更多相关内容请搜索亿速云以前的文章或继续浏览下面的相关文章希望大家以后多多支持亿速云!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI