Converting Between YUV and RGB

简介:

It is frequently necessary to convert between YUV pixel formats (used by the JPEG and MPEG compression methods) and RGB format (used by many hardware manufacturers.) The following formulas show how to compute a pixel's value in one format from the pixel value in the other format.

YUV format allows for higher compression rates without a proportionately high loss of data, as the U and V portions can be highly compressed and computed from the non- or lowly-compressed Y portion.

Computer RGB888, or full-scale RGB, uses 8 bits each for the red, green, and blue channels. Black is represented by R = G = B = 0, and white is represented by R = G = B = 255. The 4:4:4 YUV format uses 8 bits each for the Y, U, and V channels.

Converting RGB888 to YUV

The following formulas define the conversion from RGB to YUV:

Y = ( (  66 * R + 129 * G +  25 * B + 128) >> 8) +  16
U = ( ( -38 * R -  74 * G + 112 * B + 128) >> 8) + 128
V = ( ( 112 * R -  94 * G -  18 * B + 128) >> 8) + 128

These formulas produce 8-bit results using coefficients that require no more than 8 bits of (unsigned) precision. Intermediate results require up to 16 bits of precision.

Converting 8-bit YUV to RGB888

The following coefficients are used in conversion process:

C = Y - 16
D = U - 128
E = V - 128

Using the previous coefficients and noting that clip() denotes clipping a value to the range of 0 to 255, the following formulas provide the conversion from YUV to RGB:

R = clip(( 298 * C           + 409 * E + 128) >> 8)
G = clip(( 298 * C - 100 * D - 208 * E + 128) >> 8)
B = clip(( 298 * C + 516 * D           + 128) >> 8)

These formulas use some coefficients that require more than 8 bits of precision to produce each 8-bit result, and intermediate results require more than 16 bits of precision.

Note   All units range from 0 (zero) to 1.0 (one). In DirectDraw, they range from 0 to 255. Overflow and underflow can (and does) occur, and the results must be saturated.

To convert 4:2:0 or 4:2:2 YUV to RGB, convert the YUV data to 4:4:4 YUV, and then convert from 4:4:4 YUV to RGB.


 Last updated on Thursday, April 08, 2004

© 1992-2003 Microsoft Corporation. All rights reserved.

目录
相关文章
|
8天前
|
存储 编解码 监控
RGB 和 YUV 区别
【10月更文挑战第26天】RGB和YUV在色彩表示原理、数据存储方式、应用场景以及转换关系等方面都存在着明显的区别,它们各自在不同的领域发挥着重要的作用。
|
存储 编解码 Android开发
NV21、NV12、YV12、RGB、YUV、RGBA、RGBX8888等图像色彩编码格式区别
NV21、NV12、YV12、RGB、YUV、RGBA、RGBX8888都是常见的图像颜色编码格式,它们之间的主要区别在于色彩空间和数据排列方式。
208 0
|
6月前
|
存储 编解码 算法
关于YUV视频
关于YUV视频
146 0
|
存储 Web App开发 网络协议
YUV的plannar,packet及semi-planar格式及RGB
YUV的plannar,packet及semi-planar格式及RGB
202 0
YUV的plannar,packet及semi-planar格式及RGB
Fffmpeg:从AVFrame中由YUV获取RGB
Fffmpeg:从AVFrame中由YUV获取RGB
172 0
|
存储 算法 知识图谱
RGB与YUV相互转换
本文介绍YUV跟RGB互转的各种公式,以及推导原理
364 0
YUV 与 RGB的转换
RGB 转换成 YUV Y = (0.257 * R) + (0.504 * G) + (0.
5442 0
|
编解码 芯片