YUV格式初探

目录

一、 YUV起源

二、 YUV的类型

三、 YUV的采样和存储格式

四、 YUV的相关Enum

五、 YUV与RGB转换

六、 参考文献:

一、YUV起源

常见的颜色模型中,RGB主要用于电子系统里表达和显示颜色,CMYK印刷四色模式用于彩色印刷,而YUV是被欧洲电视系统所采用的一种颜色编码方法。

使用YUV的优点有两个:

一.YUV主要用于优化彩色视频信号的传输,使其向后兼容老式黑白电视,这一特性用在于电视信号上。

二.YUV是数据总尺寸小于RGB格式(但用YUV444的话,和RGB888一样都是24bits)

彩色,Y分量,V分量,U分量

二、YUV的类型

YUV细分的话有Y'UV,YUV,YCbCr,YPbPr等类型,其中YCbCr主要用于数字信号.

YCbCr 是在世界数字组织视频标准研制过程中作为ITU - R BT1601 建议的一部分, 其实是YUV经过Gamma的翻版。其中Y与YUV 中的Y含义一致, Cb , Cr 同样都指色彩, 只是在表示方法上不同而已。在YUV 家族中, YCbCr 是在计算机系统中应用最多的成员, 其应用领域很广泛,JPEG、MPEG,H264均采用此格式。

其中,Cr反映了RGB输入信号红色部分与RGB信号亮度值之间的差异,而Cb反映的是RGB输入信号蓝色部分与RGB信号亮度值之间的差异,此即所谓的色差信号

一般所讲的YUV大多是指YCbCr。以下用YUV指代YCbCr

图像生成RGB分量与YUV分量

三、YUV的采样和存储格式

  1. 采样

YUV的采样有许多种,常用的有444,422,420,411等。

用三个图来直观地表示采集的方式吧,以黑点表示采样该像素点的Y分量,以空心圆圈表示采用该像素点的UV分量。


YUV常用采样

1.YUV 4:4:4采样,每一个Y对应一组UV分量。
· 4:4:4 Formats, 24 Bits per Pixel
2.YUV 4:2:2采样,每两个Y共用一组UV分量。
· 4:2:2 Formats, 16 Bits per Pixel
3.YUV 4:2:0采样,每四个Y共用一组UV分量。
· 4:2:0 Formats, 12 Bits per Pixel

4:4:4 means no downsampling of the chroma channels.

4:2:2 means 2:1 horizontal downsampling, with no vertical downsampling. Every scan line contains four Y samples for every two U or V samples.

4:2:0 means 2:1 horizontal downsampling, with 2:1 vertical downsampling.

4:1:1 means 4:1 horizontal downsampling, with no vertical downsampling. Every scan line contains four Y samples for every U or V sample. 
4:1:1 sampling is less common than other formats, and is not discussed in detail in this article.
  1. 存储

YUV的存储格式有两大类:packed和planar,还有SemiPlanar。

对于packed的YUV格式,每个像素点的Y,U,V是连续交错存储的。

对于planar的YUV格式,先连续存储所有像素点的Y,紧接着存储所有像素点的U,随后是所有像素点的V。

对于SemiPlanar,则是先连续存储所有像素点的Y,再连续交错U和V。

根据不同的摆放顺序有如下几种常见的:

Packed方式

UYVY422(COLOR_FormatYUV422PackedPlanar)

其他的VYUY422,YUYV422,YVYU422只不过是数据矩阵中Y,U,V顺序差别:

VYUY422:  V1 Y1 U1 Y2  V2 Y3 U2 Y4 ...

YUYV422(YUY2): Y1 U1 Y2 V1   Y3 U2 Y4 V2 ...

YVYU422:  Y1 V1 Y2 U1    Y3 V2 Y4 U2 …
UYVY422

YUV420 Packet(COLOR_FormatYUV420PackedPlanar) 如下:
YUV420 packet每2X2的Y分量公用一个UV分量,并且将YUV打包到一个平面,如图所示

YUV420 Packet

Planar和SemiPlanar方式

YUV422P(COLOR_FormatYUV422Planar) 如下:
每两个连续的Y分量公用一个UV空间,Y分量空间后面跟U分量平面,然后为V分量平面

YUV422P

YUV422SP(COLOR_FormatYUV422SemiPlanar) 如下:
每两个连续的Y分量公用一个UV空间,前面是Y分量空间,后面是U,V交错分量平面

YUV422SP

YUV420P(COLOR_FormatYUV420Planar) 如下:

YUV420P

YUV420SP( COLOR_FormatYUV420SemiPlanar) 如下:

YUV420SP
其中YUV420P和YUV420SP根据U、V的顺序,又可分出2种格式。

