tf.image.resize()改变图像大小

概述

改变图像的大小,默认会 改变 长宽比例

官方api:
https://www.tensorflow.org/api_docs/python/tf/image/resize

语法

  1. tf.image.resize(
  2. images,
  3. size,
  4. method='bilinear',
  5. preserve_aspect_ratio=False,
  6. antialias=False,
  7. name=None,
  8. )
参数:
  • images:4维形状张量[batch, height, width, channels] 或 3维张量形状为 [height, width, channels]
    一般为 tf.io.decode_jpeg(img_tensor)返回对象 或者 numpy类型
    不能为tf.io.read_file()返回对象

  • size:由2个元素组成的一维int32张量:“new_height,new_width”。新的图像大小。

  • method=’bilinear’:

    • bilinear:双线性插值,转换后,像素值不越界、会有浮点数
    • nearest:最近邻插值,“ antialias”参数无效
      转换后,像素值不越界、整数,所以会有不连续感
    • bicubic:双三次插值,比Lanczos3合理的质量好且速度更快,尤其是在上采样时。
      转换后,像素值越界,即:负数 或 大于255,浮点数
    • area:使用区域插值进行抗锯齿的重采样。当与区域插值一起使用时,“ antialias”无效。它总是抗锯齿。
      转换后,像素值越界,即:大于255,浮点数
    • lanczos3:半径为3的Lanczos核,高质量的实用滤镜,但可能会有些振铃,尤其是在合成图像上。
      转换后,像素值越界,即:负数 或 大于255,浮点数
    • lanczos5:半径为5的Lanczos内核,非常高质量的过滤器,但振铃可能更强
      转换后,像素值越界,即:负数 或 大于255,浮点数
    • gaussian:半径为3的高斯核
      转换后,像素值越界,即:大于255,浮点数
    • mitchellcubic:Mitchell-Netravali三次非插值滤波器。对于合成图像(尤其是那些缺少适当的预过滤的图像),比Keys立方核的振铃少,但清晰度不高。
      转换后,像素值越界,即:大于255,浮点数
  • preserve_aspect_ratio=False:是否保存高宽比。如果设置True,将“images”调整为适合“size”的大小,同时保持原始图像的宽高比。

  • antialias=False:抗锯齿:在下采样时是否使用抗锯齿过滤器

  • name:此操作的名称(可选)

返回值
  • float32类型的Tensor,其内容是numpy数组类型的图像。
    如果要 通过matplotlib显示图片,需要使用tf.cast(img,"int8")转换成 int8 类型

    • 如果images为4-D,则为shape的4-D浮动张量 [batch, new_height, new_width, channels]。
    • 如果images为3-D,则为shape的3-D浮动张量 [new_height, new_width, channels]。

例子

  1. import tensorflow as tf
  2. img_tensor=tf.io.read_file(r"C:\Users\mym\Desktop\数据集\2_class\lake\lake_001.jpg")
  3. img_data=tf.io.decode_jpeg(img_tensor)
  4. # 改变原图片的长宽比例
  5. img_data2=tf.image.resize(img_data,(200,800))
  6. img_data2=tf.cast(img_data2,"uint8")
  7. plt.imshow(img_data2.numpy())

可能会出现的问题

https://blog.csdn.net/shentanyue/article/details/83819102
https://blog.csdn.net/u011583927/article/details/103307473


原文出处:https://malaoshi.top/show_1EF4XayTFLyw.html