1.实现指定帧数的抽取、和全部帧数的抽取,并保存到指定目录。
在QT新建一个控制台程序,程序源码如下:(程序实现每十帧获取一次帧) #include <QCoreApplication> #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/imgproc.hpp> #include <iostream>
using namespace std; using namespace cv; int main() { /******** 获取视频文件(实例化的同时进行初始化)*******/ VideoCapture capture("/home/ttwang/out.mp4"); /********** 获取视频总帧数并打印*****************/
long totalFrameNumber = capture.get(CV_CAP_PROP_FRAME_COUNT); cout << "total frames: " << totalFrameNumber << endl; Mat frame; //定义一个Mat变量,用来存放存储每一帧图像
bool flags = true; //循环标志位
long currentFrame = 0; //定义当前帧
while (flags) { capture.read(frame); // 读取视频每一帧
stringstream str; //stringstream字符串流,将long类型的转换成字符型传给对象str
str << "f" << currentFrame << ".jpg"; cout << "正在处理第" << currentFrame << "帧" << endl; /***设置每10帧获取一次帧***/
if (currentFrame % 10 == 0) { imwrite("/home/ttwang/images/image" + str.str(), frame); // 将帧转成图片输出
} /**** 结束条件,当前帧数大于总帧数时候时,循环停止****/
if (currentFrame >= totalFrameNumber) { flags = false; } currentFrame++; } waitKey(0); return 0; }
