RGB转YUV420

参考:点击打开链接

图像RGB数据转为YUV420程序:

void RGB24_To_YUV420(const unsigned char *img_r,const unsigned char *img_g, const unsigned char *img_b, int width, int height, unsigned char *img_yuv)
{
	int i, j;

	unsigned char *buf_y;
	unsigned char *buf_u;
	unsigned char *buf_v;



	buf_y = img_yuv;
	buf_u = img_yuv + (width * height);
	buf_v = buf_u + ((width * height >> 2));

	unsigned char pos;
	for (i = 0; i < height; i++)
	{
		for (j = 0; j < width; j++)
		{

			unsigned char B = img_b[i*width + j];
			unsigned char G = img_g[i*width + j];
			unsigned char R = img_r[i*width + j];

#ifdef DEBUG
			if (i==0 && j==width-1)
			printf("R=%x,G=%x,B=%x\n", R, G, B);
#endif

			//rgb to yuv 
			unsigned char Y = (unsigned char)((66 * R + 129 * G + 25 * B + 128) >> 8) + 16;
			*(buf_y++) = Clip_0_255(Y);
			//down sample 1/2 U at even line
			if (i%2 == 0 && j%2 == 0)
			{
			   unsigned char U = (unsigned char)((-38 * R - 74 * G + 112 * B + 128) >> 8) + 128;
			   *(buf_u++) = Clip_0_255(U);

			}
			//down sample 1/2 V at odd line
			else	
			{ 
				if (j % 2 == 0)
				{
				unsigned char V = (unsigned char)((112 * R - 94 * G - 18 * B + 128) >> 8) + 128;
				*(buf_v++) = Clip_0_255(V);
				}

			}

		
		}
	}
}

### RGBYUV420格式 #### 换原理 RGB颜色空间表示红、绿、蓝三个通道的颜色信息,而YUV是一种亮度-色度色彩模型。其中Y代表亮度分量,U和V分别代表蓝色差和红色差两个色度分量。当从RGB换至YUV420时,不仅涉及到色彩空间变换还涉及采样率的变化。 对于每种像素,在标准ITU-R BT.601下由如下公式完成RGBYUV的线性映射[^1]: \[ Y = 0.299R + 0.587G + 0.114B \] \[ U = -0.1687R - 0.3313G + 0.5B + 128 \] \[ V = 0.5R - 0.4187G - 0.0813B + 128 \] 在YUV420P计划中,亮度(Y)保持全分辨率存储;然而为了减少带宽需求,色度(U,V)则采用子抽样的方式处理——即每隔两行取一次样本,并在同一行内也隔列抽取,使得最终U/V平面只有原图四分之一大小的数据量. #### 实现方法 下面给出一段Python代码来展示这一过程: ```python import numpy as np def rgb_to_yuv420(rgb_image): height, width, _ = rgb_image.shape # Convert from RGB to YUV using the formulas above. yuv_image = np.zeros((height, width, 3), dtype=np.uint8) r = rgb_image[:, :, 0].astype(np.float32) g = rgb_image[:, :, 1].astype(np.float32) b = rgb_image[:, :, 2].astype(np.float32) yuv_image[:,:,0] = (0.299 * r + 0.587 * g + 0.114 * b).clip(0, 255).astype(np.uint8) yuv_image[:,:,1] = ((-0.1687 * r - 0.3313 * g + 0.5 * b + 128)).clip(0, 255).astype(np.uint8) yuv_image[:,:,2] = ((0.5 * r - 0.4187 * g - 0.0813 * b + 128)).clip(0, 255).astype(np.uint8) # Downsample chroma channels by a factor of two both horizontally and vertically. downsampled_u = cv2.resize(yuv_image[:,:,1], dsize=(width//2, height//2)) downsampled_v = cv2.resize(yuv_image[:,:,2], dsize=(width//2, height//2)) return { 'y': yuv_image[:,:,0], 'u': downsampled_u, 'v': downsampled_v } ``` 此函数接收一个形状为(height,width,channels)的numpy数组作为输入参数`rgb_image`,并返回字典形式的结果{'y', 'u', 'v'},其中'y'对应完整的亮度层,'u','v'则是经过降采样的色度层.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值