通过OpenCV实现图片和视频的互相转换

将视频转换为图片,jpg为有损压缩,png为无损压缩,jpg比png的图片清晰需要的存储空间大,需要存储哪种图片类型 可以自行修改

#include<opencv2/opencv.hpp>

using namespace cv;
using namespace std;

int main() {
    VideoCapture videoCap("E:\\软件所资料\\科创项目材料\\无人机资料\\无人机测试视频\\录制视频\\test.mov");
    Mat img;
    int count = 1;
    videoCap >> img;
    while (img.empty()==false) {  //如果取到的图片不为空
        String imageName = "E:\\软件所资料\\科创项目材料\\图片\\无人机视频所有图片\\";
        ostringstream strCount;
        strCount << count;
        string str = strCount.str();
        imageName.append(str);
        imageName.append(".png");
        imwrite(imageName, img);
        count++;
        videoCap >> img; //读取下一帧
    }
    
}

 将图片转为视频

#include<opencv2\opencv.hpp>
#include<iostream>

using namespace std;
using namespace cv;

int main()
{

    VideoWriter video("E:\\软件所资料\\科创项目材料\\无人机资料\\无人机测试视频\\录制视频\\testtest.avi", CV_FOURCC('X', 'V', 'I', 'D'), 24.0, Size(1920, 1080));

    String img_path = "E:\\软件所资料\\科创项目材料\\图片\\无人机视频所有图片\\";
    vector<String> img;

    glob(img_path, img, false);

    size_t count = img.size();
    for (size_t i = 0; i < count; i++)
    {
        stringstream str;
        str << i << ".png";
        Mat image = imread(img_path + str.str());
        if (!image.empty())
        {
            resize(image, image, Size(1920, 1080));
            video << image;
            cout << "正在处理第" << i << "" << endl;
        }
    }
    cout << "处理完毕!" << endl;
}

猜你喜欢

转载自www.cnblogs.com/henuLiGang/p/10432016.html
今日推荐