实测代码如下:
#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_YUV2RGB_I420);
//3.imwrite
std::string savePath = files_yuv[i] + "_rgb.jpg";
imwrite(savePath, rgb24);
fflush(f);
fclose(f);
}
return 0;
}
需要注意:
- char buff[2000000]预分配的数组较大,如果放置到main函数里面,会有如下报错:
0xC00000FD: Stack overflow (参数: 0x0000000000000001, 0x000000E3CE403000)。 Unhandled exception at 。。。。。 : Stack overflow (parameters: 0x0000000000000001。。。。。
解决办法: (1)如测试用例,把该数组定义成全局变量; (2)根据new, delete来创建数组:
char buff* = new char[2000000];
....
delete[] buff;
本测试用例会反复使用该buff,建议用方式1;
- c4996 error warning (fopen,fopen_s)
在Project properties->Configuration Properties->C/C+±>Preprocessor->Preprocessor Definitions 添加_CRT_SECURE_NO_WARNINGS
|