Skip to content

Instantly share code, notes, and snippets.

@robin-ai-ml
robin-ai-ml / rgb2yuv_yuv2rgb.py
Created April 27, 2025 06:43 — forked from awoimbee/rgb2yuv_yuv2rgb.py
OpenCV's RGB2YUV & YUV2RGB conversions for Numpy, for values between [0, 1] or [0, 255]
# The coefficients were taken from OpenCV https://github.com/opencv/opencv
# I'm not sure if the values should be clipped, in my (limited) testing it looks alright
# but don't hesitate to add rgb.clip(0, 1, rgb) & yuv.clip(0, 1, yuv)
#
# Input for these functions is a numpy array with shape (height, width, 3)
# Change '+= 0.5' to '+= 127.5' & '-= 0.5' to '-= 127.5' for values in range [0, 255]
def rgb2yuv(rgb):
m = np.array([
[0.29900, -0.147108, 0.614777],