我只是使用 TensorFlow 计算形状为 [16,96,96,1] 的张量 A 的 Sobel Edge 图(16 是批量大小,96 是图像 block 大小,1 是数量 channel (这里只考虑亮度 channel ))。
我发现在 TensorFlow 中,有一个名为“tf.image.sobel_edges”的函数可以返回每个 channel 的边缘图。对于此函数,它返回形状为 [16,96,96,1,2] 的张量。我不明白最后2维的意思.....我的理解是边缘图应该是二值图像,所以输出应该是[16,96,96,1],但是这个输出function is [16,96,96,1,2]......如果我只想获取图像的边缘能量,我怎么从这个函数的输出中做呢?
你能解释一下吗?提前致谢!
最佳答案
tf.image.sobel_edges文档表明,在单 channel 图像的情况下,返回的张量包含图像沿水平轴和垂直轴的梯度分量。为了计算该梯度的大小并获得边缘能量图像,我们只需要计算这些分量之和的平方根,如下所示:
import tensorflow as tf
tf.enable_eager_execution()
img = tf.random.normal(shape=(16,96,96,1),dtype=tf.float32) # replace with your image data
grad_components = tf.image.sobel_edges(img)
grad_mag_components = grad_components**2
grad_mag_square = tf.math.reduce_sum(grad_mag_components,axis=-1) # sum all magnitude components
grad_mag_img = tf.sqrt(grad_mag_square) # this is the image tensor you want
关于python - 如何在 TensorFlow 中计算 Sobel 边缘检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56740582/