tensorflow tf.contrib.image.rotate是如何工作的?

3

我刚开始使用tensorflow和python,并且稍微尝试了一下,但是无法弄清楚如何使用tf.contrib.image.rotate。例如,在tf.cast操作中,我可以将其用作张量。

也许有人能帮我弄清楚如何使用它?

Greetz

2个回答

5

您正在混合两个不同的tensorflow函数。

tf.contrib.image.rotate: 此函数用于旋转图像到任意角度。

X_img =  # Your image or batch of images
degree_angle = 45 # In degrees
radian = degree_angle * math.pi / 180
tf_img = tf.contrib.image.rotate(X_img, radian)
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    rotated_img = sess.run(tf_img)

tf.cast:用于将张量的数据类型从一种形式转换为另一种形式。以下是将数据转换成float32格式的示例:

casted_tensor = tf.cast(input_tensor, tf.float32)

你能告诉我如何解析 X_img 吗?尝试过:file_reader = tf.read_file(file_name, input_name) X_img = tf.image.decode_jpeg(file_reader, channels = 3, name='jpeg_reader')但是 Python 崩溃了。 - QSpot
@QSpot 我建议您使用 matplotlib.image.imread 函数来读取图像。 - Prasad
对我没用,你能展示一下从文件系统加载图像(包括jpg格式),旋转并转换为浮点类型的整个过程吗? - QSpot

1

对于那些仍在寻找答案的人,从TensorFlow 2.x开始,tf.contrib已被弃用,因此tf.contrib.image.rotate将不再起作用。

相反,您可以使用tensorflow_addons库中的tfa.image.rotate函数。该函数将图像逆时针旋转,并以弧度为输入接受角度。

img = ... #insert image(s) here
angle = 60 #in degrees
angle_radians = angle * math.pi / 180
rotated_img = tfa.image.rotate(img, angle_radians)

好的,它们都以弧度角作为输入。 - zheyuanWang

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接