Python tensorflow调整图像大小更改颜色

Python tensorflow调整图像大小更改颜色,python,tensorflow,Python,Tensorflow,在我下面的算法中,resize_images会将图像的颜色更改为错误的颜色。为什么?我的图像是375行1242列3频道 # Typical setup to include TensorFlow. import tensorflow as tf import matplotlib.pyplot as plt # Make a queue of file names including all the JPEG images files in the relative # image direc

在我下面的算法中,resize_images会将图像的颜色更改为错误的颜色。为什么?我的图像是375行1242列3频道

# Typical setup to include TensorFlow.
import tensorflow as tf
import matplotlib.pyplot as plt

# Make a queue of file names including all the JPEG images files in the relative
# image directory.
filename_queue = tf.train.string_input_producer(
   tf.train.match_filenames_once("./MNIST_data/*.png"))

reader = tf.WholeFileReader()
key, value = reader.read(filename_queue)

image_f = tf.image.decode_png(value) # use png or jpg decoder based on your files.
image   = tf.image.resize_images(image_f, [375, 1242])

#Generate batch
# num_preprocess_threads = 1
# min_queue_examples = 256
# batch_size=2;
# images = tf.train.shuffle_batch(
    # [image],
    # batch_size=batch_size,
    # num_threads=num_preprocess_threads,
    # capacity=min_queue_examples + 3 * batch_size,
    # min_after_dequeue=min_queue_examples)

init_op = tf.initialize_all_variables()

with tf.Session() as sess:
  sess.run(init_op)

  # Start populating the filename queue.
  coord = tf.train.Coordinator()
  threads = tf.train.start_queue_runners(coord=coord)
  my_image = image.eval() #here is your image Tensor :) 
  print(my_image.shape)
  fig = plt.figure()
  plt.imshow(my_image)
  plt.show()

  coord.request_stop()
  coord.join(threads)

引自以下文件:

如果调整大小的图像的原始纵横比与大小不同,则图像将失真。要避免失真,请参见
使用\u裁剪\u或\u垫调整\u图像\u的大小

您的纵横比不正确,因此由于扭曲,图像的颜色似乎正在改变。如果不希望发生这种情况,请使用“裁剪”或“垫”调整图像大小



请开始阅读文档。他们会回答你一半以上的问题。

被接受的答案是错误的

tf.image.resize
会更改纵横比,但这不会影响颜色贴图。使用
tf.image.resize\u image\u和\u crop\u或\u pad
也不能解决颜色失真问题

颜色映射由两条规则决定。如果图像值是整数,则使用[0255]中的比例。如果类型是浮点型,则从[0.0,1.0]映射

缩放时,“调整大小”会将整数转换为浮点数,但不会重新缩放值。这可能会导致[0.0255.0]中的值,这会使colormap出错

为了补救,您可以使用
tf.keras.layers.experimental.preprocessing.Rescaling(1./255)
将值转换回int或从[0.0,1.0]缩放值


无论如何,这通常是一个好主意,因为模型倾向于处理较小的值,而不是较大的值,并且您可以缩放其他功能,使它们位于相同的范围内。此外,强制转换为整数会损失少量的方差。

您认为StackOverflow是一种编码服务吗?这是你两天内问的第六个问题!这不是调整大小的错。扩展到下面的答案,一个简单的解决方案是将图像投射到
uint8
<代码>图像=tf.cast(图像,tf.uint8)如果未进行预处理。