1

我需要在 tensorflow 中实现 sRGB 伽玛曲线。但我无法计算张量流中的元素明智条件。

在 sRGB 曲线中,如果值小于等于 0.0031308,则为线性运算:x*12.95 如果值大于 0.0031308,则为 gamma 校正:1.055*x^(1/2.4) - 0.055

我尝试使用 tf.cond(image > 0.0031308, function1, function2) 但它返回错误。如果有人可以帮助我,我将不胜感激!!!

4

1 回答 1

1

也许你需要tf.where()tf.greater(). 例如:

import tensorflow as tf
import numpy as np

image = np.random.random_sample(size=(2,3,3,1))/100
print(image)

image_tf = tf.placeholder(shape=(None,3,3,1),dtype=tf.float32)

new_image = tf.where(tf.greater(image_tf,0.0031308)
                     ,1.055*tf.pow(image_tf,1/2.4) - 0.055
                     ,image_tf*12.95)

with tf.Session() as sess:
    print(sess.run(new_image,feed_dict={image_tf:image}))
# image
[[[[0.0048715 ]
   [0.00472688]
   [0.00138391]]

  [[0.00848472]
   [0.00055767]
   [0.00835372]]

  [[0.00872362]
   [0.00390934]
   [0.00795842]]]


 [[[0.00756143]
   [0.00494474]
   [0.00201968]]

  [[0.00350234]
   [0.0056558 ]
   [0.00602147]]

  [[0.00692543]
   [0.0045199 ]
   [0.00012196]]]]
# new image
[[[[0.05975685]
   [0.05832487]
   [0.01792167]]

  [[0.08960549]
   [0.0072218 ]
   [0.08867098]]

  [[0.09128823]
   [0.04970375]
   [0.08579818]]]


 [[[0.08282798]
   [0.06047264]
   [0.02615486]]

  [[0.04501574]
   [0.06712133]
   [0.07035124]]

  [[0.07787357]
   [0.05623017]
   [0.00157937]]]]
于 2019-03-21T02:31:26.633 回答