tensorflow rgb2yuv结果与opencv不一致替换

原因:
tensorflow使用的转换规范和OpenCV使用的不一致,使用OpenCV的转换矩阵即可实现一致的转换结果。

def rgb_to_yuv(images):
    rgb_to_yuv_kernel = [[0.299, -0.169, 0.500],
                         [0.587, -0.331, -0.419],
                         [0.114, 0.500, -0.081]]
    images = tf.convert_to_tensor(images, name='images')
    kernel = tf.convert_to_tensor(rgb_to_yuv_kernel, dtype=images.dtype, name='kernel')
    ndims = images.get_shape().ndims
    res = tf.tensordot(images, kernel, axes=[[ndims - 1], [0]])
    return tf.clip_by_value(res, 0.0, 1.0)
    # return tf.tensordot(images, kernel, axes=[[ndims - 1], [0]]) + [0, 0.5, 0.5]

posted @ 2021-06-04 15:14  wioponsen  阅读(163)  评论(0编辑  收藏  举报