YUV & RGB

 RGB和YUV都是色彩空间,用于表示颜色,两者可以相互转化。   YUV(亦称YCrCb)是被欧洲电视系统所采用的一种颜色编码方法(属于PAL)。YUV主要用于优化彩色视频信号的传输,使其向后兼容老式黑白电视。与R GB视频信号传输相比,它最大的优点在于只需占用极少的带宽(RGB要求三个独立的视频信号同时传输)。
     中"Y"表示明亮度(Lumina nce或Luma),也就是灰阶值;是个基带信号。而"U"和"V"表示的则是色度(Chrominance或Chroma),作用是描述影像色彩及饱和度,用于指定像素的颜色。U和V不是基带信号,它俩是被正交调制了的。
     "亮度"是通过RGB输入信号来创建的,方法是将RGB信号的特定部分叠加到一起。"色度"则定义了颜色的两个方面-色调与饱和度,分别用Cr和CB来表示。其中,Cr反映了RGB输入信号红色部分与RGB信号亮度值之间的差异。而CB反映的是RGB输入信号蓝色部分与RGB信号亮度值之同的差异。。通过运算,YUV三分量可以还原出R(红),G(绿),B(兰)。

一、和rgb之间换算公式的差异
yuv<-->rgb
Y'= 0.299*R' + 0.587*G' + 0.114*B'
U'= -0.147*R' - 0.289*G' + 0.436*B' = 0.492*(B'- Y')
V'= 0.615*R' - 0.515*G' - 0.100*B' = 0.877*(R'- Y')
R' = Y' + 1.140*V'
G' = Y' - 0.394*U' - 0.581*V'
B' = Y' + 2.032*U'


yCbCr<-->rgb
Y’ = 0.257*R' + 0.504*G' + 0.098*B' + 16
Cb' = -0.148*R' - 0.291*G' + 0.439*B' + 128
Cr' = 0.439*R' - 0.368*G' - 0.071*B' + 128
R' = 1.164*(Y’-16) + 1.596*(Cr'-128)
G' = 1.164*(Y’-16) - 0.813*(Cr'-128) - 0.392*(Cb'-128)
B' = 1.164*(Y’-16) + 2.017*(Cb'-128)
Note: 上面各个符号都带了一撇,表示该符号在原值基础上进行了gamma correction

二、来源上的差异
yuv色彩模型来源于rgb模型,该模型的特点是将亮度和色度分离开,从而适合于图像处理领域。

 

 

 

第一个公式是YUV转换RGB(范围0-255)时用的,第二个公式是用在YUV(601)也成为YCbCr转换RGB(范围0-255)时用的。

1.Y =   0.299R + 0.587G + 0.114B
   U = -0.147R - 0.289G + 0.436B
   V = 0.615R - 0.515G - 0.100B

   R = Y             + 1.14V
   G = Y - 0.39U - 0.58V
   B = Y + 2.03U

2.B= 1.164 * (Y - 16) + 2.018 * (U - 128)
   G= 1.164 * (Y - 16) -   0.38 * (U - 128) - 0.813 * (V - 128)
   R= 1.164 * (Y - 16)                               + 1.159 * (V - 128)

 

以上转帖来自 http://hi.baidu.com/hongjun009ok/blog/item/7c1a7636217796350b55a9c0.html

// ===============================================================================

下面参考Keith Jack. Video Demystified. ISBN 1-878707-09-4. 

Prior to the development of fast SIMD floating-point processors, most digital implementations of RGB->Y'UV used integer math, in particular fixed-point approximations. In the following examples, the operator "a >> b" denotes an integer division by a power of two, which is equivalent to a right-shift of a by b bits.

Traditional 8 bit representation of Y'UV with unsigned integers uses the following

Basic Transform
Scale down to 8 bits with rounding
Y' = (Y' + 128) > > 8
U = (U + 128) > > 8
V = (V + 128) > > 8
Shift values
Y' + = 16
U + = 128
V + = 128

Y' values are conventionally shifted and scaled to the range [16, 235] rather than using the full range of [0, 255]. This confusing practice derives from the MPEG standards and explains why 16 is added to Y' and why the Y' coefficients in the basic transform sum to 220 instead of 255. U and V values, which may be positive or negative, are summed with 128 to make them always positive.

 

下面是自己推算的,方便SIMD优化。

由此推算YUYV ---> RGB

Shift values
Y' - = 16
U - = 128
V - = 128

Basic Transform

          R' = 1192 * Y' + 1634 * V

G' = 1192 * Y' - 832 * V + 401 * U
B' = 1192 * Y' + 2064 * U

Scale down to 8 bits with rounding

          R' = R'>>10

          G' = G'>>10

          B' = B'>>10

 

 

你可能感兴趣的:(Math,优化,Integer,basic,图像处理,Standards)