python rgb转yuv_如何在tensorflow中将RGB转换为YUV?

如何使用tensorflow将RGB图像转换为YUV?

这是一个RGB到LAB的示例实现,但是我很难为YUV颜色空间定义相同的实现。我尝试分别使用YUV/RGB矩阵的最后一部分,但得到尺寸误差:# based on https://github.com/torch/image/blob/9f65c30167b2048ecbe8b7befdc6b2d6d12baee9/generic/image.c

def rgb_to_lab(srgb):

with tf.name_scope("rgb_to_lab"):

srgb = check_image(srgb)

srgb_pixels = tf.reshape(srgb, [-1, 3])

with tf.name_scope("srgb_to_xyz"):

linear_mask = tf.cast(srgb_pixels <= 0.04045, dtype=tf.float32)

exponential_mask = tf.cast(srgb_pixels > 0.04045, dtype=tf.float32)

rgb_pixels = (srgb_pixels / 12.92 * linear_mask) + (((srgb_pixels + 0.055) / 1.055) ** 2.4) * exponential_mask

rgb_to_xyz = tf.constant([

# X Y Z

[0.412453, 0.212671, 0.019334], # R

[0.357580, 0.715160, 0.119193], # G

[0.180423, 0.072169, 0.950227], # B

])

xyz_pixels = tf.matmul(rgb_pixels, rgb_to_xyz)

# https://en.wikipedia.org/wiki/Lab_color_space#CIELAB-CIEXYZ_conversions

with tf.name_scope("xyz_to_cielab"):

# convert to fx = f(X/Xn), fy = f(Y/Yn), fz = f(Z/Zn)

# normalize for D65 white point

xyz_normalized_pixels = tf.multiply(xyz_pixels, [1/0.950456, 1.0, 1/1.088754])

epsilon = 6/29

linear_mask = tf.cast(xyz_normalized_pixels <= (epsilon**3), dtype=tf.float32)

exponential_mask = tf.cast(xyz_normalized_pixels > (epsilon**3), dtype=tf.float32)

fxfyfz_pixels = (xyz_normalized_pixels / (3 * epsilon**2) + 4/29) * linear_mask + (xyz_normalized_pixels ** (1/3)) * exponential_mask

# convert to lab

fxfyfz_to_lab = tf.constant([

# l a b

[ 0.0, 500.0, 0.0], # fx

[116.0, -500.0, 200.0], # fy

[ 0.0, 0.0, -200.0], # fz

])

lab_pixels = tf.matmul(fxfyfz_pixels, fxfyfz_to_lab) + tf.constant([-16.0, 0.0, 0.0])

return tf.reshape(lab_pixels, tf.shape(srgb))

这是到目前为止我所做的,但是我在split中得到了维度错误。check_image检查通道数:

^{pr2}$

任何帮助都将不胜感激。在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值