Opencv 视频转为图像序列的实现
更新时间:2019年12月12日 09:08:11 作者:yhl_leo
今天小编就为大家分享一篇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自带的视频:Megamind.avi,可以在opencv\sources\samples\cpp\tutorial_code\HighGUI\video-input-psnr-ssim\video路径下查找,共270帧图像,运行结果部分截图如下:

以上这篇Opencv 视频转为图像序列的实现就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
Visual Studio 2022使用MinGW来编译调试C/C++程序的图文教程
这篇文章主要介绍了Visual Studio 2022使用MinGW来编译调试C/C++程序,以实例来简单介绍一下VS2022中如何使用MinGW来编译、调试C/C++程序,需要的朋友可以参考下2022-08-08
马尔可夫链算法(markov算法)的awk、C++、C语言实现代码
这篇文章主要介绍了马尔可夫链算法(markov算法)的awk、C++、C语言实现代码,需要的朋友可以参考下2014-08-08
Clion2020.2.x最新激活码破解版附安装教程(Mac Linux Windows)
Clion2020增加了很多新特性,修复了大量bug,大大提高了开发效率。这篇文章主要介绍了Clion2020.2.x最新激活码破解版附安装教程(Mac Linux Windows),需要的朋友可以参考下2020-11-11


最新评论