TensorFlow 图像调整混乱未知尺寸的图像

2024-04-18 23:38:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个可变尺寸的图像列表,希望将其标准化为256x256大小。我用了下面的代码

import tensorflow as tf
import matplotlib.pyplot as plt

file_contents = tf.read_file('image.jpg')
im = tf.image.decode_jpeg(file_contents)
im = tf.image.resize_images(im, 256, 256)

sess = tf.Session()
sess.run(tf.initialize_all_variables())

img = sess.run(im)

plt.imshow(img)
plt.show()

然而,tf.resize_images()往往会弄乱图像。然而,使用tf.reshape()似乎可以正确地使用resize_image()函数

Tensorflow版本:0.8.0

原始图像: enter image description here

调整图像大小: enter image description here

我知道skimage包可以处理我需要的,但是我希望享受tf.train.shuffle_batch()中的功能。我尽量避免维护2个相同的数据集(1个固定的图像大小),因为Caffe处理它们似乎没有问题。在


Tags: run图像imageimportimg尺寸tfas
1条回答
网友
1楼 · 发布于 2024-04-18 23:38:10

发生这种情况是因为image_resize()在相邻像素之间执行插值,并返回介于0-255之间的浮点值而不是整数。这就是为什么NEAREST-NEIGHBOR会起作用:它只需要一个邻近像素的值,而不需要做进一步的数学运算。 假设有一些相邻的像素值为240241。最近的邻居将返回240或241。对于任何其他方法,该值可能类似于240.5,并且返回时没有舍入,我认为是有意的,这样您就可以决定什么更适合您(floor、roundup等)。 这个plt.imshow公司()另一方面,当面对浮点值时,只解释小数部分,就好像它们是0.0到1.0之间的全刻度像素值一样。 为了使上述代码生效,可能的解决方案之一是:

import numpy as np
plt.imshow(img.astype(np.uint8))

相关问题 更多 >