RGB图像转换成YCbCr图像,rgb2ycbcr的使用,转换参数

原理

YCbCr到RGB的转换

 Y’ = 0.257*R' + 0.504*G' + 0.098*B' + 16

 Cb' = -0.148*R' - 0.291*G' + 0.439*B' + 128

 Cr' = 0.439*R' - 0.368*G' - 0.071*B' + 128

函数使用举例

注意事项

输入:0-1的浮点型numpy矩阵代表RGB空间

输出:0-255的浮点型numpy矩阵代表YCbCr空间

image.shape为[W*H*C]的RGB图像

使用情况1

当image每一个像素点的取值范围为0-255时候

需要先转换成0-1,因为在下面的源码分析中可以看到他给的相乘系数是65.481 … -18.214 这些是针对已经归一化到0-1的数值来讲的系数

否则这个系数和后面的16,128等偏移就不会对应了

from skimage.color import rgb2ycbcr
image_rgb = image_rgb/255.0#此时可以转换成浮点型
image_ycbcr = rgb2ycbcr(image_rgb)
此时输出为0-255的浮点型YCbCr空间图像

使用情况2

当image每一个像素点的取值范围为0-1时候

from skimage.color import rgb2ycbcr
image_ycbcr = rgb2ycbcr(image_rgb)
此时输出为0-255的浮点型YCbCr空间图像

源码解释说明

# 空间转换所需要的使用到的参数
ycbcr_from_rgb = np.array([[    65.481,   128.553,    24.966],
                           [   -37.797,   -74.203,   112.0  ],
                           [   112.0  ,   -93.786,   -18.214]])
# 通用做图像空间变换的转换函数
def _convert(matrix, arr):
    """Do the color space conversion.
    Parameters
    ----------
    matrix : array_like
        The 3x3 matrix to use.
    arr : (..., 3) array_like
        The input array. Final dimension denotes channels.
    Returns
    -------
    out : (..., 3) ndarray
        The converted array. Same dimensions as input.
    """
    arr = _prepare_colorarray(arr)

    return arr @ matrix.T.astype(arr.dtype)

# 将RGB图像通过转换矩阵转换成YCbCr
def rgb2ycbcr(rgb):
    """RGB to YCbCr color space conversion.
    Parameters
    ----------
    rgb : (..., 3) array_like
        The image in RGB format. Final dimension denotes channels.
    Returns
    -------
    out : (..., 3) ndarray
        The image in YCbCr format. Same dimensions as input.
    Raises
    ------
    ValueError
        If `rgb` is not at least 2-D with shape (..., 3).
    Notes
    -----
    Y is between 16 and 235. This is the color space commonly used by video
    codecs; it is sometimes incorrectly called "YUV".
    References
    ----------
    .. [1] https://en.wikipedia.org/wiki/YCbCr
    """
    arr = _convert(ycbcr_from_rgb, rgb)
    arr[..., 0] += 16
    arr[..., 1] += 128
    arr[..., 2] += 128
    return arr

LAST、参考文献

YCbCr与YUV的区别_machh的专栏-CSDN博客_yuv和ycbcr的区别

scikit-image/simple_metrics.py at main · scikit-image/scikit-image · GitHub

scikit-image/colorconv.py at main · scikit-image/scikit-image · GitHub

  • 5
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值