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


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

返回每個顏色通道的圖像漸變 (dy, dx)。

用法

tf.image.image_gradients(
    image
)

參數

  • image 形狀為 [batch_size, h, w, d] 的張量。

返回

  • 一對張量 (dy, dx) 保持垂直和水平圖像梯度(1 步有限差分)。

拋出

  • ValueError 如果 image 不是 4D 張量。

兩個輸出張量具有與輸入相同的形狀:[batch_size, h, w, d]。梯度值的組織使得 [I(x+1, y) - I(x, y)] 位於 (x, y) 位置。這意味著 dy 在最後一行總是有零,而 dx 在最後一列總是有零。

使用示例:

BATCH_SIZE = 1
IMAGE_HEIGHT = 5
IMAGE_WIDTH = 5
CHANNELS = 1
image = tf.reshape(tf.range(IMAGE_HEIGHT * IMAGE_WIDTH * CHANNELS,
  delta=1, dtype=tf.float32),
  shape=(BATCH_SIZE, IMAGE_HEIGHT, IMAGE_WIDTH, CHANNELS))
dy, dx = tf.image.image_gradients(image)
print(image[0,:,:,0])
tf.Tensor(
  [[ 0.  1.  2.  3.  4.]
  [ 5.  6.  7.  8.  9.]
  [10. 11. 12. 13. 14.]
  [15. 16. 17. 18. 19.]
  [20. 21. 22. 23. 24.]], shape=(5, 5), dtype=float32)
print(dy[0,:,:,0])
tf.Tensor(
  [[5. 5. 5. 5. 5.]
  [5. 5. 5. 5. 5.]
  [5. 5. 5. 5. 5.]
  [5. 5. 5. 5. 5.]
  [0. 0. 0. 0. 0.]], shape=(5, 5), dtype=float32)
print(dx[0,:,:,0])
tf.Tensor(
  [[1. 1. 1. 1. 0.]
  [1. 1. 1. 1. 0.]
  [1. 1. 1. 1. 0.]
  [1. 1. 1. 1. 0.]
  [1. 1. 1. 1. 0.]], shape=(5, 5), dtype=float32)

相關用法


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