YUV(YV12)转RGB32

#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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值