2016-02-22 90 views
2

Tensorflow中是否有任何convolution方法将Sobel滤镜应用于图像imgfloat32类型的张量和等级2)?Tensorflow中是否有卷积函数来应用Sobel滤波器?

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来解决我的问题吗?

回答

11

也许我在这里失去了一个精妙,但现在看来,您可以应用索贝尔滤波器使用tf.expand_dims()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') 
+0

完成后,我发现它可以适用TF。挤(filtered_x)。谢谢! – axelbrz

+2

你能再解释一下它是如何工作的? 我怎样才能真正卷积一个真实的图像并显示它? – mavi

+0

第二个“sobel_x_filter”(soble_x_filter)中存在拼写错误。 – OliasailO