可以先使用ffmpeg把bmp或者JPG转成YUV:
ffmpeg.exe -i C:\Users\gang.hu\Desktop\xxxx.bmp -s 1920x1080 -pix_fmt yuv420p C:\Users\gang.hu\Desktop\xxxx.yuv
#include <opencv2/opencv.hpp>
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace cv;
using namespace std;
char buff[2000000];
int main()
{
int width = 640;
int height = 640;
int yuvNV12_size = width * height * 3 / 2;
int rgb24_size = width * height;
Mat yuvNV12;
Mat rgb24;
FILE* f = fopen("./xxxx.yuv", "r");
memset(buff, 0, 2000000);
fread(buff, 1, yuvNV12_size, f);
yuvNV12.create(640 * 3 / 2, 640, CV_8UC1);
memcpy(yuvNV12.data, buff, yuvNV12_size);
cvtColor(yuvNV12, rgb24, COLOR_YUV2BGR_NV12);
imshow("12", rgb24);
fflush(f);
fclose(f);
waitKey(0);
return 0;
}