colour.matrix_YCbCr#

colour.matrix_YCbCr(K: NDArrayFloat = WEIGHTS_YCBCR['ITU-R BT.709'], S: NDArrayFloat = SCALES_YCBCR["Y'CbCr"], bits: int = 8, is_legal: bool = False, is_int: bool = False) NDArrayFloat[source]#

Compute the Y’CbCr to R’G’B’ matrix for the specified weights, bit-depth, range legality and representation.

The related offset for the R’G’B’ to Y’CbCr matrix can be computed with the colour.offset_YCbCr() definition.

Parameters:
  • K (NDArrayFloat) – Luma weighting coefficients of red and blue. See colour.WEIGHTS_YCBCR for presets. Default is (0.2126, 0.0722), the weightings for ITU-R BT.709.

  • S (NDArrayFloat) – Chroma scaling coefficients of Cb and Cr. See colour.SCALES_YCBCR for presets. Default is (0.5, 0.5), the scaling for Y’CbCr.

  • bits (int) – Bit-depth of the Y’CbCr colour encoding ranges array.

  • is_legal (bool) – Whether the Y’CbCr colour encoding ranges array is legal.

  • is_int (bool) – Whether the Y’CbCr colour encoding ranges array represents int code values.

Returns:

Y’CbCr matrix.

Return type:

numpy.ndarray

Examples

>>> matrix_YCbCr()
array([[ 1.0000000...e+00, ...,  1.5748000...e+00],
       [ 1.0000000...e+00, -1.8732427...e-01, -4.6812427...e-01],
       [ 1.0000000...e+00,  1.8556000...e+00, ...]])
>>> matrix_YCbCr(K=WEIGHTS_YCBCR["ITU-R BT.601"])
array([[ 1.0000000...e+00, ...,  1.4020000...e+00],
       [ 1.0000000...e+00, -3.4413628...e-01, -7.1413628...e-01],
       [ 1.0000000...e+00,  1.7720000...e+00, ...]])
>>> matrix_YCbCr(is_legal=True)
array([[ 1.1643835...e+00, ...,  1.7927410...e+00],
       [ 1.1643835...e+00, -2.1324861...e-01, -5.3290932...e-01],
       [ 1.1643835...e+00,  2.1124017...e+00, ...]])

Computing the Y’UV to R’G’B’ matrix:

>>> matrix_YCbCr(S=SCALES_YCBCR["Y'UV"])
array([[ 1.0000000...e+00, ...,  1.2803252...e+00],
       [ 1.0000000...e+00, -2.1482141...e-01, -3.8058884...e-01],
       [ 1.0000000...e+00,  2.1279816...e+00, ...]])

Computing the R’G’B’ to Y’UV matrix:

>>> np.linalg.inv(matrix_YCbCr(S=SCALES_YCBCR["Y'UV"]))
...
array([[ 0.2126...    ,  0.7152...    ,  0.0722...    ],
       [-0.0999068..., -0.3360931...,  0.436...     ],
       [ 0.615...    , -0.5586080..., -0.0563919...]])

Matching the default output of the colour.RGB_to_YCbCr() is done as follows:

>>> from colour.algebra import vecmul
>>> from colour.utilities import as_int_array
>>> RGB = np.array([1.0, 1.0, 1.0])
>>> RGB_to_YCbCr(RGB)
array([0.9215686..., 0.5019607..., 0.5019607...])
>>> YCbCr = vecmul(np.linalg.inv(matrix_YCbCr(is_legal=True)), RGB)
>>> YCbCr += offset_YCbCr(is_legal=True)
>>> YCbCr
array([0.9215686..., 0.5019607..., 0.5019607...])

Matching the int output of the colour.RGB_to_YCbCr() is done as follows:

>>> RGB = np.array([102, 0, 51])
>>> RGB_to_YCbCr(RGB, in_bits=8, in_int=True, out_bits=8, out_int=True)
...
array([ 38, 140, 171])
>>> YCbCr = vecmul(np.linalg.inv(matrix_YCbCr(is_legal=True)), RGB)
>>> YCbCr += offset_YCbCr(is_legal=True, is_int=True)
>>> as_int_array(np.around(YCbCr))
...
array([ 38, 140, 171])