opencv3.2.0实现视频抽帧,并保存成图片

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;
}
2.实现结果如下图(该段视频一共有75帧)

posted @ 2017-12-09 09:22  woft王  阅读(5835)  评论(0)    收藏  举报