基于OpenCV的视频转为图像序列方法:
基于C++版本
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
#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版本
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
#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 视频转为图像序列的实现就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/yhl_leo/article/details/50283303