opencv 保存yuv
时间: 2023-10-16 21:10:13 浏览: 47
你可以使用OpenCV中的cv::VideoWriter类来保存YUV格式的视频。首先,你需要创建一个cv::Mat对象来存储YUV数据,然后将该对象传递给cv::VideoWriter来进行保存。
下面是一个保存YUV视频的示例代码:
```c++
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
// 创建一个cv::Mat对象来存储YUV数据
int width = 640;
int height = 480;
Mat yuv(height + height / 2, width, CV_8UC1);
// 打开输入YUV文件
FILE* fp = fopen("input.yuv", "rb");
if (!fp) {
cerr << "Failed to open input file!" << endl;
return -1;
}
// 创建输出YUV文件
VideoWriter writer("output.yuv", cv::VideoWriter::fourcc('I', '4', '2', '0'), 25.0, Size(width, height), false);
// 读取每一帧数据并进行保存
int frame_size = width * height * 3 / 2;
while (!feof(fp)) {
fread(yuv.data, 1, frame_size, fp);
writer.write(yuv);
}
// 释放资源
fclose(fp);
writer.release();
return 0;
}
```
在上面的代码中,我们假设输入YUV文件的格式为I420,并且每一帧数据的大小为width * height * 3 / 2。我们使用cv::VideoWriter::fourcc函数来指定输出YUV文件的编码格式为I420,帧率为25帧每秒。最后,我们使用cv::VideoWriter::release函数来释放cv::VideoWriter对象。