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).
Converting sRGB to NV12 format using NumPy
The purpose of the post is demonstrating the conversion process.
The Python implementation below uses NumPy, and deliberately avoids using OpenCV.
RGB to NV12 conversion stages:
Use sRGB to YCbCr conversion formula.
Multiply each RGB triple by 3x3 conversion matrix, and add a vector of 3 offsets.
The post shows both BT.709 and BT.601 conversions (the only difference is the coefficients matrix).
The implementation resizes U,V by factor of 0.5 in each axis using bi-linear interpolation.
Note: bi-linear interpolation is not the optimal downsampling method, but it's usually good enough.
Instead of using
cv2.resize
, code uses average of every 2x2 pixels (result is equivalent to bi-linear interpolation).Note: implementation fails in case input resolution is not even in both dimensions.
Implemented by array indexing manipulation.
Here is a Python code sample for converting RGB to NV12 standard:
Sample RGB input image:

NV12 Result (displayed as Grayscale image):

Testing:
For testing we convert the same input image (
rgb_input.png
) to NV12 format using FFmpeg (command line tool), and compute the maximum absolute difference between the two conversions.The test assumes that FFmpeg is in the execution path (in Windows we may place ffmpeg.exe at the same folder as the Python script).
The following shell command, converts
rgb_input.png
to NV12 format with BT.709 color standard:ffmpeg -y -i rgb_input.png -vf "scale=flags=fast_bilinear:out_color_matrix=bt709:out_range=tv:dst_format=nv12" -pix_fmt nv12 -f rawvideo nv12_ffmpeg.raw
Note:
fast_bilinear
interpolation gives the best results with the specific input image - applies bilinear interpolation when downscalingU
andV
.The following Python code compares
nv12_ffmpeg.raw
withnv12_ffmpeg.raw
:For the specific input image the maximum difference is
2
or3
(almost identical).For other input images, the difference is larger (probably due to wrong FFmpeg arguments).