uyvy422到RGB888转换

在嵌入式系统中由于摄像头彩集出来的数据一般会采用YUV的格式,但是framebuffer一般只接收RGB的数据,因此需要YUV到RGB颜色空间的转换。

YUV到RGB的转换思路一般是提到Y,U,V分量按照标准的转换矩阵进行换算,以YUV422I(排列方式为uyvy...序列)为例:


//输入YUV422I buffer数据,输出RGB buffer数据;
static int UYVY422ToRGB888(const unsigned char *src_buffer, int w, int h, const unsigned char *des_buffer)
{
	unsigned char *yuv, *rgb;
	unsigned char u, v, y1, y2;

	yuv = src_buffer;
	rgb = des_buffer;

	if (yuv == NULL || rgb == NULL)
	{
		printf ("error: input data null!\n");	
		return -1;
	}

	int size = w * h;

	for(int i = 0; i < size; i += 2)
	{
		y1 = yuv[2*i + 1];
		y2 = yuv[2*i + 3];
		u = yuv[2*i];
		v = yuv[2*i + 2];

#if 0
		rgb[3*i]     = (unsigned char)(y1 + 1.402*(u - 128));
		rgb[3*i + 1] = (unsigned char)(y1 - 0.344*(u - 128) - 0.714*(v - 128));
		rgb[3*i + 2] = (unsigned char)(y1 + 1.772*(v - 128));

		rgb[3*i + 3] = (unsigned char)(y2 + 1.375*(u - 128));
		rgb[3*i + 4] = (unsigned char)(y2 - 0.344*(u - 128) - 0.714*(v - 128));
		rgb[3*i + 5] = (unsigned char)(y2 + 1.772*(v - 128));
#endif 
	
		//为提高性能此处用移位运算;
		rgb[3*i]     = (unsigned char)(y1 + (u - 128) + ((104*(u - 128))>>8));
		rgb[3*i + 1] = (unsigned char)(y1 - (89*(v - 128)>>8) - ((183*(u - 128))>>8));
		rgb[3*i + 2] = (unsigned char)(y1 + (v - 128) + ((199*(v - 128))>>8));

		rgb[3*i + 3] = (unsigned char)(y2 + (u - 128) + ((104*(u - 128))>>8));
		rgb[3*i + 4] = (unsigned char)(y2 - (89*(v - 128)>>8) - ((183*(u - 128))>>8));
		rgb[3*i + 5] = (unsigned char)(y2 + (v - 128) + ((199*(v - 128))>>8));
	}
	
	return 0;
}

参考资料: wiki


你可能感兴趣的:(视频图像)