在Tensorflow中调整图像保留纵横比的大小

kur*_*nes 2 image image-processing conv-neural-network tensorflow

我对TF非常陌生。我正在尝试调整图像张量的大小,以使图像的最低尺寸为常数LO_DIM。在非tf环境中,我只会做这样的事情:

if img.size[0] < img.size[1]:
    h = int(float(LO_DIM * img.size[1]) / img.size[0])
    img = resize(img, [LO_DIM, h])
else:
    w = int(float(LO_DIM * img.size[0]) / img.size[1])
    img = resize(img, [w, LO_DIM])
Run Code Online (Sandbox Code Playgroud)

我知道,应该调整大小以使用大小tf.image.resize_images,但是我不确定如何计算新的大小wh考虑到张量似乎具有shape=<unknown>

注意:我传递的每个图像可能具有不同的大小,这就是为什么我需要动态计算它的原因。而且我正在使用LO_DIM来保持宽高比并且不会扭曲图像。

关于如何实现这一目标的任何建议?

如果有帮助,处理目标是从缩放后的图像中获取随机的NxN色块,但我所能找到的只是resize_image_with_crop_or_pad似乎没有进行初始缩放。

kur*_*nes 5

这个问题回答了。

这是一个示例片段,用于调整张量图像的大小并保持aspext比率:

def resize_image_keep_aspect(image, lo_dim=LO_DIM):
  # Take width/height
  initial_width = tf.shape(image)[0]
  initial_height = tf.shape(image)[1]

  # Take the greater value, and use it for the ratio
  min_ = tf.minimum(initial_width, initial_height)
  ratio = tf.to_float(min_) / tf.constant(lo_dim, dtype=tf.float32)

  new_width = tf.to_int32(tf.to_float(initial_width) / ratio)
  new_height = tf.to_int32(tf.to_float(initial_height) / ratio)

  return tf.image.resize_images(image, [new_width, new_height])
Run Code Online (Sandbox Code Playgroud)

张量为shape=<unknown>的问题通过使用类型特定的解码器(例如tf.image.decode_jpeg或)解决,tf.image.decode_pngtf.image.decode_image