Currently I'm working with some YUV422p-formatted pics. I read the documentations and tried running python codes below:
print([i for i in dir(cv2) if i.startswith("COLOR_YUV")])
And I found no support for YUV422p format. Does anybody know the reason why opencv has no support for such format, or if there is any other python-based libraries that provides such support?
We may use PyAV library.
PyAV is a Pythonic binding for the FFmpeg libraries.
FFmpeg supports YUV422 Planar picture format, and PyAV also supports it.
Unlike other FFmpeg bindings, PyAV is relatively low-level - instead of using FFmpeg CLI, it allows more low level functionalities that supported by the C interface of FFmpeg.
libswscale is "sub module" of FFmpeg that supports multiple color formats conversions.
PyAV exposes some of the capabilities of libswscale.
Start by creating raw input file in yuv422p pixel format, using FFmpeg CLI.
Execute the following shell command for building sample input (used for testing):
ffmpeg -y -f lavfi -i testsrc=192x108:rate=1:duration=10 -vf "scale=out_color_matrix=bt709:out_range=tv" -pix_fmt yuv422p -f rawvideo input.yuv422pThe next example, uses PyAV for converting from raw yuv422p to BGR pixel format:
Create a
VideoFrameobject:Assume
y,uandvare 3 color planes (yis full resolution, anduandvare half resolution in the horizontal axis).The planes may be stored in NumPy arrays or bytes arrays (multiple buffer formats are supported).
Update the 3 planes of
frame:Convert the frame to BGR format using reformat method, and convert to NumPy array:
The following code sample reads
yuv422praw data from file, converts to BRG using PyAV (and shows the output using OpenCV):Notes:
Installing PyAV may be problematic (at least when using Windows).
Instead of using
pip install, I downloaded awhlfile from here (and usedpip installfor installing the wheel).I assumed that the YUV applies BT.709 standard (and applies "TV range" -
Yrange is [16, 235] not [0, 255]).For correct conversion, you have to know the exacts standard of your raw data.