YUV420P:U前V后即YUV420P,也叫I420,

V前U后,叫YV12(YV表示Y后面跟着V,12表示12bit)。

YUV420SP:U前V后叫NV12,

V前U后叫NV21。

数据排列如下:

I420: YYYYYYYY UU VV =>YUV420P

YV12: YYYYYYYY VV UU =>YUV420P

NV12: YYYYYYYY UVUV =>YUV420SP

NV21: YYYYYYYY VUVU =>YUV420SP

四、YUV的相关Enum

OpenNI2的原生像素格式定义如下:

typedef enum

{

// Depth

ONI_PIXEL_FORMAT_DEPTH_1_MM = 100,

ONI_PIXEL_FORMAT_DEPTH_100_UM = 101,

ONI_PIXEL_FORMAT_SHIFT_9_2 = 102,

ONI_PIXEL_FORMAT_SHIFT_9_3 = 103,

// Color

ONI_PIXEL_FORMAT_RGB888 = 200,

ONI_PIXEL_FORMAT_YUV422 = 201,

ONI_PIXEL_FORMAT_GRAY8 = 202,

ONI_PIXEL_FORMAT_GRAY16 = 203,

ONI_PIXEL_FORMAT_JPEG = 204,

ONI_PIXEL_FORMAT_YUYV = 205,

} OniPixelFormat;

AstraSDK的像素格式定义如下:

typedef enum {

    ASTRA_PIXEL_FORMAT_UNKNOWN = 0,

    ASTRA_PIXEL_FORMAT_DEPTH_MM = 100,

    // color layouts

    ASTRA_PIXEL_FORMAT_RGB888 = 200,

    ASTRA_PIXEL_FORMAT_YUV422 = 201,

    ASTRA_PIXEL_FORMAT_YUYV = 202,

    ASTRA_PIXEL_FORMAT_RGBA = 203,

    ASTRA_PIXEL_FORMAT_GRAY8 = 300,

    ASTRA_PIXEL_FORMAT_GRAY16 = 301,

    ASTRA_PIXEL_FORMAT_POINT = 400,

} astra_pixel_formats;

LINUX_VIDEODEV2(V4L2)的相关定义如下:

#define V4L2_PIX_FMT_YYUV    v4l2_fourcc('Y','Y','U','V') /* 16  YUV 4:2:2     */

#define V4L2_PIX_FMT_YUYV    v4l2_fourcc('Y','U','Y','V') /* 16  YUV 4:2:2     */

#define V4L2_PIX_FMT_UYVY    v4l2_fourcc('U','Y','V','Y') /* 16  YUV 4:2:2     */

#define V4L2_PIX_FMT_YUV422P v4l2_fourcc('4','2','2','P') /* 16  YVU422 planar */

#define V4L2_PIX_FMT_NV12    v4l2_fourcc('N','V','1','2') /* 12  Y/CbCr 4:2:0  */

#define V4L2_PIX_FMT_NV21    v4l2_fourcc('N','V','2','1') /* 12  Y/CrCb 4:2:0  */

#define V4L2_PIX_FMT_YVU420  v4l2_fourcc('Y','V','1','2') /* 12  YVU 4:2:0     */

#define V4L2_PIX_FMT_YUV420  v4l2_fourcc('Y','U','1','2') /* 12  YUV 4:2:0     */

微软的YUV Video Subtypes相关定义如下:

GUID Format Sampling Packed or planar Bits per channel
MEDIASUBTYPE_AYUV AYUV 4:4:4 Packed 8
MEDIASUBTYPE_YUY2 YUY2 4:2:2 Packed 8
MEDIASUBTYPE_UYVY UYVY 4:2:2 Packed 8
MEDIASUBTYPE_IMC1 IMC1 4:2:0 Planar 8
MEDIASUBTYPE_IMC3 IMC2 4:2:0 Planar 8
MEDIASUBTYPE_IMC2 IMC3 4:2:0 Planar 8
MEDIASUBTYPE_IMC4 IMC4 4:2:0 Planar 8
MEDIASUBTYPE_YV12 YV12 4:2:0 Planar 8
MEDIASUBTYPE_NV12 NV12 4:2:0 Planar 8

Android API的MediaCodecInfo.CodecCapabilities如下:

