• 抽取视频中的帧并保存为图片



    注:原创不易,转载请务必注明原作者和出处,感谢支持!

    保存视频中的帧

    利用OpenCV提供的VideoCapture类可以轻松实现保存视频中帧的功能。下面代码可以将一个视频文件中的所有帧抽取并保存成JPG图像。

    videoName - 视频文件名
    imagePath - 图片保存的路径
    imagePrefix - 图片文件前缀字符串

    图片完整路径:imagePath + "/" + imagePrefix + to_string(i) + ".jpg"

    void ExtractFrames(const string &videoName, const string &imagePath, const string &imagePrefix)
    {
    	VideoCapture cap;
    	Mat img;
    
    	// 打开视频
    	cap.open(videoName);
    	if (!cap.isOpened())
    	{
    		cout << "Error : could not load video" << endl;
    		exit(-1);
    	}
    
    	// 取得视频帧数
    	size_t count = (size_t)cap.get(CV_CAP_PROP_FRAME_COUNT);
    	for (size_t i = 0; i < count; ++i)
    	{
    		cap >> img;
    		string imgName = imagePath + "/" + imagePrefix + to_string(i) + ".jpg";
    		// 将当前帧保存
    		imwrite(imgName, img);
    		cout << "Frames " << i << " ... done" << endl;
    	}
    }
    

    ExtractFrames调用实例:

    #include <opencv2/opencv.hpp>
    #include <iostream>
    
    using namespace cv;
    using namespace std;
    
    int main(int argc, char *argv[])
    {
    	ExtractFrames("E:/images/video/1.mp4", "E:/images/video", "red-forest");
    
    	return 0;
    }
    

    抽取出的图片将被保存为:

    red-forest0.jpg
    red-forest1.jpg
    red-forest2.jpg
    ...
    

    red-forest45.jpg

  • 相关阅读:
    使用Nginx搭建http服务器
    (七)Docker搭建httpd集群
    zlib库对文件进行压缩和解压操作
    (一)Apache Thrift 的使用
    (一)select、poll、epoll
    (十三)备忘录模式
    (十二)命令模式
    (十一)迭代器模式
    centos下利用httpd搭建http服务器方法
    shell快捷键
  • 原文地址:https://www.cnblogs.com/laizhenghong2012/p/10797745.html
Copyright © 2020-2023  润新知