最近需要制作VOC数据集,需要的样本都是以视频的形式拍摄下来的,所以需要实现将视频转化为图片。
通常,视频的帧率大概为30帧每秒
也就是一秒的视频,如果逐一帧转化则最后转化为30张图片,实事上可能逐帧转化效果并不好,看起来图片几乎一样。因此需要调节每秒传转化帧率。代码:
#include <iostream>
#include <opencv2\highgui/highgui.hpp>
#include <opencv2\core/core.hpp>
#include <fstream>
#define SAVEPATH "E:/picture/67/" //保存地址
using namespace std;
using namespace cv;
int main()
{
string file = "E:/video/67.mp4";//读取视频路径
VideoCapture cap(file);
if (!cap.isOpened())
{
cout << "open video file failed." << endl;
}
int frame_cnt = 0;
int num = 0;
Mat img;
while (true)
{
bool success = cap.read(img);
if (!success)
{
cout<< "Process " << num << " frames from" << file << endl;
break;
}
if (img.empty())
{
cout << "frame capture failed." <<