本文介绍了将rgb视频图像转换为yuv420格式视频图像的方法,附有详细的代码和图像示例。文中rgb24和yuv420文件需要使用yuv/rgb播放器才能查看,参考播放器可选择雷神推荐的修改了一个YUV/RGB播放器,或者小编使用的vooya播放器。
rgb到yuv的转换公式:
Y = 0.299 ∗ R + 0.587 ∗ G + 0.114 ∗ B Y=0.299*R+0.587*G+0.114*B Y=0.299∗R+0.587∗G+0.114∗B U = − 0.147 ∗ R − 0.289 ∗ G + 0.463 ∗ B U=-0.147*R-0.289*G+0.463*B U=−0.147∗R−0.289∗G+0.463∗B V = 0.615 ∗ R − 0.515 ∗ G − 0.100 ∗ B V=0.615*R-0.515*G-0.100*B V=0.615∗R−0.515∗G−0.100∗B
1.RGB24存储方式是Packed,YUV420存储方式是planer。
2.U、V分量在水平和垂直方向上的取样数是Y分量的1/2。
本函数实现了将rgb视频图像转换为yuv420格式视频图像,具体代码如下所示。
int CTransPic::simplest_rgb24_to_yuv420(const char* url, int w, int h, int num, const char* url_out)
{
ifstream fp_rgb24;
fp_rgb24.open(url, ios::in | ios::binary);
ofstream fp_yuv420;
fp_yuv420.open(url_out, ios::out | ios::binary);
//RGB buffer
unsigned char* rgb24_pic = (unsigned char*)malloc(w*h * 3);
//YUV buffer
unsigned char* yuv420_pic = (unsigned char*)malloc(w*h * 3 / 2);
for (int i = 0; i < num; i++)
{
fp_rgb24.read((char*)rgb24_pic, w*h * 3);
RGB24_to_YUV420(rgb24_pic, w, h, yuv420_pic);
fp_yuv420.write((const char*)yuv420_pic, w*h * 3 / 2);
}
free(rgb24_pic);
free(yuv420_pic);
fp_rgb24.close();
fp_yuv420.close();
return 0;
}
代码中的关键函数RGB24_to_YUV420(rgb24_pic, w, h, yuv420_pic)
实现了rgb像素到yuv像素的转换,具体实现过程如下所示。
int CTransPic::RGB24_to_YUV420(unsigned char* rgb24_pic, int w, int h, unsigned char* yuv420_pic)
{
unsigned char *YPtr, *UPtr, *VPtr, *RGBPtr;
//Init YUV buffer
memset(yuv420_pic, 0, w*h * 3 / 2);
YPtr = yuv420_pic;
UPtr = yuv420_pic + w*h;
VPtr = UPtr + w*h / 4;
unsigned char r, g, b, y, u, v;
for (int i = 0; i < h; i++)
{
RGBPtr = rgb24_pic + w*i * 3;
for (int j = 0; j < w; j++)
{
r = *(RGBPtr++);
g = *(RGBPtr++);
b = *(RGBPtr++);
y = (unsigned char)((66 * r + 129 * g + 25 * b + 128) >> 8) + 16;
u = (unsigned char)((-38 * r - 74 * g + 112 * b + 128) >> 8) + 128;
v = (unsigned char)((112 * r - 94 * g - 18 * b + 128) >> 8) + 128;
*(YPtr++) = clip_value(y, 0, 255);
if (i % 2 == 0 && j % 2 == 0){
*(UPtr++) = clip_value(u, 0, 255);
}
else{
if (j % 2 == 0){
*(VPtr++) = clip_value(v, 0, 255);
}
}
}
}
return 0;
}
其中,clip_value(u, 0, 255)
函数对转换后的像素进行了范围约束。
unsigned char CTransPic::clip_value(unsigned char pix, unsigned char min_val, unsigned char max_val)
{
if (pix < min_val){
return min_val;
}
else if (pix > max_val) {
return max_val;
}
else
{
return pix;
}
}
函数调用方法如下所示。
TranPic.simplest_rgb24_to_yuv420("4_1024x640.rgb", 1024, 640, 1, "output_yuv420_1024x640.yuv");
vooya播放器:https://download.csdn.net/download/whegqing/11519824
测试用例图:https://download.csdn.net/download/whegqing/11522107
版本声明:本文参考了雷神的文章,文中代码重新做了编写,亲测可用,欢迎转载交。
另外,测试用例图出自昵图网,如有侵权请告知。