NV12 format defines specific color channels ordering of YUV color space with 420 subsampling.
NV12 format is mostly used in video encoding/decoding pipeline.
NV12 is a biplanar format with a full sized Y plane followed by a single chroma plane with weaved U and V values. NV21 is the same but with weaved V and U values. The 12 in NV12 refers to 12 bits per pixel. NV12 has a half width and half height chroma channel, and therefore is a 420 subsampling.
In context of NV12, YUV format is mainly referred as YCbCr color space.
NV12 elements are 8 bits per element (uint8
type).
In the context of the post, YUV elements are in "limited range" standard: Y range is [16, 235], U,V range is [16, 240].
sRGB (standard Red Green Blue) is a standard color space used by PC systems.
In the context of the post, sRGB color components range is [0, 255] (uint8
type).
RGB elements ordering is not relevant to the post (assume 3 color planes).
There are currently at least 2 possible YCbCr formats applying NV12:
Example for NV12 elements ordering:YYYYYY
YYYYYY
UVUVUV
RGB to NV12 conversion can be described by the following stages:
- Color space conversion - convert from sRGB to YUV color space.
- Chroma downsampling - shrink U,V channels by a factor of x2 in each axis (converting from YUV444 to YUV420).
- Chroma elements interleaving - arrange U,V elements as U,V,U,V...
Following figure illustrates the conversion stages applying image size of 6x6 pixels:
How can we convert sRGB to NV12 using NumPy?
Note:
The question refers Python implementation that demonstrates the conversion process (post is not intended for existing function like OpenCV implementation).