YUV基础

YUV基础介绍:

YUV在做手机图像或者视频处理的时候会经常用到的一个格式,用此文来记录YUV相关介绍,让项目中用到YUV不再对这个概念懵逼。

YUV这个名名词是什么意思的:参考维基百科:

The YUV model defines one luminance component (Y) meaning physical linear-space brightness, and two chrominance components, called U (blue projection) and V (red projection) respectively. It can be used to convert to and from the RGB model, and with different color spaces.

链接:
外链YUV维基百科介绍:https://en.wikipedia.org/wiki/YUV
国内可访问:https://encyclopedia.thefreedictionary.com/YUV

YUV 作为一种图像编码格式,Y表示亮度分量,UV表示色度信息:U表示蓝色部分投影,V表示红色部分投影。

不同采样YUV格式的区别

YUV在日常使用的时候会有很多格式,YUV420,YUV444,YUV422,NV21,NV12,这些格式的区别的什么呢?
下图是一张采样图:
**YUV444**
参考:https://en.wikipedia.org/wiki/Chroma_subsampling
上面的图像中展示了不同的数字所表示的采样格式的不同:

  • YUV4444:每一个Y分量对应一个像素,每个Y分别对应单独的U和V,因此YUV444每个像素占用的图片大小为 8bit+8bit+8bit=24bit(3byte)。

  • YUV422:水平方向上每两个Y分量对应一对UV分量,则每个像素占用的大小就是8bit+4bit+4bit=16bit。(1byte+1/2byte+1/2byte=2byte)

  • YUV420:420在水平和垂直方向都进行了采样,每一个Y分量对应1/4哥UV分量,因此表示一个像素需要的大小为1byte+1/4byte+1/4byte=1.5byte

    参考链接:https://en.wikipedia.org/wiki/YUV#Y%E2%80%B2UV420p_(and_Y%E2%80%B2V12_or_YV12)_to_RGB888_conversion

  • I420格式:
    I420是一种yuv420采样的数据格式,存储的时候按照y、u、v的顺序依次存储
    YUV 4:2:0 (I420/J420/YV12)
    I420
    It has the luma “luminance” plane Y first, then the U chroma plane and last the V chroma plane.
    The two chroma planes (blue and red projections) are sub-sampled in both the horizontal and vertical dimensions by a factor of 2. That is to say, for a 2×2 square of pixels, there are 4 Y samples but only 1 U sample and 1 V sample.
    This format requires 4×8+8+8=48 bits per 4 pixels, so its depth is 12 bits per pixel.

  • YV12格式:
    YV12格式也是一种YUV420采样格式使用plane格式存储,只不过在存储时按照Y,V,U顺序存储。
    YV12
    YV12 is exactly like I420, but the order of the U and V planes is reversed. In the name, “YV” refers to the plane order: Y, then V (then U). “12” refers to the pixel depth: 12-bits per pixel as for I420.

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

  • NV21和NV12格式:
    NV21和NV12是YUV420编码方式的一种,采样使用4:2:0的方式进行采样,只不过在存储的时候使用UV交错存储,是一种YUV420sp格式。只不过在UV分量的保存上顺序不同
    NV21:存储顺序是先存Y,再VU交替存储,NV21存储顺序是先存Y值,再VU交替存储:YYYYVUVUVU
    NV12:存储顺序是先存Y值,再UV交替存储:YYYYUVUVUV,以 4 X 4 图片为例子,占用内存为 4 X 4 X 3 / 2 = 24 个字节

参考:https://blog.csdn.net/byhook/article/details/84037338

为什么要使用YUV格式呢?

YUV相比于RGB的优势
数据量减少:RGB一个像素需要3byte,RGB每一个数据都需要1byte(8bit),一张长width,高height的图片,需要的空间为widthheight3byte,而对于YUV420格式,所需要的空间就为widthheight1.5byte,占用空间减少了一半。
向前兼容黑白电视:YUV中的Y分量可以兼容之前不支持彩色画面的设备。
不足:
采样带来图像画质的降低

YUV的存储方式
  • Planar (平面格式):y, u, v使用三个平面存储 ,例如:第0个平面表示y分量数据,第1个平面表示u分量数据,第2个平面表示v分量数据,这种存储名称以p结尾,例如:420p。
  • Semi-Planar:Y分量在单独的平面中存放,uv分量在一个平面中交错存放,这种存储名称以sp结尾,420sp。
  • Packed: YUV数3个分量全部交错存储。Y读取他周围的UV分量数据,以YUY2为例,存储方式为:Y0U0Y1V0 Y2U1Y3V1。
Android中的YUV_420_888

This format is a generic YCbCr format, capable of describing any 4:2:0 chroma-subsampled planar or semiplanar buffer (but not fully interleaved), with 8 bits per color sample.
上面的格式是一种通用的YU(cb)V(cr)格式,可以用来表示任何420采样的平面或者半平面数据,但是不能用来表示YUV都是交错(不支持Packed格式的数据)。

这里如何区分是平面还是半平面的数据的呢,可以使用plane属性中的getPixelStride去区分,1表示是plane因为相同类型数据存储是相邻的,2的话就表示相同数据会间隔存储,表示存储是交错的,即sp格式。

plane #0 is always Y, plane #1 is always U (Cb), and plane #2 is always V (Cr).
0平面表示Y平面,1平面表示U平面,2平面表示V平面。
The Y-plane is guaranteed not to be interleaved with the U/V planes (in particular, pixel stride is always 1
Y平面不会和UV平面交错,同时Y平面的Pixel stride总是1。
The U/V planes are guaranteed to have the same row stride and pixel stride (in particular, uPlane.getRowStride() == vPlane.getRowStride() and uPlane.getPixelStride() == vPlane.getPixelStride(); ).
同时U/V平面的rowstride和pixelstride相同。
名词解释:

  • pixelStride:表示同一个颜色分量之间的步长。取值1的话那同一个颜色分量的index为0、1、2、3、4、…,取值为2的话那同一个颜色分量的index为0、2、4、6。
  • rowStride:表示该平面每行数据的宽度。(byte为单位)

在ARCore和华为ArEngine获取相机数据的时候,返回的数据格式就是上面的YUV_420_888格式。
Android中如果pixelStride的size时2,那么格式就是YUV420SP,420sp中获取到的数据uv分量交替存储。

附录:

获取到的三个平面plane[0]+plane[1]就是NV12,plane[0]+plane[2]就是NV21:
https://blog.csdn.net/lbknxy/article/details/54633008
根据pixelstride区分存储方式,解析uv数据:
https://blog.csdn.net/weekend_y45/article/details/125079916

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值