當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python tf.image.resize_with_crop_or_pad用法及代碼示例

將圖像裁剪和/或填充到目標寬度和高度。

用法

tf.image.resize_with_crop_or_pad(
    image, target_height, target_width
)

參數

  • image 形狀為 [batch, height, width, channels] 的 4-D 張量或形狀為 [height, width, channels] 的 3-D 張量。
  • target_height 目標高度。
  • target_width 目標寬度。

拋出

  • ValueError 如果 target_heighttarget_width 為零或負數。

返回

  • 裁剪和/或填充的圖像。如果 images 是 4-D,則形狀為 [batch, new_height, new_width, channels] 的 4-D 浮點張量。如果 images 是 3-D,則形狀為 [new_height, new_width, channels] 的 3-D 浮點張量。

通過集中裁剪圖像或用零均勻填充圖像,將圖像大小調整為目標寬度和高度。

如果 widthheight 分別大於指定的 target_widthtarget_height,則此操作沿該維度集中裁剪。

例如:

image = np.arange(75).reshape(5, 5, 3)  # create 3-D image input
image[:,:,0]  # print first channel just for demo purposes
array([[ 0,  3,  6,  9, 12],
       [15, 18, 21, 24, 27],
       [30, 33, 36, 39, 42],
       [45, 48, 51, 54, 57],
       [60, 63, 66, 69, 72]])
image = tf.image.resize_with_crop_or_pad(image, 3, 3)  # crop
# print first channel for demo purposes; centrally cropped output
image[:,:,0]
<tf.Tensor:shape=(3, 3), dtype=int64, numpy=
array([[18, 21, 24],
       [33, 36, 39],
       [48, 51, 54]])>

如果 widthheight 分別小於指定的 target_widthtarget_height,則此操作沿該維度居中填充 0。

例如:

image = np.arange(1, 28).reshape(3, 3, 3)  # create 3-D image input
image[:,:,0]  # print first channel just for demo purposes
array([[ 1,  4,  7],
       [10, 13, 16],
       [19, 22, 25]])
image = tf.image.resize_with_crop_or_pad(image, 5, 5)  # pad
# print first channel for demo purposes; we should see 0 paddings
image[:,:,0]
<tf.Tensor:shape=(5, 5), dtype=int64, numpy=
array([[ 0,  0,  0,  0,  0],
       [ 0,  1,  4,  7,  0],
       [ 0, 10, 13, 16,  0],
       [ 0, 19, 22, 25,  0],
       [ 0,  0,  0,  0,  0]])>

相關用法


注:本文由純淨天空篩選整理自tensorflow.org大神的英文原創作品 tf.image.resize_with_crop_or_pad。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。