其他分享
首页 > 其他分享> > OpenCV——保存图片和视频

OpenCV——保存图片和视频

作者:互联网

保存图片 imwrite

bool cv :: imwrite(const String& filename,InputArray img,Const std::vector<int>& params = std::vector<int>())

暂时没有用到复杂的图像保存

基本都是8位三通道彩色图

简单示例代码

//
// Created by smallflyfly on 2021/6/9.
//

#include "opencv2/video.hpp"
#include "opencv2/core.hpp"
#include "opencv2/highgui.hpp"
#include <iostream>


using namespace std;
using namespace cv;

int main() {

    VideoCapture cap(0);
    if (!cap.isOpened()) {
        cout << "read camera error!" << endl;
        return -1;
    }
    Mat frame;
    cap >> frame;

    // 保存图片
    imwrite("test.jpg", frame);

    cap.release();

    return 0;
}

保存视频 VideoWriter

cv :: VideoWriter :: VideoWriter(); //默认构造函数

cv :: VideoWriter :: VideoWriter(const String& filename,
                                       int fourcc,
                                       double  fps,
                                       Size frameSize,
                                       bool  isColor=true
                                       )

 简单示例代码

//
// Created by smallflyfly on 2021/6/9.
//

#include "opencv2/core.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/video.hpp"

#include "iostream"

using namespace cv;
using namespace std;

int main() {
    VideoCapture cap(0);
    if (!cap.isOpened()) {
        cout << "read camera error!" << endl;
        return -1;
    }
    VideoWriter videoWriter;
    // 编码格式
    int coder = VideoWriter::fourcc('M', 'J', 'P', 'G');
    double fps = 25.0;
    Mat frame;
    cap >> frame;
    videoWriter.open("test.avi", coder, fps, frame.size(), true);
    while (cap.isOpened()) {
        cap >> frame;
        videoWriter.write(frame);
        imshow("camera", frame);

        if (waitKey(1) == 'q') {
            break;
        }
    }
    videoWriter.release();
    cap.release();

    destroyAllWindows();

    return 0;
}

 

标签:视频,hpp,frame,cap,保存,opencv2,OpenCV,include
来源: https://blog.csdn.net/B08370108/article/details/117734856