Qt 播放Yuv420p视频

环境:

    vs2015;Qt5.8;opencv3.4;

从文件读取一帧的数据后从yuv转到rgb,用rgb构造一个IplImage,再把IplImage 转成QImage,显示。

yuv420转rgb:

void YUV420_2_RGB(unsigned char* pYUV, unsigned char* pRGB, int width, int height)
{
	//找到Y、U、V在内存中的首地址  
	unsigned char* pY = pYUV;
	unsigned char* pU = pYUV + height*width;
	unsigned char* pV = pU + (height*width / 4);


	unsigned char* pBGR = NULL;
	unsigned char R = 0;
	unsigned char G = 0;
	unsigned char B = 0;
	unsigned char Y = 0;
	unsigned char U = 0;
	unsigned char V = 0;
	double temp = 0;
	for (int i = 0; i < height; i++)
	{
		for (int j = 0; j < width; j++)
		{
			//找到相应的RGB首地址  
			pBGR = pRGB + i*width * 3 + j * 3;

			//取Y、U、V的数据值  
			Y = *(pY + i*width + j);
			U = *pU;
			V = *pV;

			//yuv转rgb公式  
			//yuv转rgb公式  
			temp = Y + ((1.773) * (U - 128));
			B = temp<0 ? 0 : (temp>255 ? 255 : (unsigned char)temp);

			temp = (Y - (0.344) * (U - 128) - (0.714) * (V - 128));
			G = temp<0 ? 0 : (temp>255 ? 255 : (unsigned char)temp);

			temp = (Y + (1.403)*(V - 128));
			R = temp<0 ? 0 : (temp>255 ? 255 : (unsigned char)temp);

			//将转化后的rgb保存在rgb内存中,注意放入的顺序b是最低位  
			*pBGR = B;
			*(pBGR + 1) = G;
			*(pBGR + 2) = R;


			if (j % 2 != 0)
			{
				*pU++;
				*pV++;
			}

		}
		if (i % 2 == 0)
		{
			pU = pU - width / 2;
			pV = pV - width / 2;
		}
	}
}
IplImage 转成QImage:

QImage *IplImageToQImage(const IplImage *img)  
{  
    QImage *image;  
    cvCvtColor(img,img,CV_BGR2RGB);  
    uchar *imgData=(uchar *)img->imageData;  
    image=new QImage(imgData,img->width,img->height,QImage::Format_RGB888);  
    return image;  
}  

参考:

IplImage转QImage: https://blog.csdn.net/gfocean/article/details/6440844

yuv420转RGB:https://blog.csdn.net/rookie_wei/article/details/32909871

项目流程参考:https://blog.csdn.net/leixiaohua1020/article/details/50466201

项目下载:

https://download.csdn.net/download/st_spring/10350563




猜你喜欢

转载自blog.csdn.net/st_spring/article/details/79950454