python - 如何在 TensorFlow 中计算 Sobel 边缘检测

标签 python tensorflow

我只是使用 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/

相关文章:

python - Tweepy.cursor 查询项的多/或逻辑函数

javascript - 自动重定向到 div,无需用户干预

machine-learning - 在 CNN 中 Dropout 之前堆叠多个 Conv2D 层背后的直觉

python - Keras 中的自定义损失函数和输出精度不正确

tensorflow - 数十年升级CUDA和cuDNN的最佳实践

opencv - 通过 TensorFlow Lite、Caffe2 或 OpenCV 部署 cnn 模型哪个更快?

Python 变量的参数

python - 如何使用 pyqt4 将小部件添加到网格布局的中心

python - Visual Studio Code 上的覆盖范围

python - tensorflow 中的1和1.0有区别吗?