在tensorflow中手动计算图像梯度



我知道tensorflow中有image_gradients可以像这样得到图像的dx, dy

dx, dy = 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(dx[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(dy[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)

看起来梯度值被组织起来,使得[I(x+1, y) - I(x, y)]位于位置(x, y)。如果我想手动操作,我不知道该怎么做。

我试图输入公式[I(x+1, y) - I(x, y)],但我不知道如何在循环中实现它

x = image[0,:,:,0]
x_unpacked = tf.unstack(x)
processed = []
for t in x_unpacked:
???
processed.append(result_tensor)
output = tf.concat(processed, 0)

或者如果我可以把整个张量移动到x,y方向,我可以做张量减法,但仍然不确定如何处理边缘信息。(在上面的例子中,最后一行/最后一列的值都是0)

如有任何帮助,不胜感激。

对于上面的例子,dx

dx = tf.pad(img[1:,] - img[:-1,], [[0,1],[0,0]])

fordy

dy = tf.pad(img[:,1:] - img[:,:-1], [[0,0],[0,1]])

最新更新