【问题标题】:Is there a convolution function in Tensorflow to apply a Sobel filter?Tensorflow 中是否有卷积函数来应用 Sobel 滤波器?
【发布时间】:2016-06-04 13:38:19
【问题描述】:

Tensorflow 中是否有任何 convolution 方法可将 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 来解决我的问题吗?

【问题讨论】:

    标签: python tensorflow convolution sobel


    【解决方案1】:

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

    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')
    

    【讨论】:

    • 完成后我发现可以应用 tf.squeeze(filtered_x)。谢谢!
    • 你能解释一下它是如何工作的吗?我如何才能真正卷积真实图像并显示它?
    • 第二个“sobel_x_filter”(soble_x_filter)有错别字。
    • @OliasailO 感谢您指出这一点!我已经更新了代码。
    • 您的 sobel_x 过滤器不正确。 tf.constant([[1, 0, -1], [2, 0, -2], [1, 0, -1]], tf.float32) 最左边的必须与最右边的交换。
    【解决方案2】:

    Tensorflow 1.8 添加了 tf.image.sobel_edges(),所以这是现在最简单也可能是最稳健的方法。

    【讨论】:

      猜你喜欢
      • 2017-11-04
      • 2018-11-27
      • 2020-11-23
      • 1970-01-01
      • 1970-01-01
      • 2018-06-28
      • 2019-11-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多