int COLOR_Format12bitRGB444
This constant was deprecated in API level 23. Use COLOR_Format24bitBGR888.  
int COLOR_Format16bitARGB1555
This constant was deprecated in API level 23. Use COLOR_Format32bitABGR8888.    
int COLOR_Format16bitARGB4444
This constant was deprecated in API level 23. Use COLOR_Format32bitABGR8888.    
int COLOR_Format16bitBGR565
This constant was deprecated in API level 23. Use COLOR_Format16bitRGB565.  
int COLOR_Format16bitRGB565
16 bits per pixel RGB color format, with 5-bit red & blue and 6-bit green component.    
int COLOR_Format18BitBGR666
This constant was deprecated in API level 23. Use COLOR_Format24bitBGR888.  
int COLOR_Format18bitARGB1665
This constant was deprecated in API level 23. Use COLOR_Format32bitABGR8888.    
int COLOR_Format18bitRGB666
This constant was deprecated in API level 23. Use COLOR_Format24bitBGR888.  
int COLOR_Format19bitARGB1666
This constant was deprecated in API level 23. Use COLOR_Format32bitABGR8888.    
int COLOR_Format24BitABGR6666
This constant was deprecated in API level 23. Use COLOR_Format32bitABGR8888.    
int COLOR_Format24BitARGB6666
This constant was deprecated in API level 23. Use COLOR_Format32bitABGR8888.    
int COLOR_Format24bitARGB1887
This constant was deprecated in API level 23. Use COLOR_Format32bitABGR8888.    
int COLOR_Format24bitBGR888
24 bits per pixel RGB color format, with 8-bit red, green & blue components.    
int COLOR_Format24bitRGB888
This constant was deprecated in API level 23. Use COLOR_Format24bitBGR888 or COLOR_FormatRGBFlexible.   
int COLOR_Format25bitARGB1888
This constant was deprecated in API level 23. Use COLOR_Format32bitABGR8888.    
int COLOR_Format32bitABGR8888
32 bits per pixel RGBA color format, with 8-bit red, green, blue, and alpha components. 
int COLOR_Format32bitARGB8888
This constant was deprecated in API level 23. Use COLOR_Format32bitABGR8888 Or COLOR_FormatRGBAFlexible.    
int COLOR_Format32bitBGRA8888
This constant was deprecated in API level 23. Use COLOR_Format32bitABGR8888 Or COLOR_FormatRGBAFlexible.    
int COLOR_Format8bitRGB332
This constant was deprecated in API level 23. Use COLOR_Format24bitBGR888.  
int COLOR_FormatCbYCrY
This constant was deprecated in API level 23. Use COLOR_FormatYUV422Flexible.   
int COLOR_FormatCrYCbY
This constant was deprecated in API level 23. Use COLOR_FormatYUV422Flexible.   
int COLOR_FormatL16
16 bits per pixel, little-endian Y color format.    
int COLOR_FormatL2
This constant was deprecated in API level 23. Use COLOR_FormatL8.   
int COLOR_FormatL24
This constant was deprecated in API level 23. Use COLOR_FormatL16.  
int COLOR_FormatL32
This constant was deprecated in API level 23. Use COLOR_FormatL16.  
int COLOR_FormatL4
This constant was deprecated in API level 23. Use COLOR_FormatL8.   
int COLOR_FormatL8
8 bits per pixel Y color format.    
int COLOR_FormatMonochrome
This constant was deprecated in API level 23. Use COLOR_Format24bitBGR888.  
int COLOR_FormatRGBAFlexible
Flexible 32 bits per pixel RGBA color format with 8-bit red, green, blue, and alpha components. 
int COLOR_FormatRGBFlexible
Flexible 24 bits per pixel RGB color format with 8-bit red, green and blue components.  
int COLOR_FormatRawBayer10bit
SMIA 10-bit Bayer format.   
int COLOR_FormatRawBayer8bit
SMIA 8-bit Bayer format.    
int COLOR_FormatRawBayer8bitcompressed
SMIA 8-bit compressed Bayer format. 
int COLOR_FormatSurface 
int COLOR_FormatYCbYCr
This constant was deprecated in API level 23. Use COLOR_FormatYUV422Flexible.   
int COLOR_FormatYCrYCb
This constant was deprecated in API level 23. Use COLOR_FormatYUV422Flexible.   
int COLOR_FormatYUV411PackedPlanar
This constant was deprecated in API level 23. Use COLOR_FormatYUV420Flexible.   
int COLOR_FormatYUV411Planar
This constant was deprecated in API level 23. Use COLOR_FormatYUV420Flexible.   
int COLOR_FormatYUV420Flexible
Flexible 12 bits per pixel, subsampled YUV color format with 8-bit chroma and luma components.  
int COLOR_FormatYUV420PackedPlanar
This constant was deprecated in API level 23. Use COLOR_FormatYUV420Flexible.   
int COLOR_FormatYUV420PackedSemiPlanar
This constant was deprecated in API level 23. Use COLOR_FormatYUV420Flexible.   
int COLOR_FormatYUV420Planar
This constant was deprecated in API level 23. Use COLOR_FormatYUV420Flexible.   
int COLOR_FormatYUV420SemiPlanar
This constant was deprecated in API level 23. Use COLOR_FormatYUV420Flexible.   
int COLOR_FormatYUV422Flexible
Flexible 16 bits per pixel, subsampled YUV color format with 8-bit chroma and luma components.  
int COLOR_FormatYUV422PackedPlanar
This constant was deprecated in API level 23. Use COLOR_FormatYUV422Flexible.   
int COLOR_FormatYUV422PackedSemiPlanar
This constant was deprecated in API level 23. Use COLOR_FormatYUV422Flexible.   
int COLOR_FormatYUV422Planar
This constant was deprecated in API level 23. Use COLOR_FormatYUV422Flexible.   
int COLOR_FormatYUV422SemiPlanar
This constant was deprecated in API level 23. Use COLOR_FormatYUV422Flexible.   
int COLOR_FormatYUV444Flexible
Flexible 24 bits per pixel YUV color format with 8-bit chroma and luma components.  
int COLOR_FormatYUV444Interleaved
This constant was deprecated in API level 23. Use COLOR_FormatYUV444Flexible.   
int COLOR_QCOM_FormatYUV420SemiPlanar
This constant was deprecated in API level 23. Use COLOR_FormatYUV420Flexible.   
int COLOR_TI_FormatYUV420PackedSemiPlanar
This constant was deprecated in API level 23. Use COLOR_FormatYUV420Flexible.   

