OpenCV——保存图片和视频
作者:互联网
保存图片 imwrite
bool cv :: imwrite(const String& filename,InputArray img,Const std::vector<int>& params = std::vector<int>())
- 16位无符号(CV_16U)图像可以保存成PNG、JPEG、TIFF格式文件;
- 32位浮点(CV_32F)图像可以保存成PFM、TIFF、OpenEXR和Radiance HDR格式文件;
- 4通道(Alpha通道)图像可以保存成PNG格式文件。
暂时没有用到复杂的图像保存
基本都是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
)
- filename:保存视频的地址和文件名,包含视频格式
- fourcc:压缩帧的4字符编解码器代码,详细参数在表2-7给出。
- fps:保存视频的帧率,即视频中每秒图像的张数。
- framSize:视频帧的尺寸
- isColor:保存视频是否为彩色视频
简单示例代码
//
// 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