实测代码如下:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
char buff[2000000];
int main()
{
int width = 1280;
int height = 960;
int yuvNV12_size = width * height * 3 / 2;
int rgb24_size = width * height;
Mat yuvNV12;
Mat rgb24;
vector<cv::String> files_yuv;
glob("D:/work/data-yuv/*.yuv", files_yuv);
for (size_t i = 0; i < files_yuv.size(); i++)
{
printf("image file : %s \n", files_yuv[i].c_str());
//1.read nv12 file to nv12 mat
FILE* f = fopen(files_yuv[i].c_str(), "r");
memset(buff, 0, 2000000);
fread(buff, 1, yuvNV12_size, f);
yuvNV12.create(height * 3 / 2, width, CV_8UC1);
memcpy(yuvNV12.data, buff, yuvNV12_size);
//2.cvt nv12 mat to rgb24 mat
cvtColor(yuvNV12, rgb24, COLOR_YUV2BGR_NV12);
//3.imwrite
std::string savePath = files_yuv[i] + "_rgb.jpg";
imwrite(savePath, rgb24);
fflush(f);
fclose(f);
}
retu

本文档展示了如何使用OpenCV库将YUV NV12格式的视频帧转换为RGB并保存为JPEG图片。代码中遇到栈溢出问题,通过将大数组改为全局变量或使用动态内存分配解决。同时,提到了在Windows环境下消除fopen安全警告的方法。
最低0.47元/天 解锁文章
2784





