#include <iostream>
#include <fstream>
#include <opencv2/opencv.hpp>
#include<ctime>
using namespace cv;
using namespace std;
bool YV12_to_RGB32(unsigned char* pYV12, unsigned char* pRGB32, int iWidth, int iHeight);
int main()
{
clock_t t1 = clock();
unsigned char *buf = (unsigned char*)malloc(1382400 * sizeof(unsigned char));
fstream binary_file("C:/Users/xgx/Desktop/DecodedVideo.yuv", ios::binary | ios::in);
binary_file.read(reinterpret_cast<char *>(buf), 1382400);
/*for (int i = 0; i < 1382400; i++)
{
cout << (int)buf[i] << endl;
}*/
binary_file.close();
unsigned char *rgb32 = new unsigned char[1280 * 720 * 4];
bool result = YV12_to_RGB32(buf, rgb32, 1280, 720);
Mat rgb(720, 1280, CV_8UC4, rgb32);
clock_t t2 = clock();
cout << "t2-t1: "<<t2-t1<<"ms" << endl;
imshow("rgb", rgb);
waitKey();
return 0;
}
bool YV12_to_RGB32(unsigned char* pYV12, unsigned char* pRGB32, int iWidth, int iHeight)
{
if (!pYV12 || !pRGB32)
return false;
const long nYLen = long(iHeight * iWidth);
const int nHfWidth = (iWidth >> 1);
if (nYLen < 1 || nHfWidth < 1)
return false;
unsigned char* yData = pYV12;
unsigned char* vData = pYV12 + iWidth*iHeight + (iHeight / 2)*(iWidth / 2);
unsigned char* uData = pYV12 + iWidth*iHeight;
if (!uData || !vData)
return false;
int rgb[4];
int jCol, iRow;
for (iRow = 0; iRow < iHeight; iRow++)
{
for (jCol = 0; jCol < iWidth; jCol++)
{
rgb[3] = 1;
int Y = yData[iRow*iWidth + jCol];
int U = uData[(iRow / 2)*(iWidth / 2) + (jCol / 2)];
int V = vData[(iRow / 2)*(iWidth / 2) + (jCol / 2)];
int R = Y + (U - 128) + (((U - 128) * 103) >> 8);
int G = Y - (((V - 128) * 88) >> 8) - (((U - 128) * 183) >> 8);
int B = Y + (V - 128) + (((V - 128) * 198) >> 8);
// r分量值
R = R<0 ? 0 : R;
rgb[2] = R > 255 ? 255 : R;
// g分量值
G = G<0 ? 0 : G;
rgb[1] = G>255 ? 255 : G;
// b分量值
B = B<0 ? 0 : B;
rgb[0] = B>255 ? 255 : B;
pRGB32[4 * (iRow*iWidth + jCol) + 0] = rgb[0];
pRGB32[4 * (iRow*iWidth + jCol) + 1] = rgb[1];
pRGB32[4 * (iRow*iWidth + jCol) + 2] = rgb[2];
pRGB32[4 * (iRow*iWidth + jCol) + 3] = rgb[3];
}
}
return true;
}
RGB24就是普通的三通道,RGB32为带有A通道的,即BGRA
YUV(YV12)转RGB32
最新推荐文章于 2022-07-13 16:06:43 发布