COLOR_FormatYUV420Flexible

这里简单谈谈COLOR_FormatYUV420Flexible,YUV420Flexible并不是一种确定的YUV420格式,而是包含COLOR_FormatYUV411Planar, COLOR_FormatYUV411PackedPlanar, COLOR_FormatYUV420Planar, COLOR_FormatYUV420PackedPlanar, COLOR_FormatYUV420SemiPlanar和COLOR_FormatYUV420PackedSemiPlanar。

在API 21引入YUV420Flexible的同时,它所包含的这些格式都deprecated掉了。

五、YUV与RGB转换

Digital Y′CbCr (8 bits per sample) is derived from analog R'G'B' as follows:

将系数都除以255以后,得到以下公式:

1. <u>RGB 转换成 YUV</u>

2. 

3. Y = (0.257 * R) + (0.504 * G) + (0.098 * B) + 16

4. 

5. Cr = V = (0.439 * R) - (0.368 * G) - (0.071 * B) + 128

6. 

7. Cb = U = -( 0.148 * R) - (0.291 * G) + (0.439 * B) + 128

8. 

9. <u>YUV 转换成 RGB</u>

10. 

11. B = 1.164(Y - 16) + 2.018(U - 128)

12. 

13. G = 1.164(Y - 16) - 0.813(V - 128) - 0.391(U - 128)

14. 

15. R = 1.164(Y - 16) + 1.596(V - 128)

这些转换都可以使用SIMD指令集(比如NEON,MMX),来提高性能。

六、参考文献:

https://wiki.videolan.org/YUV/

https://msdn.microsoft.com/en-us/library/aa904813(VS.80).aspx

http://www.fourcc.org/yuv.php

https://en.wikipedia.org/wiki/YUV

https://en.wikipedia.org/wiki/YCbCr

https://developer.android.com/reference/android/media/MediaCodecInfo.CodecCapabilities.html

http://blog.csdn.net/jumper511/article/details/21719313

图侵删

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,332评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,508评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,812评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,607评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,728评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,919评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,071评论 3 410
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,802评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,256评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,576评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,712评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,389评论 4 332
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,032评论 3 316
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,798评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,026评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,473评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,606评论 2 350

推荐阅读更多精彩内容

  • 由于H.264等压缩算法都是在YUV的颜色空间上进行的,所有在进行压缩前,首先要进行颜色空间的转换。如果摄像头采集...
    眷卿三世阅读 13,559评论 2 6
  • 转自:http://www.cnblogs.com/azraelly/archive/2013/01/01/284...
    rickytang0阅读 862评论 0 1
  • 英孚家长: 您好! 孩子今天学习U6L5: 1.复习了所有水果及蔬菜的单词: 苹果,香蕉,橘子,桃子,李子,柠檬,...
    EmilyJia阅读 289评论 0 0
  • 分布式事务:不过是在一致性、吞吐量和复杂度之间,做一个选择Sagas给我的感觉式过于复杂,要提供正反操作,这个是个...
    Jeff阅读 205评论 0 0
  • 转眼间一年的时光在指缝间悄悄流去,看着小幺鸡那青涩的脸庞,仿佛回忆停留在去年前的。去年前的自己懵懂,青涩又...
    火阑珊阅读 376评论 0 1