gpt4 book ai didi

python - Tensorflow 中是否有卷积函数来应用 Sobel 滤波器?

转载 作者:太空狗 更新时间:2023-10-30 01:46:17 24 4
gpt4 key购买 nike

Tensorflow 中是否有任何卷积 方法可以将 Sobel 滤波器应用于图像 img(类型为 float32 且秩为 2 的张量)?

sobel_x = tf.constant([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]], 'float32')
result = tf.convolution(img, sobel_x) # <== TO DO THIS

我已经看过 tf.nn.conv2d 但我看不到如何将它用于此操作。有什么方法可以使用 tf.nn.conv2d 来解决我的问题吗?

最佳答案

也许我在这里遗漏了一个微妙之处,但您似乎可以使用 tf.expand_dims() 将 Sobel 过滤器应用于图像和 tf.nn.conv2d() ,如下:

sobel_x = tf.constant([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]], tf.float32)
sobel_x_filter = tf.reshape(sobel_x, [3, 3, 1, 1])
sobel_y_filter = tf.transpose(sobel_x_filter, [1, 0, 2, 3])

# Shape = height x width.
image = tf.placeholder(tf.float32, shape=[None, None])

# Shape = 1 x height x width x 1.
image_resized = tf.expand_dims(tf.expand_dims(image, 0), 3)

filtered_x = tf.nn.conv2d(image_resized, sobel_x_filter,
strides=[1, 1, 1, 1], padding='SAME')
filtered_y = tf.nn.conv2d(image_resized, sobel_y_filter,
strides=[1, 1, 1, 1], padding='SAME')

关于python - Tensorflow 中是否有卷积函数来应用 Sobel 滤波器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35565312/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com